2016-12-23
讀這本《Ansible權威指南》學習ansible,根據本書內容和網上的各種文檔,以及經過自己測試,寫出以下筆記。另,這本書內容很好,但印刷錯誤比較多,作者說第二版會改進,還沒買的小伙伴們可以買第二版。
一、安裝
1、安裝要求:
控制服務器:需要安裝Python2.6/2.7
被管理服務器:需要安裝Python2.4 以上版本,若低於Python2.5 需要安裝pythonsimplejson;若啟用了selinux,則需要安裝libselinux-python
2、yum安裝
yum install ansible -y
安裝方式很多,個人推薦使用epel源安裝,epel源安裝方式不再贅述。截至目前,epel源安裝的是2.2版。
3、簡單配置
vim /etc/ansible/ansible.cfg 默認配置文件,讀取配置文件的順序是當前目錄——當前用戶家目錄——/etc/ansible/ansible.cfg(該順序未驗證,建議在統一的地方配置,以免混亂)
#remote_user = root 默認使用的遠程連接用戶
vim /etc/ansible/hosts Inventory文件的默認位置,指定所要管理的主機安裝
二、ssh密鑰登陸
ansilbe采用ssh的方式管理節點,為了方便管理,使用密鑰方式面密碼登陸被管理節點。
1、生成rsa格式密鑰
ssh-keygen -t rsa
2、把公鑰寫入到遠端主機的~/.ssh/authorized_keys
ssh-copy-id username@192.168.1.50
3、管理機設置默認遠程用戶
vim /etc/ansible/ansible.cfg
remote_user = username
三、ansible命令
1、ansible 臨時的一次性操作
Usage: ansible <host-pattern> [options]
host-pattern可以是域名,IP,也可以在/etc/ansible/hosts指定
options
-m 后接模塊
-a 后接模塊參數
-u 指定用戶名
-f 啟動的並發線程數
例:
ansible all -m ping
ansible all -m copy -a "src=/etc/fstab dest=/tmp/fatab owner=root group=root mode=644 backup=yes"
報錯:
The authenticity of host '[192.168.99.43]:22051 ([192.168.99.43]:22)' can't be established.
RSA key fingerprint is 5e:9d:5c:4c:e8:cd:6e:78:70:a2:04:1c:5f:6f:3a:1e.
Are you sure you want to continue connecting (yes/no)? The authenticity of host '[192.168.99.248]:22051 ([192.168.99.248]:22)' can't be established.
RSA key fingerprint is 64:d9:ef:67:6a:d5:37:ff:70:2f:06:d2:35:d1:6b:a2.
Are you sure you want to continue connecting (yes/no)? The authenticity of host '[192.168.99.247]:22051 ([192.168.99.247]:22)' can't be established.
解決方法:
sed -i 's/^#host_key_checking = False/host_key_checking = False/' /etc/ansible/ansible.cfg
2、ansible-doc 查看模塊文檔
Usage: ansible-doc [options] [module...]
Options:
-h, --help show this help message and exit
-l, --list List available modules
-M MODULE_PATH, --module-path=MODULE_PATH
specify path(s) to module library (default=None)
-s, --snippet Show playbook snippet for specified module(s)
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
--version show program's version number and exit
ansible-doc 模塊名 模塊說明
ansible-doc -s 模塊名 簡要說明
ansible-doc -l 報錯:
[DEPRECATION WARNING]: docker is kept for backwards compatibility but usage is discouraged. The module documentation details page may explain more about this rationale..
This feature will be removed in
a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[ERROR]: unable to parse /usr/lib/python2.6/site-packages/ansible/modules/extras/cloud/misc/rhevm.py
ERROR! module rhevm has a documentation error formatting or is missing documentation
解決方法:
sed -i 's/^#deprecation_warnings = True/deprecation_warnings = False/' /etc/ansible/ansible.cfg
rm -f /usr/lib/python2.6/site-packages/ansible/modules/extras/cloud/misc/rhevm.py
3、ansible-playbook 讀取事先寫好的playbook,可以理解為按一定條件組成的ansible任務集
Usage: ansible-playbook playbook.yml
4、ansible-galaxy 上傳下載Roles,Roles地址https://galaxy.ansible.com/
Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...
5、ansible-pull pull模式(ansible默認push模式),用於數量巨大的機器配置,以及沒有網絡連接的主機上運行ansible。
Usage: ansible-pull -U <repository> [options]
-U URL of the playbook repository
6、ansible-vault 用於配置文件加密
ansible-vault [create|decrypt|edit|encrypt|rekey|view] [--help] [options] vaultfile.yml
例:
ansible-vault encrypt a.yml 加密並設置解密密碼,加密后打開為亂碼
ansible-vault decrypt a.yml 解密
7、ansible-console 進入類似於shell的交互模式