自動化運維之Ansible基礎元素介紹
阿新 • • 發佈:2018-10-23
shell table inter 匹配 p地址 元素 不同的 lin 直接 Inventory(主機清單)
Ansible為了更加便捷的管理主機,在主機清單中將被管理主機進行分組命名,默認的主機清單為/etc/ansible/hosts文件。主機清單可以設置為多個,也可以通過Dynamic Inventory動態生成。
Inventory文件以中括號中的字符標識為組名,將主機分組管理,也可以將同一主機同時劃分到多個不同的組中。如果被管理主機使用非默認的SSH端口,還可以在主機名之後用冒號加端口號的方式來進行標明。
例如:
[node1] //方括號設置組名 www1.example.org //定義被監控主機,這邊可以是主機名也可以是IP地址 www2.example.org:2222 冒號後定義遠程連接端口,默認是ssh的22端口
如果是名稱類似的主機,可以使用列表的方式標識各個主機
[node2]
www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123456
//01-50臺主機 ,ssh登陸的用戶,密碼
[node3]
db-[a:f].example.org //支持匹配a b c ... f
Inventory中變量
1.主機變量
可以在定義主機時添加主機變量,以便在後續的playbook中使用,如:
[webserver] www1.magedu.com http_port=80 maxRequestsChild=808 //定義主機名,端口80,進程號808 www2.magedu.com http_port=8080 maxRequestsChild=909
2.組變量
組變量是指給指定主機設置可以在Playbook中直接使用的變量,如:
http1.example.org
http2.example.org
3.組嵌套
在Inventory中的組還可以嵌套其他的組,也可以向組中的主機指定變量。不過這些變量只能在ansible-playbook工具中使用,直接使用Ansible工具並不支持,如:
[apache]
http1.example.org
http2.example.org
[nginx]
ngx1.example.org
ngx2.example.org
[webservers:children]
apache
nginx
Inventory變量參數
參數 | 含義 |
---|---|
ansible_ssh_host | 將要連接的遠程主機名.與你想要設定的主機的別名不同的話,可通過此變量設置. |
ansible_ssh_port | ssh端口號.如果不是默認的端口號,通過此變量設置 |
ansible_ssh_user | 默認的 ssh 用戶名 |
ansible_ssh_pass | ssh 密碼(這種方式並不安全,我們強烈建議使用 --ask-pass 或 SSH 密鑰) |
ansible_ssh_private_key_file | ssh 使用的私鑰文件.適用於有多個密鑰,而你不想使用 SSH 代理的情況. |
ansible_ssh_common_args | 此設置附加到sftp,scp和ssh的缺省命令行 |
ansible_sftp_extra_args | 此設置附加到默認sftp命令行 |
ansible_scp_extra_args | 此設置附加到默認scp命令行 |
ansible_ssh_extra_args | 此設置附加到默認ssh命令行 |
ansible_ssh_pipelining | 確定是否使用SSH管道。 這可以覆蓋ansible.cfg中得設置 |
ansible_shell_type | 目標系統的shell類型.默認情況下,命令的執行使用 ‘sh‘ 語法,可設置為 ‘csh‘ 或 ‘fish‘ |
ansible_python_interpreter | 目標主機的 python 路徑.適用於的情況: 系統中有多個 Python, 或者命令路徑不是"/usr/bin/python",比如 *BSD, 或者 /usr/bin/python |
ansible_*_interpreter | 這裏的"*"可以是ruby 或perl 或其他語言的解釋器,作用和ansible_python_interpreter 類似 |
ansible_shell_executable | 這將設置ansible控制器將在目標機器上使用的shell,覆蓋ansible.cfg中的配置,默認為/bin/sh |
自動化運維之Ansible基礎元素介紹