工欲善其事,必先利其器,所以今天呢,我們要先來把 Ansible 的環境給弄好。
那因為筆者自己用的是 macOS,所以今後的文章還是會以 macOS 為主。
以下提供幾種常見的安裝方式,選一種你習慣的就好。
# 作法 A:用 Homebrew(簡單直接,推推)
brew install ansible
# 作法 B:用 pipx(官方的另一種做法,但我覺得有點麻煩)
brew install pipx
pipx ensurepath
pipx install --include-deps ansible
# 路線 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 8+ / CentOS Stream
sudo dnf install ansible
# CentOS 7(要注意 Redhat 家族有兩種套件管理工具)
sudo yum install epel-release
sudo yum install ansible
# 建議先安裝 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
幾個重點可以先記起來:
確認模組能跑,對 localhost 來個最小化測試:
ansible -i localhost, -c local -m ping all
如果看到類似這樣就代表沒問題:
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansible
,重開一個終端機或確認 PATH。pipx
安裝,避免和系統 Python 互相影響。ansible --version
,確認安裝成功。ansible -i localhost, -c local -m ping all
做基本功能驗證。今天裝好 Ansible 了,明天我們就要來設定 Inventory,開始跟遠端主機打交道囉!