• ansible Invetory(管理主机信息)


    1. 定义组机和组

    inventory文件可以是许多格式之一,具体取决于您拥有的inventory插件。 对于这个例子, /etc/ansible/hosts的格式是一个INI(类似于Ansible的默认设置)

    ##ip,或者域名
    192.168.1.1
    www.magedu.com
    
    ##组
    [webservers]
    foo.example.com
    bar.example.com
    
    [dbservers]
    one.example.com
    two.example.com
    three.example.com
    
    
    ##加上端口
    www.magedu.com:2020
    
    ##很多主机遵循类似的模式,你可以这样做,而不是列出每个主机名
    [webservers]
    www[01:50].example.com
    
    [databases]
    db-[a:f].example.com
    
    ##还可以在每个主机的基础上选择连接类型和用户:
    
    [targets]
    
    localhost              ansible_connection=local
    other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
    other2.example.com     ansible_connection=ssh        ansible_user=mdehaan

    2. 主机变量

    如上所述,很容易将变量分配给稍后将在playbook中使用的主机:

    [atlanta]
    host1 http_port=80 maxRequestsPerChild=808
    host2 http_port=303 maxRequestsPerChild=909

     

    3. 组变量

    变量也可以一次性应用于整个组:[atlanta]
    host1
    host2
    
    [atlanta:vars]
    ntp_server=ntp.atlanta.example.com
    proxy=proxy.atlanta.example.com

    请注意,这只是将变量一次应用于多个主机的便利方式。 即使您可以按组来定位主机,在执行play之前,变量总是被平坦化为主机级别。

    4. 组嵌套和组变量

    也可以使用:children后缀来创建组组。 如上所述,您可以使用:vars应用:vars :

    [atlanta]
    host1
    host2
    
    [raleigh]
    host2
    host3
    
    [southeast:children]  #southest 由atlanta和raleigh两个组组成了
    atlanta
    raleigh
    
    [southeast:vars]
    some_server=foo.southeast.example.com
    halon_system_timeout=30
    self_destruct_countdown=60
    escape_pods=2
    
    [usa:children]
    southeast
    northeast
    southwest
    northwest
    如果需要存储列表或散列数据,或者希望将主机和组的特定变量与Inventory文件分开,请参阅下一节。 子组有几个属性要注意:

    • 首先,作为子组成员的任何主机自动是父组的成员

      

    • 第二,子组的变量将具有较高的优先级(覆盖)父组的变量。

    5. 默认组

    有两个默认组: allungrouped 。 all包含每个主机。 ungrouped包含除所有主机外没有其他组的所有主机。

     

    6. 多重变量定义

    Ansible中的首选实践实际上不是在主Inventory文件中存储变量。

    除了将变量直接存储在INI文件中,主机和组变量可以存储在相对于Inventory文件的单个文件中。

    这些变量文件采用YAML格式。 有效的文件扩展名包括'.yml','.yaml','.json'或没有文件扩展名。 如果您是YAML的新手,请参阅YAML语法 。

    假设库存文件路径是:

    /etc/ansible/hosts

    如果主机被命名为“foosball”,并且在“raleigh”和“webservers”组中,以下位置的YAML文件中的变量将提供给主机:

    /etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
    /etc/ansible/group_vars/webservers
    /etc/ansible/host_vars/foosball


    对于变量的读取,ansible遵循以上优先级顺序,因此设置变量尽量沿用一种方式方便管理.


    作为一个高级用例,您可以创建以组或主机命名的目录 ,而Ansible将读取这些目录中的所有文件。 “raleigh”组的一个例子:

    /etc/ansible/group_vars/raleigh/db_settings
    /etc/ansible/group_vars/raleigh/cluster_settings


    “raleigh”组中的所有主机都将具有这些文件中定义的变量。 当单个文件开始太大时,或者当您想在组变量的一部分使用可安全保管库时,这可能非常有用。 请注意,这只适用于Ansible 1.4或更高版本。



    7. 其他inventory参数列表

    ssh连接

    设置以下变量控制如何可以与远程主机进行交互。

    ansible_connection
    连接类型到主机。 这可以是任何ansible的连接插件的名称。 SSH协议类型是smart , sshparamiko 。 默认是smart。 下一节将介绍基于非SSH的类型。

    ansible_host

    要连接到的主机的名称,如果与要提供的别名不同。
    ansible_port
    ssh端口号,如果不是22
    ansible_user
    要使用的默认ssh用户名。

    特定于SSH连接:

    ansible_ssh_pass
    要使用的ssh密码(不要以纯文本形式存储此变量;请始终使用保管库。请参阅变量和保管库 )
    ansible_ssh_private_key_file
    ssh使用的私钥文件。 如果使用多个键,并且您不想使用SSH代理程序,则很有用。
    ansible_ssh_common_args
    此设置始终附加到sftp , scpssh的默认命令行。 有用于配置特定主机(或组)的ProxyCommand 。
    ansible_sftp_extra_args
    此设置始终附加到默认sftp命令行。
    ansible_scp_extra_args
    此设置始终附加到默认的scp命令行。
    ansible_ssh_extra_args
    此设置始终附加到默认的ssh命令行。
    ansible_ssh_pipelining
    确定是否使用SSH流水线。 这可以覆盖ansible.cfgpipelining设置。
    ansible_ssh_executable(在版本2.2中添加)
    此设置将覆盖使用系统ssh的默认行为。 这可以覆盖ssh_executable中的ssh_executable设置。

    特权升级(有关详细信息,请参阅可选权限升级 ):

    ansible_become
    相当于ansible_sudoansible_su ,允许强制特权升级
    ansible_become_method
    允许设置权限升级方法
    ansible_become_user
    相当于ansible_sudo_useransible_su_user ,允许设置您成为通过特权升级的用户
    ansible_become_pass
    相当于ansible_sudo_passansible_su_pass ,您可以设置权限升级密码(永远不要以纯文本形式存储此变量;请始终使用保管 ),请参见变量和保管库 )

    远程主机环境参数:

    ansible_shell_type
    目标系统的shell类型。 除非您将ansible_shell_executable设置为非Bourne(sh)兼容shell,否则不应使用此设置。 默认情况下,使用sh -style语法格式化命令。 将此设置为cshfish将导致在目标系统上执行的命令来跟随这些shell的语法。
    ansible_python_interpreter
    目标主机python路径。 这对于具有多个Python或不在/ usr / bin / python (如* BSD)的系统或/ usr / bin / python不是2.X系列Python的系统非常有用。 我们不使用/ usr / bin / env机制,因为这要求远程用户的路径设置正确,并且还假定python可执行文件被命名为python,其中可执行文件可能被命名为python2.6 。
    ansible _ * _解释
    适用于任何类似ruby或perl的工作,就像ansible_python_interpreter一样。 这将替代将在该主机上运行的模块的shebang。

    2.1版新功能

    ansible_shell_executable
    这将设置可控制器将在目标计算机上使用的shell,覆盖默认为/ bin / sh的 ansible.cfg中的executable 。 如果不可能使用/ bin / sh (即/ bin / sh未安装在目标机器上或不能从sudo运行),那么您应该只能改变它。

    Ansible-INI主机文件中的示例:

      some_host ansible_port = 2222 ansible_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


    非ssh连接类型

    使用主机特定参数ansible_connection=<connector> ,可以更改连接类型。 以下基于非SSH的连接器可用:

    local

    该连接器可以用于将该playbook部署到控制机器本身。

    docker

    该连接器使用本地Docker客户机将该playbook直接插入Docker容器。 以下参数由此连接器处理:

    ansible_host
    要连接的Docker容器的名称。
    ansible_user
    在容器内操作的用户名。 用户必须存在于容器内。
    ansible_become
    如果设置为true ,则将使用become_user在容器内操作。
    ansible_docker_extra_args
    可以是由Docker理解的任何其他参数的字符串,它们不是特定于命令的。 此参数主要用于配置远程Docker守护程序以使用。
    以下是如何立即部署到创建的容器的示例:
    - name: create jenkins container
      docker_container:
        docker_host: myserver.net:4243
        name: my_jenkins
        image: jenkins
    
    - name: add container to inventory
      add_host:
        name: my_jenkins
        ansible_connection: docker
        ansible_docker_extra_args: "--tlsverify --tlscacert=/path/to/ca.pem --tlscert=/path/to/client-cert.pem --tlskey=/path/to/client-key.pem -H=tcp://myserver.net:4243"
        ansible_user: jenkins
      changed_when: false
    
    - name: create directory for ssh keys
      delegate_to: my_jenkins
      file:
        path: "/var/jenkins_home/.ssh/jupiter"
        state: directory

     




     

  • 相关阅读:
    论 设计模式及项目架构的作用
    Linux根据进程号查找其程序文件路径 及 lsof 命令使用
    Synchronized 原理
    Seata 中类SPI使用机制分析
    redisson spring boot starter 做分布式锁
    TTFB 时间过长
    ShardingSphere Hint模式 SpringBoot + Mybatis
    Core源码(十三)Stack和ConcurrentStack
    C#源码(十二) HashSet
    Core源码(十一)Queue
  • 原文地址:https://www.cnblogs.com/yitianyouyitian/p/9183295.html
Copyright © 2020-2023  润新知