• 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/)本人同意即可转载,谢谢!

  • 相关阅读:
    Python使用Django创建第一个项目
    如何使用RobotFramework编写好的测试用例
    接口测试 总结(什么是接口测试)
    APP接口测试
    Django基础五之中间件
    Django基础五之django模型层(二)多表操作
    Django基础五之django模型层(一)单表操作
    Django基础四之模板系统
    Django基础三之视图函数
    DJANGO2--url路由
  • 原文地址:https://www.cnblogs.com/zyxnhr/p/12794356.html
Copyright © 2020-2023  润新知