首页 > 其他 > 详细

Ansible hosts文件配置格式

时间:2020-10-03 11:10:30      阅读:1381      评论:0      收藏:0      [点我收藏+]

Ansible hosts文件配置IP的方式

 

第一种 IP+A/C

示例如下:
192.168.0.10 ansible_ssh_user=“vagrant” ansible_ssh_pass=“vagrant”

第二种 nickname+IP+A/C

testserver ansible_ssh_host=192.168.0.10 ansible_ssh_user=“vagrant” ansible_ssh_pass=“vagrant”

第三种 nickname+IP+Account+节点私有key

testserver ansible_ssh_host=192.168.0.10 ansible_ssh_user=“vagrant” ansible_private_key_file=./vagrant/machines/default/virtual/private_key

第四种 IP

192.168.0.10
注:这种方式需要先用ssh-keygen命令(回车三次)生成管理主机的public key
然后把key用命令ssh-copy-id -i 发送到节点主机上去才能起作用。
示例:
ssh-copy-id -i “/home/vagrant/.ssh/id_rsa.pub” vagrant@192.168.0.10

 

 

1、正常写法,name1为别名:
[test1]
name1 ansible_ssh_host=192.168.1.111 ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22


name2 ansible_ssh_host=192.168.1.222  ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22

 

2、连续的IP写法,表示192.168.1.20到192.168.1.50,共31台主机:
[test1]


name1 ansible_ssh_host=192.168.1.[20:50] ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22

 

3、带参数的群组,vars底下为群组共同便变量,包括已定义变量和自定义变量:
[test1]
name1 ansible_ssh_host=192.168.1.[20:50]

[test1:vars]
ansible_ssh_user=root
ansible_ssh_pass="1234"
testvar="test"

 

4、群组整合,children底下为父群组test的子群组,调用方式为ansible test -m ping:


[dbtest]
name1 ansible_ssh_host=192.168.1.[20:50] ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22
[webtest]


name2 ansible_ssh_host=192.168.2.[20:50] ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22
[test:children]
dbtest
webtest

 

5、调用两个主机组的写法,以下webservers和dbservers都会被调用:


ansible webservers:dbservers -m ping

 

6、在webservers组中但不在dbsersers中的调用:


ansible webservers:!dbservers -m win_ping

 

7、在webservers组中并且在dbservers组中的才会调用:
ansible webservers:&dbservers -m ping

 

8、在调用前加~,代表正则表达式:


ansible ~(web|db).*.91it.org -m win_ping

 

9、组合的例子:
webserver:dbservers:&nginx:!ntp

 

10、hosts文件中没有定义的IP或别名,在进行调用中,会提示错误。ansible对单台服务器的调用,服务器IP或域名必须有写在hosts里

 

11、ansible配置文件
在配置文件里面配置用户和端口,就可以不要在host里面配置指定里
ansible 有许多参数,下面我们列出一些常见的参数:

inventory = /etc/ansible/hosts #这个参数表示资源清单inventory文件的位置


library = /usr/share/ansible #指向存放Ansible模块的目录,支持多个目录方式,只要用冒号(:)隔开就可以


forks = 5 #并发连接数,默认为5


sudo_user = root #设置默认执行命令的用户


remote_port = 22 #指定连接被管节点的管理端口,默认为22端口,建议修改,能够更加安全


host_key_checking = False #设置是否检查SSH主机的密钥,值为True/False。关闭后第一次连接不会提示配置实例


timeout = 60 #设置SSH连接的超时时间,单位为秒


log_path = /var/log/ansible.log #指定一个存储ansible日志的文件(默认不记录日志)

 

Ansible hosts文件配置格式

原文:https://www.cnblogs.com/chengxuyonghu/p/13763561.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!