Ansible默認安裝好後有一個配置文件/etc/ansible/ansible.cfg,該配置文件中定義了ansible的主機的默認配置部分,如默認是否需要輸入密碼、是否開啓sudo認證、action_plugins插件的位置、hosts主機組的位置、是否開啓log功能、默認端口、key文件位置等等。
具體如下:
[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 test -a 'uptime'
192.168.1.1| FAILED =>Using a SSH password instead of a key is not possible because HostKey checking is enabled and sshpass does not support this.Please add this host's fingerprint to your known_hosts file to manage this host.
192.168.1.2 | FAILED => Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host.
是由於在本機的~/.ssh/known_hosts文件中並有fingerprint key串,ssh第一次連接的時候一般會提示輸入yes 進行確認爲將key字符串加入到 ~/.ssh/known_hosts 文件中。
方法1:
在進行ssh連接時,可以使用-o參數將StrictHostKeyChecking設置爲no,使用ssh連接時避免首次連接時讓輸入yes/no部分的提示。通過查看ansible.cfg配置文件,發現如下行:
[ssh_connection]
# ssh arguments to use
# Leaving off ControlPersist will result in poor performance, so use
# paramiko on older platforms rather than removing it
#ssh_args = -o ControlMaster=auto -o ControlPersist=60s
可以啓用ssh_args 部分,使用下面的配置,避免上面出現的錯誤:
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
方法2:
在ansible.cfg配置文件中,也會找到如下配置:
# uncomment this to disable SSH key host checking
host_key_checking = False
默認host_key_checking部分是註釋的,通過找開該行的註釋,同樣也可以實現跳過ssh 首次連接提示驗證部分。但在實際測試中,似乎並沒有效果,建議使用方法1.
其他部分
默認ansible 執行的時候,並不會輸出日誌到文件,不過在ansible.cfg 配置文件中有如下行:
log_path = /var/log/ansible.log
默認log_path這行是註釋的,打開該行的註釋,所有的命令執行後,都會將日誌輸出到/var/log/ansible.log文件。
本文出自 “無名小卒” 博客,請務必保留此出處http://breezey.blog.51cto.com/2400275/1757635