Ansible的安裝和命令的介紹
ansible管理機可以裝在任何的作業系統上,除了windows。下面介紹在centos7上安裝ansible。
最簡單的安裝方式,yum安裝:
yum install ansible
配置
ansbile預設的配置檔案路徑為 /etc/ansible
ansible.cfg 配置檔案,這個配置檔案預設就好了。如果有需求可以根據自己的需求去修改
#inventory = /etc/ansible/hosts #預設存放可通訊主機的目錄檔案
#library = /usr/share/my_modules/ #預設搜尋模組位置
#module_utils = /usr/share/my_module_utils/
#remote_tmp = ~/.ansible/tmp #遠端的臨時目錄
#local_tmp = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5 #在與主機通訊時的預設並行程序數,預設併發是5
#poll_interval = 15
#sudo_user = root
#ask_sudo_pass = True
#ask_pass = True
#transport = smart
#remote_port = 22 #遠端SSH埠,預設是22
#module_lang = C #模組和系統之間通訊的計算機語言,預設是C語言
#module_set_locale = False
備註:其他引數設定 可以參考ansible的中文文件。中文文件地址
hosts 主機清單檔案
Ansible 可同時操作屬於一個組的多臺主機,組和主機之間的關係通過 inventory 檔案配置. 預設的檔案路徑為 /etc/ansible/hosts
[zabbix_agentd_servers]
172.30.47.203 # op01
172.30.47.201 # op02
172.30.47.202 # proxy01
172.30.47.101 # asrtest01
[lvs_servers]
172.30.47.141
172.30.47.142
[test]
172.17.30.102
[test:vars]
ansible_ssh_port=9022
中括號[] 裡面是主機名,用於對系統進行分類。為了方便不通的系統管理,可以設定多個分組,用中括號區分即可。如果埠非預設埠,要單獨增加一個變數,例如上面的test組。
主機除了是ip地址,也可以寫成是別名或者是主機名的方式。
[test]
test.sunqfrs.com:9022
不過,你需要在hosts裡面做好主機 與 別名的對應關係。
如果埠不是預設的埠直接在後面加上埠號,用冒號分割即可。
公鑰認證
如果你的管理機和被管理機之間沒有做公鑰認證的話,每次執行ansibie命令 都要輸入被管理機器的ssh登入密碼
下面在管理機上生成一個公鑰,然後分發的其他的機器上去:
[[email protected] ~]# 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:
ad:cb:d7:c0:e6:1c:fe:5c:d4:d4:34:f1:a1:91:36:20 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| E ....+o|
| . +o.=|
| ... +|
| . o |
| S.. . .|
| .= . |
| .= + . |
| . .=... |
| o. .o |
+-----------------+
3.然後 執行
[[email protected] ~]# ssh-copy-id .ssh/id_rsa.pub [email protected] #拷貝公鑰到另外一臺電腦去
#如果埠非22採用以下方式
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub '-p 9822 [email protected] '
#主機登入其他兩臺就不需要密碼登入了。
[[email protected] ~]# ssh [email protected] #測試直接登入
ansible 命令
安裝完之後,發現ansible一共為我們提供了7個命令: ansible、ansible-doc、ansible-galaxy、ansible-lint、ansible-playbook、ansible-pull、ansible-vault
我們日常用的最多的兩個命令是:ansible 和 ansible-playbook
ansible
格式: ansible <host-pattern> [options]
可用選項:
-v, --verbose 輸出更詳細的執行過程
-i 指定inventory 資訊,預設是用/etc/ansible/hosts
-l 選擇指定的組
-f FORKS, --forks=FORKS 併發執行緒數,預設是5個執行緒
--private-key=PRIVATE_KEY_FILE: 指定金鑰檔案。
-m NAME, --module-name=NAME: 指定執行使用的模組。
還有其他很多引數,詳情請看 ansible --help
執行:
[[email protected] ~]# ansible all -m shell -a "w"
192.168.137.102 | SUCCESS | rc=0 >>
14:51:11 up 37 min, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 192.168.137.1 14:22 27:59 0.01s 0.01s -bash
root pts/1 192.168.137.101 14:51 0.00s 0.14s 0.05s w
ansible-doc
ansible-doc 命令常用於獲取模組資訊及其使用幫助,一般用法如下
ansible-doc -l #獲取全部模組資訊
ansible-doc -s 模組名字 #獲取指定模組的使用幫助。
例子:
ansible-doc -s shell #獲取shell 模組的幫助資訊
ansible-lint
ansible-lint是對playbook的語法進行檢查的一個工具。用法是ansible-lint playbook.yml 。
ansible-playbook
該指令是使用最多的指令,其通過讀取playbook 檔案後,執行相應的動作,這個後面會做為一個重點來講。
備註:其他命令我用得比較少,如果需要用到的話,可以使用--help 查詢。