zoukankan      html  css  js  c++  java
  • 【Ansible 文档】【译文】主机清单文件

    Inventory 主机清单文件

    Ansible 可以对你的基础设施中多个主机系统同时进行操作。通过选择在Ansible的inventory列出的一部分主机来实现。inventory默认保存在/etc/ansible/hosts中。你可以通过指定 -i <path> 参数指定使用其他文件的路径。

    不仅可以使用这个默认的配置文件,你也可以同时指定多个文件,或者从动态的或者云资源上来去inventory,详细可查看Dynamic Inventory。Ansible 2.4 引入了inventory插件,使得主机清单文件可以灵活并且可定制。

    Hosts and Groups 主机和组

    inventory 文件可以是许多格式中的一种,取决于你有的inventory插件。例如,/etc/ansible/hosts 是一个ini-格式(Ansible默认的)文件,其看起来像下面这样:

    mail.example.com
    
    [webservers]
    foo.example.com
    bar.example.com
    
    [dbservers]
    one.example.com
    two.example.com
    three.example.com

    括号中的标题是组名,用来区分不同的主机系统,以及决定你在什么时候、为了什么目的操作主机。

     YAML 格式看起来想下面这样子:

    all:
      hosts:
        mail.example.com
      children:
        webservers:
          hosts:
            foo.example.com:
            bar.example.com:
        dbservers:
          hosts:
            one.example.com:
            two.example.com:
            three.example.com:

    把一台主机放到一个以上的组中是可以的,例如一个服务器可以是web服务器,也可以是数据库服务器。如果你这样做了,这时属于两个组的变量都可以为这台主机所用,至于变量的优先级关系将于以后的章节中讨论。

    如果有主机的SSH端口不是标准的22端口,可在主机名之后加上端口号,用冒号分隔。SSH 配置文件中列出的端口号不会在 paramiko 连接中使用,会在 openssh 连接中使用。

    端口号不是默认设置时,可显示设置为:

    badwolf.example.com:5309

    假设你有一些静态IP地址,并且希望设置一些别名,但不是在系统的 host 文件中设置,或者你是通过隧道在连接,那么你可以通过变量描述主机:

    INI格式

    jumper ansible_port=5555 ansible_host=192.0.2.50

    YAML格式

    hosts:
      jumper:
        ansible_port: 5555
        ansible_host: 192.0.2.50

    在上面的例子中,试着使用Ansible主机jumper(不是一个真正主机名)会去连接192.0.2.50的5555端口。

    注意,这是通过inventory文件来定义特殊变量的特性。通常来说,这不是最好的定义变量的方式(描述你的系统策略的变量)的最好方式。后面会说到这个问题。

    如果你正添加一组相似模式的主机,你可以按照如下方式列出,而不是列出全部:

    [webservers]
    www[01:50].example.com

    对于数字模式,头部0可以被包含或者移除,如预期的一样。范围是多种多样的。你可以指定字符范围:

    [databases]
    db-[a:f].example.com

    注意

      Ansible 2.0 中, ansible_ssh_useransible_ssh_host, and ansible_ssh_port中的 ‘ssh’已经过时了。使用ansible_useransible_host, and ansible_port。

      如果你使用的是2.0之前的版本,你应该使用老版本的Ansible变量(Ansible_ssh_*),短格式变量会忽略,没有警告,在老版本Ansible中。

    你可以基于每一个主机选择连接类型和连接用户名:

    [targets]
    
    localhost              ansible_connection=local
    other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
    other2.example.com     ansible_connection=ssh        ansible_user=mdehaan

    正如上面说的,在inventory文件中设置这些变量是临时的方法,后面我们会讨论如何将这些设置保存为 ‘host_vars’ 目录中的独立的文件。

    Host Variables 主机变量

    如上面描述的,可以很容易的定义hosts的变量,这些变量可以在后面的playbooks中使用:

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

    Group Variables 组变量

     变量也可以一起应用到整个组中:

    ini格式

    [atlanta]
    host1
    host2
    
    [atlanta:vars]
    ntp_server=ntp.atlanta.example.com
    proxy=proxy.atlanta.example.com

     YAML格式

    atlanta:
      hosts:
        host1:
        host2:
      vars:
        ntp_server: ntp.atlanta.example.com
        proxy: proxy.atlanta.example.com

    要了解这是唯一方便的方式,变量一起应用到多个主机的方式。即使你可以把目标主机分组,在play执行前,变量总是会被扁平化到主机层级。

    Groups of Groups, and Group Variables 组中组和组变量

    可以通过在INI格式中的.children 后缀或者YAML格式中的children entry 来实现组中组。你可以使用 :vars or vars::

    INI

    [atlanta]
    host1
    host2
    
    [raleigh]
    host2
    host3
    
    [southeast:children]
    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

    YAML

    all:
      children:
        usa:
          children:
            southeast:
              children:
                atlanta:
                  hosts:
                    host1:
                    host2:
                raleigh:
                  hosts:
                    host2:
                    host3:
              vars:
                some_server: foo.southeast.example.com
                halon_system_timeout: 30
                self_destruct_countdown: 60
                escape_pods: 2
           northeast:
           northwest:
           southwest:

    如果你需要存储列表或者hash数据,你应该使用另外一种方法:把 host 和 group 的变量分开配置,请看后续部分的说明。

    子组有几个属性需要注意:

    • 任何子组的成员自动称为父组成员。
    • 子组变量优先级比父组更高(覆盖)。
    • 组可以有多个父组和子组,但不能有循环关系。
    • hosts可以被包含在多个组中,但是仅会有一个host的一个实例,从多个组中合并数据。

    Default groups 默认组

    有俩个默认组: all and ungrouped 。 all 包含了所有的主机,ungrouped包含了所有没有被包含仅除all之外的其他组中。

    每一个主机至少属于俩个组。尽管all和ungrouped总是出现,但他们被隐藏并且不出现在group listtings,例如group_names。    

    Splitting Out Host and Group Specific Data 分开定义主机和组特定数据

    这是Ansible更好的实践:不再main inventory文件中存储变量。

    除了在inventory文件中存储变量之外,host和group变量可以被存储在单独的文件,相对于inventory文件的路径(不是目录,总是文件)。

    变量文件是YAML格式的。合法的文件扩展名为 ‘.yml’, ‘.yaml’, ‘.json’, 或者没有扩展名。详细可查看 YAML Syntax

    假设inventory文件是:

    /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

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

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

    如果这些文件不存在没关系,这是可选的。

    还有更进一步的运用,你可以为一个主机,或一个组,创建一个目录,目录名就是主机名或组名。目录中的可以创建多个文件,文件中的变量都会被读取为主机或组的变量。如下 ‘raleigh’ 组对应于 /etc/ansible/group_vars/raleigh/ 目录,其下有两个文件 db_settings 和 cluster_settings,其中分别设置不同的变量:

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

    所有在raleigh组中的主机会拥有这些文件中定义的变量。这是有用的,当单个文件开始变得越来越大时。 还有一个方式也可参考,详见  Ansible Vault 关于组变量的部分。注意,分文件定义变量的方式只适用于 Ansible 1.4 及以上版本。

    提示:Ansible 1.2之后,group_vars/ 和 host_vars/ 目录可以存在playbook目录或者inventory目录。如果俩个路径都存在,playbook中的变量会覆盖inventory设置的变量。

    提示:保存你的inventory file和变量在一个git repo(或者其他版本控制)是非常棒的方式,可以追踪你的inventory和host变量的变化。

    List of Behavioral Inventory Parameters 动态的inventory参数列表

    正如上面提到的,设置如下变量控制Ansible如何与远程主机交互。

    主机连接参数:

      ansible_connection  到远端主机的连接类型。这个可以是任何一个Ansible连接插件的名字。SSH协议类型是:smart/ssh/paramiko。默认是smart。非SSH类型在下一章节描述。

    所有连接插件共有的一般参数:

      ansible_host  要连接的主机(IP地址等),它不同于你给出的别名

           ansible_port  如果不是22,指定ssh端口

           ansible_user 默认使用的用户名

    特定连接类型的参数

    ansible_ssh_pass
      要使用的ssh密码,不要存储这个变量为普通文本,总是使用vault。详细查看  Variables and Vaults
    ansible_ssh_private_key_file
      指定ssh使用的私钥文件。如果使用多个keys并且你不像使用SSH代理时有用
    ansible_ssh_common_args
      该设置总是被添加到 sftp/scp/ssh 默认的命令行当为某一主机或者组配置一个ProxyCommand命令时有用
    ansible_sftp_extra_args
      该设置总添加到默认的 sftp 命令行
    ansible_scp_extra_args
      该设置总是添加到默认的scp命令行
    ansible_ssh_extra_args
      该设置总是添加到默认的ssh命令行
    ansible_ssh_pipelining
      决定是否使用SSH pipelining,这个可以覆写Ansible.cfg配置文件中的pipelining设置
    ansible_ssh_executable (added in version 2.2)
      该设置覆写默认使用系统ssh的行为,可以覆写Ansible.cfg配置文件中的pipelining设置

    特权上升

    ansible_become
      等价于 ansible_sudo 或 ansible_su, 允许强制特权上升
    ansible_become_method
      允许设置特权上升方法
    ansible_become_user
      等价于 ansible_sudo_user 或 ansible_su_user, 允许通过特权上升设置你成为的用户
    ansible_become_pass
      等价于 ansible_sudo_pass 或 ansible_su_pass, 允许你设置特权上升密码
    ansible_become_exe
      等价于 ansible_sudo_exe 或 ansible_su_exe, 允许你设置选择的上升方法的可执行程序
    ansible_become_flags
      等价于 ansible_sudo_flags 或 ansible_su_flags, 允许你设置标志传给被选择的方法,这个同样可以全局的设置,在ansible.cfg中

    远程主机环境参数

    ansible_shell_type
      目的主机的shell type。你不应该使用这个设置除非设置了ansible_shell_executable为一个非bourne(sh) 兼容shell。
      默认命令使用sh-style语法格式化。
      设置该参数为csh或者fish会导致目标系统上指定的命令跟随那些shell的语法。

    ansible_python_interpreter
      目的主机的python path。这是有用的对于多余一个Python,或者python没有在/usr/bin/python中的系统,来说是有用的
      例如*BSD或者其/usr/bin/python不是2.x版本的系统。
      我们不使用 /usr/bin/env 机制,因为它要求远程用户来设置正确的path并且需要假设python可执行程序是名为“python”,而不是命名为python2.6

    ansible_*_interpreter
      用于ruby或者perl的东西 并且和 ansible_python_interpreter 一样工作。 这回替代该台主机的模块shebang

      ansible_shell_executable 2.1 新特性

        这设置了ansible要使用的目的主机上的shell,覆写ansible.cfg中executable配置。其默认值是/bin/sh。你仅在不能使用/bin/sh时更改它

     Ansible-INI host file 例子

    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

    Non-SSH connection types 非SSH连接类型

    正如上面的部分,ansible在ssh上执行playbook,但是它不限于该连接类型。使用主机连接参数,ansible_connection=<connector>,连接类型可以被更改。下面是支持的非SSH连接类型:

    local

      该连接器可以用来部署playbook来控制自身机器。

    docker

      该连接器直接在docker容器中部署playbook,使用本地 docker client,下面的参数可以传入该连接器处理:

    ansible_host
      The name of the Docker container to connect to.
    ansible_user
      The user name to operate within the container. The user must exist inside the container.
    ansible_become
      If set to true the become_user will be used to operate within the container.
    ansible_docker_extra_args
      Could be a string with any additional arguments understood by Docker, which are not command specific. This parameter is mainly used to configure a remote Docker daemon to use.

    - 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
  • 相关阅读:
    小小的封装了一个pie的echarts
    recent.css常用的页面初始化样式
    bootstrap表格多样式及代码
    Java内存回收机制
    栈帧
    互斥锁和条件变量
    UML类图的常见关系
    堆栈详解(数据与内存中的存储方式)
    PR消减视频中的鼠标声
    SmartPlant Review 三维视图快捷键
  • 原文地址:https://www.cnblogs.com/pengyusong/p/7686474.html
Copyright © 2011-2022 走看看