Ansible inventory 文件

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

在本机 ubuntu 18.04 操作其他三主机:

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

配置好 ssh keys

主机

INI

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 也可使用其他名字

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

INI

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

YAML

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

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

  • all:所有的主机
  • ungrouped:不在除 all 之外的其他的组的主机

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

主机变量

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

[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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值