Ansible inventory 文件

ansible.cfg 中的 inventory 参数定义主机的列表,默认存放在 /etc/ansible/hosts。除此配置文件外,也能够同时使用多个 inventory 文件,或者从动态云资源拉取 inventory 配置信息,支持不一样的格式,如 yamlini 等。python

在本机 ubuntu 18.04 操做其余三主机:git

  • localhost
  • 10.53.141.252ubuntu 18.04,用户 cec
  • 10.53.128.20RHEL 5.8,用户 durant,安装多版本 Python

配置好 ssh keysweb

主机

INIshell

localhost ansible_connection=local
10.53.141.252 ansible_user=cec
10.53.128.20 ansible_user=durant ansible_python_interpreter="/usr/bin/env python3"
复制代码

YAMLall 也可以使用其余名字json

all: 
  hosts: 
    localhost: 
      ansible_connection: local
    10.53.141.252: 
      ansible_user: cec
    10.53.128.20: 
      ansible_user: durant
      ansible_python_interpreter: "/usr/bin/env python3"
复制代码

INIubuntu

localhost ansible_connection=local

[dev]
10.53.141.252 ansible_user=cec
10.53.128.20 ansible_user=durant ansible_python_interpreter="/usr/bin/env python3"
复制代码

YAMLbash

all: 
  hosts: 
    localhost: 
      ansible_connection: local
  children:
    dev: 
      hosts: 
        10.53.141.252: 
          ansible_user: cec
        10.53.128.20: 
          ansible_user: durant
          ansible_python_interpreter: "/usr/bin/env python3"
复制代码

在上面的例子中,包含三个组:allungroupeddevssh

  • all:全部的主机
  • ungrouped:不在除 all 以外的其余的组的主机

所任何一个主机都至少在两个组中。即便 allungrouped 一直有,但不会出如今组列表(如group_names)中。ui

主机变量

给主机分配变量,在 playbook 中会用到spa

[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
复制代码

组的变量

属于整个组的变量

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
复制代码

请注意,这只是个同时给多个主机赋予变量的简便方法,即便你能够根据组指定目标主机。variables are always flattened to the host level before a play is executed.

嵌套组和变量

INI 文件使用 :children 或在 YAML 文件中使用 children: 入口,能够一个组设置为另外一个组的成员,使用 :varsvars: 设置变量。

[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
复制代码
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:
复制代码

子组有下列注意点:

  • 是一个子组的成员的主机,也是父组的成员
  • 子组的变量的优先级别比父组高
  • 组能够有多个组和子组,但不是环形关系
  • 主机能够有多个组,但一个主机只会有一个实例,从多个组中合并数据。

YAML 文件中,组的下一层只能是 varschildrenhosts

分离主机、组的特殊数据

若是要存储序列或 hash 值,或把 host、group 的变量与 inventory 文件分开,也有办法作到。

Ansible 实践中也不推荐在 inventory 文件中存储变量。

将主机、组的变量存放在 inventory 相关的一个独立文件中(不是目录,一般是文件),这些变量文件是 YAML 格式,扩展名通常是 ymlyamljson 或者没有扩展名

若是 inventory 文件路径是 /etc/ansible/hosts,主机名是 football,在组 raleighwebservers 中。下面的变量文件都是有效:

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

变量文件内容:

---
ntp_server: acme.example.org
database_server: storage.example.org
复制代码

固然,这些文件不存在也不要紧,由于是可选特性。

在高级用法中,也能够在组或主机下继续建立目录,Ansible使字典编纂顺序读取这文件,例如 raleigh 组:

/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings
复制代码

raleigh 组的全部主机都会有这些文件定义的变量,当一个单独文件太大的时候,用这个方法可更好的组织文件结构,或者当使用 Ansible Vault 做为组变量的一部分时。

group_vars/host_vars/ 能够存放在 playbook 或者 inventory 目录。若是两个路径都有,playbook 目录下的变量会覆盖 inventory 目录下的变量。

inventory 文件和变量加入 git 仓库(或其余版本控制系统)是一个追踪 inventory 和主机变量变化的好方法。

变量合并的方式

play 运行前,会对特定的主机进行变量合并,这可使 Ansible 专一于主机和任务,所以组实际只存在于 inventory 和主机匹配中。

下面是变量的优先顺序(低 -> 高):

  • all,由于 all 是全部组的父组
  • 父组
  • 子组
  • host

根据字母顺序合并相同级别的父组、子组,后面的覆前面的。

Ansible 2.4 开始,可使用 ansible_group_priority 改变相同级别组的合并顺序( parent/child 的顺序被解析后)。数字越大,越后合并,即优先度更高,默认是 1。

例如,若是这两个组优先级相同,结果是 testvar == b,但因为给 a_group 更高的权限,结果是 testvar == a

a_group:
    testvar: a
    ansible_group_priority: 10
b_group:
    testvar: b
复制代码

Inventory 参数

  • ansible_connection
  • ansible_host
  • ansible_user
  • ansible_ssh_pass
  • ansible_ssh_private_key_file
  • ansible_ssh_common_args
  • ansible_sftp_extra_args
  • ansible_scp_extra_args
  • ansible_ssh_extra_args
  • ansible_ssh_pipelining
  • ansible_ssh_executable
  • ansible_become
  • ansible_become_method
  • ansible_become_user
  • ansible_become_pass
  • ansible_become_exe
  • ansible_become_flags
  • ansible_shell_type
  • ansible_python_interpreter
  • ansible_*_interpreter
  • ansible_shell_executable
相关文章
相关标签/搜索