Ansible小结(三)ansible.cfg与默认配置

Posted by Yancy on 2016-03-07

#Ansible小结(四)ansible.cfg与默认配置

Ansible默认安装好后有一个配置文件/etc/ansible/ansible.cfg,该配置文件中定义了ansible的主机的默认配置部分,如默认是否需要输入密码、是否开启sudo认证、action_plugins插件的位置、hosts主机组的位置、是否开启log功能、默认端口、key文件位置等等。

具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[defaults]
# some basic default values...
hostfile = /etc/ansible/hosts \\指定默认hosts配置的位置
# library_path = /usr/share/my_modules/
remote_tmp = $HOME/.ansible/tmp
pattern = *
forks = 5
poll_interval = 15
sudo_user = root \\远程sudo用户
#ask_sudo_pass = True \\每次执行ansible命令是否询问ssh密码
#ask_pass = True \\每次执行ansible命令时是否询问sudo密码
transport = smart
remote_port = 22
module_lang = C
gathering = implicit
host_key_checking = False \\关闭第一次使用ansible连接客户端是输入命令提示
log_path = /var/log/ansible.log \\需要时可以自行添加。chown -R root:root ansible.log
system_warnings = False \\关闭运行ansible时系统的提示信息,一般为提示升级
# set plugin path directories here, separate with colons
action_plugins = /usr/share/ansible_plugins/action_plugins
callback_plugins = /usr/share/ansible_plugins/callback_plugins
connection_plugins = /usr/share/ansible_plugins/connection_plugins
lookup_plugins = /usr/share/ansible_plugins/lookup_plugins
vars_plugins = /usr/share/ansible_plugins/vars_plugins
filter_plugins = /usr/share/ansible_plugins/filter_plugins
fact_caching = memory
[accelerate]
accelerate_port = 5099
accelerate_timeout = 30
accelerate_connect_timeout = 5.0
# The daemon timeout is measured in minutes. This time is measured
# from the last activity to the accelerate daemon.
accelerate_daemon_timeout = 30

在ansible.cfg配置文件中,也会找到如下部分:

1
2
# uncomment this to disable SSH key host checking
host_key_checking = False

默认host_key_checking部分是注释的,通过找开该行的注释,同样也可以实现跳过 ssh 首次连接提示验证部分。

我这里做了免秘钥的查看日志如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@docker ~]# ansible tomcat_C1 -a "uptime"
192.168.1.177 | SUCCESS | rc=0 >>
09:28:03 up 24 days, 15:39, 4 users, load average: 0.00, 0.00, 0.00
[root@docker ~]#
[root@docker ~]#
[root@docker ~]# cat /var/log/ansible.log
2016-09-05 09:19:23,235 p=1412 u=root | ERROR! Missing target hosts
2016-09-05 09:19:54,580 p=1417 u=root | 192.168.1.177 | SUCCESS | rc=0 >>
09:20:24 up 24 days, 15:31, 4 users, load average: 0.00, 0.00, 0.00
2016-09-05 09:20:07,206 p=1440 u=root | 192.168.1.177 | SUCCESS | rc=0 >>
09:20:37 up 24 days, 15:31, 4 users, load average: 0.00, 0.00, 0.00
2016-09-05 09:27:31,607 p=1460 u=root | 192.168.1.177 | SUCCESS | rc=0 >>
09:28:01 up 24 days, 15:39, 4 users, load average: 0.00, 0.00, 0.00
2016-09-05 09:27:33,800 p=1482 u=root | 192.168.1.177 | SUCCESS | rc=0 >>
09:28:03 up 24 days, 15:39, 4 users, load average: 0.00, 0.00, 0.00

更多部分可以参看官方文档