zoukankan      html  css  js  c++  java
  • Ansible配置文件

    Ansible 配置文件

    概览

    控制Ansible行为的方式有很多,基本分为如下4种:

    1. 配置文件
    2. 命令行参数
    3. playbook关键词
    4. 变量

    比如主机清单位置,默认是/etc/ansible/hosts,也可以通过命令行参数- i更改,也可以通过修改配置文件更换默认主机清单位置;playbook里可以控制执行用户,在主机变量里也可以控制

    上面4种配置方法,优先级从低到高,简单来说就是后面定义的内容会覆盖前面的,配置文件的优先级是最低的,本篇文章重点说一说Ansible常用的一些配置项

    配置文件路径

    Ansible按照下面的顺序寻找配置文件,首先找到的被使用,不再进行后续查找(即被忽略)

    1. ANSIBLE_CONFIG 环境变量
    2. ansible.cfg 当前路径
    3. ~/.ansible.cfg 家目录
    4. /etc/ansible/ansible.cfg

    如果我们希望测试一份配置文件,而不想影响其它配置文件,可以像下面这样

    ANSIBLE_CONFIG=/tmp/ansible.cfg ansible all -m ping
    #这种情况变量 ANSIBLE_CONFIG ,仅仅对当前这一行命令生效
    

    配置文件注释

    分为两种情况

    1 注释书写在开头部分(顶格写或者前面是空格),使用 #;

    # cat /etc/ansible/ansible.cfg
    # config file for ansible -- https://ansible.com/
    # ===============================================
    
    # nearly all parameters can be overridden in ansible-playbook
    ...
    

    2 注释在中间,只能使用; (不太常用)

    cat /etc/ansible/ansible.cfg
    ...
    inventory = /etc/ansible/hosts  ; This points to the file that lists your hosts
    ...
    

    配置的基本格式

    如果是全新安装,可以参考cat /etc/ansible/ansible.cfg;该配置文件的线上地址是https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg,其基本格式如下

    # 注释
    # 注释
    # 下面这个 [defaults] 是分组名称
    [defaults]
    inventory       = /etc/ansible/hosts
    #library         = ~/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
    
    [inventory]
    # List of enabled inventory plugins and the order in which they are used.
    #enable_plugins = host_list, script, auto, yaml, ini, toml
    

    ansible-config

    ansible的配置项有很多,不同ansible版本配置项略有不同,绝大多数无需关注,不用更改。最方便的得知我们当前安装的ansible版本有哪些可用的配置项,是使用ansible-config命令

    • 查看帮助
    [root@192_168_31_106 ~]# ansible-config -h
    usage: ansible-config [-h] [--version] [-v] {list,dump,view} ...
    
    View ansible configuration.
    
    positional arguments:
      {list,dump,view}
        list            Print all config options
        dump            Dump configuration
        view            View configuration file
    
    optional arguments:
      --version         show program's version number, config file location,
                        configured module search path, module location, executable
                        location and exit
      -h, --help        show this help message and exit
      -v, --verbose     verbose mode (-vvv for more, -vvvv to enable connection
                        debugging)
    
    • 查看全部可用配置项
    ansible-config list
    

    比如我们想要更改默认主机清单的位置,可以搜索inventory关键词,查看帮助,示例如下
    ![在这里插入图片描述](C:UsersLIUPENGHUIPicturesBili ScreenShota1b730d482f0fcd92f2d64ff28f0e613.png)

    • 导出当前配置文件(如果需要进行必要的合并)
    ansible-config dump
    
    [root@192_168_31_106 ~]# ansible-config dump > /tmp/tmp.txt && fgrep '/etc/ansible' /tmp/tmp.txt
    DEFAULT_FORKS(/etc/ansible/ansible.cfg) = 20
    DEFAULT_HOST_LIST(default) = [u'/etc/ansible/hosts']
    DEFAULT_ROLES_PATH(default) = [u'/root/.ansible/roles', u'/usr/share/ansible/roles', u'/etc/ansible/roles']
    

    参考常用配置项

    全部配置项可以使用上文ansible-config命令得到,也可以直接到官方文档里查看(见文章最后的参考资料),下面介绍一下常用的配置项

    • defaults 分组下常用配置

    一个示例
    ![在这里插入图片描述](C:UsersLIUPENGHUIPicturesBili ScreenShot5330c1d700899ec07e4d33b047134bd1.png)

    #1 主机清单的位置,还可以设置成目录,此时目录里的文件名不关键,主要是文件里内容中括号的部分,作为机器分组的名称
    inventory = /etc/ansible/hosts
    比如说,我们可以有下面的目录结构
    # tree inventory
    ├── web
    ├── golang
    ├── griddns
    ├── group_vars
    │   ├── admin5_php-fpm_bx.yml
    │   ├── admin5_php-fpm_yf3.yml
    │   ├── all.yml
    │   ├── bt_ctorrent_bx.yml
    │   ├── bt_hwraid_bx.yml
    │   ├── ceph_bx.yml
    │   ├── ceph_dbl.yml
    ├── host_vars
    │   ├── 10.13.32.100.yml
    │   ├── 10.13.32.101.yml
    │   ├── 10.13.32.102.yml
    │   ├── 10.13.32.103.yml
    │   ├── 10.13.32.104.yml
    │   ├── 10.13.32.110.yml
    
    #2 并发执行的数量
    forks           = 5
    
    #3 目标机器的SSH端口
    remote_port    = 26387
    
    #4 角色查找路径
    roles_path    = /usr/local/myansible/roles
    
    #5 SSH连接超时时间
    timeout = 10
    

    参考资料

    https://docs.ansible.com/ansible/latest/reference_appendices/config.html
    https://docs.ansible.com/ansible/latest/cli/ansible-config.html
    https://docs.ansible.com/ansible/latest/reference_appendices/general_precedence.html
    
    很高兴各位朋友能欣赏作品,本文版权归作者和博客园共有,欢迎转载,请在文章页面明显位置给出原文出处,否则将追究法律责任。 原文链接: https://www.cnblogs.com/strugger-0316
  • 相关阅读:
    XAMPP重要文件目录及配置
    xmlhttp
    深入php内核,从底层c语言剖析php实现原理
    史上最全的MSSQL复习笔记
    LNMP状态管理命令
    SSL证书更换(具体路径可参考iRedMail.tips文件)及邮件服务器架构
    (转)CentOS 7 —— /etc/rc.local 开机不执行
    从CMDB查询云平台组件或者IP简单脚本
    将电脑文件复制到vm虚拟机中,然后安装步骤
    Linux-vmware tools安装与cdrom挂载(转)
  • 原文地址:https://www.cnblogs.com/strugger-0316/p/14626080.html
Copyright © 2011-2022 走看看