Ansible常用模組實例
- 2019 年 12 月 12 日
- 筆記
ansible主要文件

(/etc/ansible/ansible.cfg)
ansible主配置文件。
(/etc/ansible/hosts)
主機清單,保存管理的主機資訊。
(/etc/ansible/roles)
公共角色,主要在自動化部署多台主機時應用。
ansible命令集
ansible
定義並運行簡單任務。
ansible-config
查看、編輯、管理ansible配置。
ansible-doc
文檔查看工具。
ansible-galaxy
共享和下載roles的工具。
ansible-inventory
查看inventory的資訊。
ansible-playbook
執行playbook。
ansible-pull
從倉庫中拉去playbook。
ansible-vault
文件加密工具。
ansible-console
repl控制台執行ansible任務。
配置SSH免秘鑰訪問
為了避免ansible每次下髮指令都要輸入目標主機密碼,所以這裡使用(ssh-keygen)在控制主機創建一對秘鑰,使用(ssh-copy-id)來下發生成的公鑰。
[root@master ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): (回車) Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase):(回車) Enter same passphrase again:(回車) Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: e8:2b:a7:a2:6b:19:87:0b:b5:4e:fc:46:5b:9e:3d:7b root@master The key's randomart image is: +--[ RSA 2048]----+ | . . | | o.. . S | |.o+.... | |.o=o +.o | | +o = +.o E | |+o o.+. .+ | +-----------------+
在/root/.ssh/下生產一對秘鑰,其中(id_rsa)為私鑰,(id_rsa.pub)為公鑰.

[root@master ~]# tree /root/.ssh/ /root/.ssh/ ├── id_rsa └── id_rsa.pub
控制主機把公鑰(id_rsa.pub)下發到被管節點上用戶下的.ssh目錄,並重命名為authorized_key,許可權製為400,這裡使用秘鑰拷貝工具(ssh-copy-id)把公鑰文件(id_rsa.pub)拷貝到被管節點。
[root@master ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] The authenticity of host '192.168.1.111 (192.168.1.111)' can't be established. ECDSA key fingerprint is 75:5c:83:a1:b4:cc:bf:28:71:a5:d5:d1:94:35:3c:9a. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]'" and check to make sure that only the key(s) you wanted were added.
公鑰分發後,需要驗證SSH無密碼登錄,如下為成功。
[root@master ~]# ssh [email protected] Last login: Wed Jul 17 23:13:35 2018 from master [root@slave ~]#
如果有多台節點分發公鑰和上面一樣,或者寫一個簡單的腳本分發。
主機連通性測試
[root@master ~]# cat >>/etc/ansible/hosts <<EOF > [cluster_host] > 192.168.1.111 > EOF [root@master ~]# ansible cluster_host -m command -a 'uptime' 192.168.1.111 | SUCCESS | rc=0 >> 09:58:17 up 1:03, 2 users, load average: 0.01, 0.04, 0.05
常用模組解析
查詢某個模組請執行:(ansible-doc -s 模組名)
查詢更多模組請執行:(ansible-doc -l)
(setup)模組
#查看目標主機的一些資訊
示例:
[root@master ~]# ansible cluster_host -m setup 192.168.1.111 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.16.1.15", "192.168.1.111" ], "ansible_all_ipv6_addresses": [ "fe80::20c:29ff:fe91:ce86", "fe80::20c:29ff:fe91:ce7c" ], "ansible_apparmor": { "status": "disabled" }
(ping)模組
#測試目標主機的運行狀態
示例:
[root@master ~]# ansible cluster_host -m ping 192.168.1.111 | SUCCESS => { "changed": false, "ping": "pong" }
(file)模組
#對目標主機的文件進行操作
參數如下:
force
在兩種情況下強制創建軟鏈接
1.源文件不存在但之後會建立的情況下;
2.目標軟連接已經存在,需要取消之前的軟鏈接,然後創建洗的軟連接,有兩個選項:yes|no;
group
定義文件/目標的屬組。
mode
定義文件/目錄的許可權。
owenr
定義文件/目錄的屬主。
path
必選項,定義文件/目錄的路徑。
recurse
遞歸的設置文件的屬性,只對目錄有效。
src
要被鏈接的源文件的路徑,只應用於state=link的情況。
dest
被鏈接到的路徑,只應用於state=link的情況。
file
即使文件不存在,也不會被創建。
link
創建軟連接。
hard
創建硬鏈接。
touch
如果文件不存在,則會創建一個新的文件,如果文件或目錄已存在,則更新其最後修改時間。
absent
刪除目錄、文件或者取消鏈接文件。
示例:
#創建遠程文件符號鏈接 [root@master ~]# ansible cluster_host -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link" 192.168.1.111 | SUCCESS => { "changed": true, "dest": "/tmp/resolv.conf", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 16, "src": "/etc/resolv.conf", "state": "link", "uid": 0 } [root@master ~]# #遠程文件資訊查看 [root@master ~]# ansible cluster_host -m command -a "ls -la /tmp/resolv.conf" 192.168.1.111 | SUCCESS | rc=0 >> lrwxrwxrwx 1 root root 16 7月 18 10:21 /tmp/resolv.conf -> /etc/resolv.conf #刪除遠程文件符號鏈接 [root@master ~]# ansible cluster_host -m file -a "path=/tmp/resolv.conf state=absent" 192.168.1.111 | SUCCESS => { "changed": true, "path": "/tmp/resolv.conf", "state": "absent" } #遠程文件資訊查看 [root@master ~]# ansible cluster_host -m command -a "ls -la /tmp/resolv.conf" 192.168.1.111 | FAILED | rc=2 >> ls: 無法訪問/tmp/resolv.conf: 沒有那個文件或目錄non-zero return code
(copy)模組
#進行遠程複製
參數如下:
src
被複制到遠程主機的本地對象文件或者文件夾,可以是絕對路徑,也可以是相對路徑。
dest
被複制到遠程主機的本地對象文件或者文件夾。
mode
複製對象的設定許可權。
backup
在文件存在的時候可以選擇覆蓋之前,將源文件備份.設定值:yes/no 預設為yes。
force
是否強制覆蓋.設定值:yes/no 預設為no。
示例:
#將本地文件/etc/ansible/ansible.cfg複製到目標機器/tmp/ansible.cfg [root@master ~]# ansible cluster_host -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644" 192.168.1.111 | SUCCESS => { "changed": true, "checksum": "4cb7c9f51f91d0c5895c2ae5bca18b4904ed407e", "dest": "/tmp/ansible.cfg", "gid": 0, "group": "root", "md5sum": "1f7011f69060831cfcd3b04136c2be6a", "mode": "0644", "owner": "root", "size": 19549, "src": "/root/.ansible/tmp/ansible-tmp-1531881922.82-138288626316266/source", "state": "file", "uid": 0 } #查看目標主機的/tmp/ansible.cfg文件 [root@master ~]# ansible cluster_host -m command -a "ls -la /tmp/ansible.cfg" 192.168.1.111 | SUCCESS | rc=0 >> -rw-r--r-- 1 root root 19549 7月 18 10:45 /tmp/ansible.cfg [root@master ~]#
(command)模組
#在遠程主機上執行命令
參數如下:
chdiiv
在執行命令之前,通過cd命令進入指定目錄中。
creates
定義一個文件是否存在,如果不存在運行相應命令;如果存在跳過此步驟。
executable
改變shell使用command進行執行,並且執行時要使用絕對路徑。
free_form
命令模組採用自由形式命令組合;即可以輸入任意linux命令。
removes
定義一^文件是否存在,如果存在運行相應命令;如果不存在跳過此步驟。
warn
如果ansible配置文件中定義了命令警告,如果參數設置了no/false將不會警告此行命令。
示例:
[root@master ~]# ansible cluster_host -m command -a "pwd" 192.168.1.111 | SUCCESS | rc=0 >> /root [root@master ~]# ansible cluster_host -m command -a "who" 192.168.1.111 | SUCCESS | rc=0 >> root pts/0 2018-07-18 09:13 (10.0.0.1) root pts/1 2018-07-18 10:58 (10.0.0.5)
(shell)模組
#切換到某個shell執行指定命令,參數與command模組相同
示例:
#本地創建一個shell腳本 [root@master ~]# vim /tmp/test.sh #!/bin/bash date +%F_%H.%M.%S :wq [root@master ~]# chmod +x /tmp/test.sh #將創建的腳本分發到目標主機 [root@master ~]# ansible cluster_host -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh owner=root group=root mode=0755" 7192.168.1.111 | SUCCESS => { "changed": true, "checksum": "eccafd201f4a61757facc4a34e67e955b4f8a03f", "dest": "/tmp/test.sh", "gid": 0, "group": "root", "md5sum": "f027af9860d5c4548220b09bfa042b57", "mode": "0755", "owner": "root", "size": 30, "src": "/root/.ansible/tmp/ansible-tmp-1531883311.27-270962148668548/source", "state": "file", "uid": 0 } #目標主機執行 [root@master ~]# ansible cluster_host -m shell -a "/tmp/test.sh" 192.168.1.111 | SUCCESS | rc=0 >> 2018-07-18_11.09.31
更多模組
service
系統服務管理
cron
計劃任務管理
yum
yum軟體包安裝管理
synchronize
使用rsync同步文件
user
系統用戶管理
group
系統用戶組管理