/etc/ansible/ansible.cfg 主配置文件 ansible的配置文件
/etc/ansible/hosts Inventory 要遠程控制的主機列表
/usr/bin/ansible-doc 幫助文件
/usr/bin/ansible-playbook 指定運行任務文件
默認: /etc/ansible/hosts
inventory file可以有多個,且也可以通過Dynamic Inventory來動態生成。
參考解釋例子ansible_ssh_host將要連接的遠程主機名.與妳想要設定的主機的別名不同的話,可通過此變量設置.ansible_ssh_host=192.169.1.123ansible_ssh_portssh端口號.如果不是默認的端口號,通過此變量設置.ansible_ssh_port=5000ansible_ssh_user默認的 ssh 用戶名ansible_ssh_user=cxpadminansible_ssh_passssh 密碼(這種方式並不安全,我們強烈建議使用 --ask-pass 或 SSH 密鑰)ansible_ssh_pass=’123456’
ansible2.0,ansible_ssh_user, ansible_ssh_host, ansible_ssh_port已經改變為ansible_user, ansible_host, ansible_port。具體參考官網
/ansible/latest/intro_inventory.html
常見的模塊及使用參考: ansible基本使用教程 - 陳小跑 - 博客園
命令行方式壹次只能執行單條命令,如果命令量較多,可以使用playbook的方式。 /a/1190000038230424
playbook使用yaml格式編寫。組成結構如下:
舉例如下:
使用參考: /a/1190000038230424
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
通常情況下,通過&指定的後臺任務在終端退出後會自動退出執行。壹般來說,加上nohup即可在後臺壹直執行。但在使用ansible時,發現 ansible all -m shell -a 'nohup cmd &' 命令無法在後臺壹直執行。後來想了個辦法,將命令寫入腳本,然後通過執行腳本來實現。
腳本內容為
ansible命令為 ansible all -m script -a 'bash xxx.sh' 。sleep的原因是退出太快可能腳本沒跑起來就退出了。