如何在ansible库存中构建一组具有模式的组?

时间:2017-12-01 16:10:39

标签: ansible

我想建立一个目标主机列表,这些主机位于DC-North和DC-South两个组中,并且不是数据库服务器。

这是我的方法,不幸的是失败了:

[DC-North]
host1
host2 

[DC-South]
host3
host4 

[dbservers]
host3
host1 

[target:children]
DC-North:&DC-South:!dbservers

我如何定义这个正确的?

4 个答案:

答案 0 :(得分:0)

你定义得很好。它位于错误的位置。主机模式进入剧本

- hosts: DC-North:&DC-South:!dbservers
...

或在--limit / -l

的CLI上
ansible-playbook -l 'DC-North:&DC-South:!dbservers' some_playbook.yml

答案 1 :(得分:0)

无法使用模式,但可以使用children语法使用YAML锚点或引用组:

apps_web_a:
  hosts: &apps_web_a
    prod-api-1:
    prod-admin-1:

apps_web_b:
  hosts: &apps_web_b
    prod-api-2:
    prod-admin-2:

# YAML anchors:
# apps_web:
#   hosts:
#     <<: *apps_web_a
#     <<: *apps_web_b

apps_web:
  children:
    apps_web_a:
    apps_web_b:

阅读有关组分组的小注释:https://docs.ansible.com/ansible/2.7/user_guide/intro_inventory.html

也可以使用INI中的:children后缀或YAML中的children:条目来分组组。您可以使用:varsvars: ...

来应用变量

那样,只能进行代数 UNION 设置操作...

在这里关注我的问题:https://www.reddit.com/r/ansible/comments/f68yse/is_there_syntax_for_creating_group_from_other/

答案 2 :(得分:0)

docs:https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#inheriting-variable-values-group-variables-for-groups-of-groups


[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

答案 3 :(得分:-2)

在您的广告资源更改中:

[target:children]
DC-North
DC-South