ansible hosts文件写法
目录
1 ansible hosts文件写法
[linux:children] # 指定子组
linux6
linux7
[linux6]
192.168.0.10
192.168.0.11
192.168.0.[12:20] #192.168.0.12——192.168.0.30
[linux7]
192.168.0.87 ansible_ssh_user="root" ansible_ssh_pass="qwe123" #单独配置登陆用户及密码
192.168.0.88 ansible_ssh_user="root" ansible_ssh_pass="qwe123"
[linux:vars] #统一配置该组的登陆用户及密码
ansible_ssh_user="root"
ansible_ssh_pass="qwe123"
[mweb]
cld1.web.ser.com ansible_connection=ssh ansible_host=10.19.51.212 ansible_ssh_private_key_file=/home/user1/.ssh/id_rsa ansible_ssh_port=1022 # 指定hostip和 private_key_file ssh_port
cld3.web.ser..com ansible_connection=ssh ansible_host=10.19.17.227 ansible_ssh_private_key_file=/home/user1/.ssh/id_rsa
[windows] #Windows主机配置管理 #Windows要求安装powershell,开启WinRM服务
192.168.0.84 ansible_ssh_user="administrator" ansible_ssh_pass="qwe123" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_transport="ntlm" ansible_winrm_server_cert_validation="ignore"
2 调用
1、调用两个主机组的写法,以下webservers和dbservers都会被调用:
ansible webservers:dbservers -m win_ping
2、在webservers组中但不在dbsersers中的调用:
ansible webservers:!dbservers -m win_ping
3、在webservers组中并且在dbservers组中的才会调用:
ansible webservers:&dbservers -m win_ping
4、在调用前加~,代表正则表达式:
ansible ~(web|db).*.91it.org -m win_ping
5、组合的例子:
webserver:dbservers:&nginx:!ntp
10、hosts文件中没有定义的IP或别名,在进行调用中,会提示错误。ansible对单台服务器的调用,服务器IP或域名必须有写在hosts里。