Ansible 安裝
Ansible 有三種安裝方式,源碼安裝、發行版安裝和?Python 安裝。
使用發行版安裝或?Python 安裝兩種方式時,Ansible 的安裝包有兩個,區別如下:
-
??
ansible-core
:一種極簡語言和運行時包,包含一組內置模塊和插件。 -
??
ansible
:一個更大的“包含電池”的軟件包,它添加了社區精選的?Ansible 集合選擇,用于自動化各種設備。
在用源碼或者?Python?安裝 Ansible 時,默認不會安裝?
sshpass
?軟件包,該軟件包用來給 Ansible 提供密碼驗證被控端,因此如果在執行 Ansible 的命令時需要輸入 ssh 的密碼,則需要該軟件包,該軟件包通過?dnf install -y sshpass
。[root@ansible ansible]# ansible servera -m ping servera | FAILED! => {"msg":?"to use the 'ssh' connection type with passwords or pkcs11_provider, you must install the sshpass program" }
本次安裝使用 Rocky 8 Linux 系統。
源碼安裝
[root@ansible ~]# dnf install python3.12 python3.12-pip sshpass
[root@ansible ~]# tar xf ansible-2.16.3.tar.gz
[root@ansible ~]#?cd?ansible-2.16.3/
[root@ansible ansible-2.16.3]# python3 -m pip install -r ./requirements.txt
[root@ansible ansible-2.16.3]# python3 setup.py install
源碼安裝只能安裝?
Ansible-core
。
發行版安裝
[root@ansible ~]# dnf install -y epel-release# 安裝最簡潔的 Ansible
[root@ansible ~]# dnf install ansible-core# 安裝包含常用模塊的 Ansible
[root@ansible ~]# dnf install ansible
Python 安裝
# 安裝 Python3 和 pip
[root@ansible ~]# dnf install python3.12 python3.12-pip sshpass# 安裝 Ansible-core
[root@ansible ~]# python3.12 -m pip install ansible-core==2.16.3# 安裝 Ansible
[root@ansible ~]# python3.12 -m pip install ansible
設置 Ansible 參數自動補全
[root@ansible ~]# python3 -m pip install argcomplete
[root@ansible ~]# activate-global-python-argcomplete --user
重新登錄命令行加載一下環境變量就可以看到自動補全了。
快速配置并使用 Ansible
有一個被控節點,地址為?192.168.221.131
,主機名為?servera
。
[root@ansible ~]#?mkdir?ansible
[root@ansible ~]#?cd?ansible
[root@ansible ansible]# sed -i?'s/;inventory=.*/inventory\ =\ .\/inventory/'?ansible.cfg
[root@ansible ansible]#?cat?<<-EOF > ansible.cfg
[defaults]
inventory ? ? ?= ./inventory
ask_pass ? ? ? = false
remote_user ? ?= ?root
log_path ? ? ? = /var/log/ansible.log
host_key_checking = False
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
[ssh_connection]
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
EOF
[root@ansible ansible]#?echo?servera > inventory
[root@ansible ansible]#?tail?-n1 /etc/hosts
192.168.221.131 servera# 通過 ping 模塊測試網絡連通性
[root@ansible ansible]# ansible all -k -m ping
SSH password:
servera | SUCCESS => {"ansible_facts": {"discovered_interpreter_python":?"/usr/libexec/platform-python"},"changed":?false,"ping":?"pong"
}