003.Ansible配置文件管理


一 配置文件的優先級

ansible的配置文件名為ansible.cfg,它一般會存在於四個地方:

  1. ANSIBLE_CONFIG:首先,Ansible命令會檢查該環境變量,及這個環境變量將指向的配置文件
  2. ./ansible.cfg:當前工作目錄,即當前執行ansible指令的目錄,如果ANSIBEL_CONFIG環境變量未定義,則優先使用該配置文件
  3. ~/.ansible.cfg:當前用戶家目錄下的一個隱藏文件,如果當前工作目錄下不存在ansible.cfg配置文件,則會查找用戶家目錄下的該隱藏文件
  4. /etc/ansible/ansible.cfg:默認配置文件,如果上面兩個路徑下的ansible.cfg都不存在,則使用該文件

配置文件中所有的配置項都可以通過環境變量的方式來定義,而環境變量定義的配置項具有最高優先級,會覆蓋掉所有配置文件中的配置項

[root@node1 ansible]# cp ansible.cfg /root/
[root@node1 ansible]# vim /root/ansible.cfg

#修改remote用戶為root
remote_user = root

進入root目錄下

[root@node1 ansible]# cd

[root@node1 ~]# ansible -i /etc/ansible/inventory web -m shell -a "whoami"

進入/etc/ansible目錄下

插入一個環境變量

[root@node1 ansible]# export ANSIBLE_CONFIG=/aaa/bbb/ansible.cfg
[root@node1 ansible]# ansible -i /etc/ansible/inventory web -m shell -a "whoami"

當變量不存在,使用用戶的家目錄藏文件

[root@node1 ansible]# cd /opt/

[root@node1 opt]# mv /root/ansible.cfg /root/.ansible.cfg

[root@node1 opt]# ansible -i /etc/ansible/inventory web -m shell -a "whoami"

使用當前目錄的ansible.cfg

[root@node1 ansible]# mkdir -p /aaa/bbb

[root@node1 ansible]# cp /root/ansible.cfg $ANSIBLE_CONFIG

[root@node1 ansible]# ansible -i /etc/ansible/inventory web -m shell -a "whoami"

優先級順序:環境變量---->家目錄隱藏文件------>當前目錄下文件------>默認位置文件

二 配置文件分段說明

ansible.cfg的配置默認分為十段:

  • [defaults]:通用配置項
  • [inventory]:與主機清單相關的配置項
  • [privilege_escalation]:特權升級相關的配置項
  • [paramiko_connection]:使用paramiko連接的相關配置項,Paramiko在RHEL6以及更早的版本中默認使用的ssh連接方式
  • [ssh_connection]:使用OpenSSH連接的相關配置項,OpenSSH是Ansible在RHEL6之后默認使用的ssh連接方式
  • [persistent_connection]:持久連接的配置項
  • [accelerate]:加速模式配置項
  • [selinux]:selinux相關的配置項
  • [colors]:ansible命令輸出的顏色相關的配置項
  • [diff]:定義是否在運行時打印diff(變更前與變更后的差異)

三 配置參數說明

[default]
inventory      = /etc/ansible/hosts
remote_user    = root
ask_pass       = false
log_path       = /var/log/ansible.log

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

[ssh_connection]
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s 
host_key_checking = False 

配置項說明:

  • inventory:定義默認使用的主機清單

  • remote_user: ansible在操作遠程主機時,使用遠程主機上的哪個用戶身份,默認是root

  • ask_pass:ansible在操作遠程主機時,獲取遠程主機上的用戶身份,是否交互提示密碼驗證,默認為true。如果使用密鑰認證的話,建議將其設置為false

  • log_path:默認ansible 執行的時候,並不會輸出日志到文件,打開該配置項,所有的命令執行后,都會將日志輸出到/var/log/ansible.log文件。

  • become:如果ansible在操作遠程主機時,使用的是遠程主機上的普通用戶,該普通用戶是否允許提權

  • become_method:如果允許提權,使用何種提權方式,默認是sudo

  • become_user:提權到哪個用戶身份,默認是root

  • become_ask_pass:提權時,是否交互提示密碼驗證,默認為False

  • ssh_args:ansible通過ssh連接遠程被管理機,這里用於定義一些ssh連接時的參數,如-C啟用壓縮傳輸,ControlPersist用於提升性能。

  • host_key_checking:通過ssh首次連接遠程主機時,由於在本機的~/.ssh/known_hosts文件中並有fingerprint key串,ssh第一次連接的時候一般會提示輸入yes/no進行確認將key字符串加入到~/.ssh/known_hosts文件中。將此項設置為False將跳過該確認過程。

四 關於ssh連接一些常見的錯誤說明

ERROR! to use the 'ssh' connection type with passwords, you must install the sshpass program

完整錯誤示例如下:

root@ctnr:/etc/ansible# ansible '*.a32-168-1.*' -m ping
ctnr.a32-168-1.prod.yiz | FAILED! => {
    "failed": true, 
    "msg": "ERROR! to use the 'ssh' connection type with passwords, you must install the sshpass program"
}

一般出現這種錯誤,是在通過密碼驗證遠程被管理機的時候,需要在server端安裝sshpass:

yum install sshpass -y 

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

完整錯誤如下:

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.

這種錯誤通常就出現在server端第一次連接被管理機的時候,就是上面說到的需要通過輸入yes/no進行確認將key字符串加入到~/.ssh/known_hosts文件中。

解決辦法有兩個:

  • 通過修改上面提到的host_key_cheking,將其設置為false(在實際測試中,似乎並沒有效果)
  • 通過修改ssh_args參數,修改如下:
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no 

指定hosts文件

inventory      = /etc/ansible/inventory

查看

[root@node1 ansible]# cat /etc/ansible/inventory 

[web]
node1 
node2
[mysql]  
node3 
node4
[redis]
node5
[php]
node1
node3

執行

[root@node1 ansible]# ansible  all  -m shell -a "whoami"

一共五個主機,后面就是用ansible對着五個主機進行操作


 博主聲明:本文的內容來源主要來自譽天教育晏威老師,由本人實驗完成操作驗證,需要的博友請聯系譽天教育(http://www.yutianedu.com/),獲得官方同意或者晏老師(https://www.cnblogs.com/breezey/)本人同意即可轉載,謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM