zoukankan      html  css  js  c++  java
  • Ansible--inventory

    简介

    Inventory 是 Ansible 管理主机信息的配置文件,相当于系统 HOSTS 文件的功能,默认存放在 /etc/ansible/hosts。
    为方便批量管理主机,便捷使用其中的主机分组,Ansible 通过 Inventory 来定义其主机和组,在使用时通过一1 或 --inventory-file 指定读取
    如果只有一个 Inventory 时可不用指定路径,默认读取 /etc/ansible/hosts。 Inventory 可以同时存在多个,而且支持动态生成
    与 Ansible 命令结合使用时组合如下:

    [root@ansible ~]# ansible -i /etc/ansible/hosts webs -m ping
    

    定义主机和组

    Inventory配置文件遵循的是INI文件风格,中括号表示组名,其支持将同一个主机加入到不同的组中,此外若主机没有使用默认的SSH的22端口,还可以在主机名字或者IP后面加上冒号来指定,#号为注释行

    示例(编辑/etc/ansible/hosts文件):

    # 直接跟主机IP及其他端口
    192.168.192.129
    192.168.192.129:2333
    
    # 使用主机hostname及其他端口
    ansible-node1
    ansible-node1:2333
    
    # 主机分组
    [webserver]
    192.168.192.129
    ansible-node1:2333
    
    # 连续主机
    [dbserver]
    192.168.192.[100:200]    # 表示192.168.192.100--192.168.192.200之间的所有主机
    ansible-node[10:20]:2333 # 表示ansible-node10:2333--ansible-node20:2333之间的所有主机
    

    定义主机变量

    在工作中,通常会遇到非标准化的需求配置,考虑安全的问题,通常会把企业内部的80端口修改为其他的端口,这个就可以在Inventory中定义,然后在后续的playbook使用
    示例(编辑/etc/ansible/hosts文件):

    [dbserver]
    # 自定义http_port的端口为80,配置maxRequestsPerChild(最大请求数)为801
    192.168.192.129 http_port=808 maxRequestsPerChild=801
    
    # 自定义http_port的端口为303,配置maxRequestsPerChild(最大请求数)为909
    ansible-node1 http_port=303 maxRequestsPerChild=909
    

    定义组变量

    Ansible支持定义组的变量,主要是针对大量的机器的变量定义需求,赋予指定组内所有主机在playbook中可用的变量,等同于逐一给该组下的所有主机赋予同一个变量
    示例(编辑/etc/ansible/hosts文件):

    [groupserver]
    192.168.192.129
    ansible-node1
    
    [groupserver:vars]
    # 定义groupserver组中所有主机ntp_server的值为ntp1.aliyun.com
    ntp_server=ntp1.aliyun.com 
    
    # 定义groupserver组中所有主机nfs_server的值为nfs.aliyun.com
    nfs_server=nfs.aliyun.com
    

    定义组嵌套和组变量

    Inventory中,组还可以包含其他的组(嵌套),并且也可以向组中的主机指定变量,不过这些变量只能在playbook中使用,在ansible中不支持,组与组之间可以相互调用,并且可以向组中的主机指定变量
    示例(编辑/etc/ansible/hosts文件):

    [apache]
    192.168.192.129
    
    [nginx]
    nginx1.brian.com
    
    # 将要嵌套的组名放到一个新的组中
    [webserver:children]
    apache
    nginx
    
    # 给新的组定义ntp_server的变量给主机使用
    [webserver:vars]
    ntp_server=ntp1.aliyun.com
    

    定义多重变量

    变量除了可以在Inventory中一并定义,也可以独立于Inventory文件之外单独存储到YAML格式的配置文件中,这些文件通常以 .yml、.yam!、.json为后缀或者无后缀。
    变量通常从如下4个位置检索:

    1. Inventory配置文件(默认/etc/ansible/hosts)
    2. Playbook中vars定义的区域
    3. Roles中 vars目录下的文件
    4. Roles同级目录group_vars和hosts_vars目录下的文件

    假如foosball主机同属于raleigh和webservers组, 那么其变量在如下文件中设置均有效
    示例:
    假设你有一些主机,属于不同的数据中心,并依次进行划分.每一个数据中心使用一些不同的服务器比如ntp服务器,database服务器等等.
    那么'raleigh'这个组的组变量定义在文件'/etc/ansible/group_vars/raleigh'之中,可能类似这样

    ---
    ntp_server: acme.example.org
    database_server: storage.example.org
    

    Inventory其他参数

    通过设置下面的参数,可以控制 ansible 与远程主机的交互方式

    ansible_ssh_host                # 将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
    ansible_ssh_port                # ssh端口号.如果不是默认的端口号,通过此变量设置.
    ansible_ssh_user                # 默认的 ssh 用户名
    ansible_ssh_pass                # ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
    ansible_sudo_pass               # sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
    ansible_ssh_exe                 # sudo 命令路径(适用于1.8及以上版本)
    ansible_connection              # 与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
    ansible_ssh_private_key_file    # ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况
    ansible_shell_type              # 目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'.
    ansible_python_interpreter      #  目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python" 
    
    与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径
    
    
    示例(编辑/etc/ansible/hosts文件):
    [testserver]
    some_host         ansible_ssh_port=2222     ansible_ssh_user=manager 
    aws_host          ansible_ssh_private_key_file=/home/example/.ssh/aws.pem
    freebsd_host      ansible_python_interpreter=/usr/local/bin/python
    ruby_module_host  ansible_ruby_interpreter=/usr/bin/ruby.1.9.3
    

    Ansible正则

    语法:ansible <pattern_goes_here> -m <module_name> -a <arguments>
    示例:
    1) all(全量)匹配
    # all和*相同,但*号要引起来
    ansible all -m ping
    ansible '*' -m ping
    
    2)逻辑或(or)匹配
    # 如果要同时对多台主机或者多个组同时执行,使用冒号分隔
    ansible 'db:web' -m ping
    
    3)逻辑非(!)匹配
    # 所有在web组但不在db组中的主机
    ansible 'web:!db' -m ping
    
    4)逻辑与(&)匹配
    # web组和db组同时存在的主机
    ansible 'web:&db' -m ping
    
    5)多条件组合
    # web和db两个组中的所有主机在python组中存在且不在php组中的主机
    ansible 'web:db:&python:!php' -m ping
    
    6)模糊匹配
    # 所有以.python结尾的主机
    ansible '*.python' -m ping
    # 以brian开头.python结尾的所有主机和db组中的所有主机
    ansible 'brian*.python:db' -m ping
    
    7)域切割(python中的列表切割)
    主机清单:
    [web]
    web1
    web2
    web3
    
    使用:
    ansible 'web[0]' -m ping 等于 ansible web1 -m ping
    ansible 'web[-1]' -m ping 等于 ansible web3 -m ping
    ansible 'web[0:1]' -m ping 等于 ansible web1,web2 -m ping
    ansible 'web[1:]' -m ping 等于 ansible web2,web3 -m ping
    
    
    8)正则匹配
    ansible 支持完整的正则匹配
    ansible '~(web|db).*.python.com' -m ping
    
    #检测beta.python.com 、web.python.com 、green.python.com 、befa.python.cn 、web.python.cn、green.python.cn的存活
    ansible '~(beta|web|green).python.(com|cn)' -m ping
    
    #检测Inventory 中所有以192.168开头的服务器存活信息
    ansible '~192.168.[0-9]{2}.[0-9]{2,}' -m ping
    
  • 相关阅读:
    wpf写的httpget请求
    c#的反射机制
    C#中所有的类都继承自哪个类
    使用c#创建windows的桌面的自启服务
    工厂模式的三种实现,就这么简单!
    Github上的沙雕项目,玩100遍都不够
    代理模式的种类、原理及各种实例详解
    面试官问:HashMap在并发情况下为什么造成死循环?一脸懵
    Oracle中 COUNT(count(*))语法
    jquery如何获取当前时间
  • 原文地址:https://www.cnblogs.com/brianzhu/p/10188676.html
Copyright © 2011-2022 走看看