iT邦幫忙

2025 iThome 鐵人賽

DAY 2
0
DevOps

不爆肝學習 Ansible 的短暫30天系列 第 2

Day 02 – 安裝 Ansible

  • 分享至 

  • xImage
  •  

工欲善其事,必先利其器,所以今天呢,我們要先來把 Ansible 的環境給弄好。

那因為筆者自己用的是 macOS,所以今後的文章還是會以 macOS 為主。

今日目標

  • 在本機安裝好 Ansible
  • 確認 Ansible 安裝成功,基本指令可用

如何安裝呢?

以下提供幾種常見的安裝方式,選一種你習慣的就好。

macOS

# 作法 A:用 Homebrew(簡單直接,推推)
brew install ansible

# 作法 B:用 pipx(官方的另一種做法,但我覺得有點麻煩)
brew install pipx
pipx ensurepath
pipx install --include-deps ansible

Ubuntu

# 路線 A:使用發行版套件
sudo apt update
sudo apt install ansible

# 路線 B:使用官方 PPA(如果要最新版本,建議用這個)
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

Fedora / RHEL / CentOS

# Fedora / RHEL 8+ / CentOS Stream
sudo dnf install ansible

# CentOS 7(要注意 Redhat 家族有兩種套件管理工具)
sudo yum install epel-release
sudo yum install ansible

Windows

# 建議先安裝 WSL,然後在 WSL (Ubuntu) 內依照上面的 Ubuntu 步驟安裝

那安裝完後要怎麼確認呢?

安裝完成後,先看一下版本與環境資訊:

ansible --version

你會看到類似這樣的輸出:

ansible [core 2.18.x]
  config file = None
  configured module search path = [...]
  ansible python module location = ...
  ansible collection location = ...
  executable location = /opt/homebrew/bin/ansible
  python version = 3.x.x
  jinja version = 3.1.x
  libyaml = True

幾個重點可以先記起來:

  1. core 版本:Ansible 的核心版本號。
  2. config file:如果是 None,代表還沒有使用 ansible.cfg,之後會介紹。
  3. python version:Ansible 依賴 Python,建議 3.10+。
  4. jinja version:模板引擎,之後做動態設定會用到。

接著跟自己打聲招呼吧

確認模組能跑,對 localhost 來個最小化測試:

ansible -i localhost, -c local -m ping all

如果看到類似這樣就代表沒問題:

localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

常見問題

  • 路徑問題:安裝完找不到 ansible,重開一個終端機或確認 PATH。
  • 多個 Python:盡量用 pipx 安裝,避免和系統 Python 互相影響。
  • 版本混用:Homebrew 和 pipx 同時安裝可能造成混亂,擇一即可。

作業練習時間

  1. 在自己的電腦上完成 Ansible 安裝(任選一種方式)。
  2. 執行 ansible --version,確認安裝成功。
  3. 執行 ansible -i localhost, -c local -m ping all 做基本功能驗證。

明日預告

今天裝好 Ansible 了,明天我們就要來設定 Inventory,開始跟遠端主機打交道囉!


上一篇
Day 01 – 為什麼要使用 Ansible?
下一篇
Day 03 – 認識 Inventory
系列文
不爆肝學習 Ansible 的短暫30天4
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言