且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ansible.cfg文件及host文件配置

更新时间:2022-09-26 15:50:22

ansible.cfg文件及host文件配置


inventory:资源清单文件的存放,资源清单就是一些主机的列表。可以指向一个文件也可以指向一个目录。

默认为:

1
inventory      = /etc/ansible/hosts


library:ansible的操作动作,指定ansible模块的目录。

默认为:

1
library        = /usr/share/my_modules/



forks:设置ansible最多能有多少个进程同时工作。

默认为:

1
forks          = 5



sudo_user:默认执行命令的用户,可以在playbook中重新设置这个参数。

默认为:

1
#sudo_user      = root



remote_port:远程被管节点的管理关口,默认为22.


host_key_checking:设置是否检查ssh主机的秘钥。

1
默认为:host_key_checking = False



timeout:设置ssh连接超时的时间间隔。

默认为:

1
timeout = 10

log_path:指定ansible执行过程存储日志的文件。

1
log_path = /var/log/ansible.log


host文件的配置:


可以这样写:

1
2
3
[test]
192.168.121.128
192.168.121.129



也可以这样写:

1
2
[test]
192.168.121.[128:129]


1
2
3
# ansible test -m 'ping ' -o
192.168.121.128 | SUCCESS => {"changed"false"ping""pong"}
192.168.121.129 | SUCCESS => {"changed"false"ping""pong"}



要是host主机分组比较多,都属于同一个大组,不同的小组,可以这样写:


1
2
3
4
5
6
7
[test:children]
test1
test2
[test1]
192.168.121.128
[test2]
192.168.121.129



1
2
3
4
5
6
7
[root@192 ansible]# ansible test -m 'ping ' -o
192.168.121.128 | SUCCESS => {"changed"false"ping""pong"}
192.168.121.129 | SUCCESS => {"changed"false"ping""pong"}
[root@192 ansible]# ansible test1 -m 'ping ' -o
192.168.121.128 | SUCCESS => {"changed"false"ping""pong"}
[root@192 ansible]# ansible test2 -m 'ping ' -o
192.168.121.129 | SUCCESS => {"changed"false"ping""pong"}

当然也可以查看这个组下的主机:

[

1
2
3
4
5
6
7
root@192 ansible]# ansible test1 --list-hosts
  hosts (1):
    192.168.121.128
[root@192 ansible]# ansible test --list-hosts
  hosts (2):
    192.168.121.128
    192.168.121.129



本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1886319