如何有条件地为 Ansible 中的游戏定义主机 [英] howto conditionally define hosts for a play in Ansible

查看:20
本文介绍了如何有条件地为 Ansible 中的游戏定义主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在多组节点上运行一组任务.我为每个定义了组的节点集都有一个专用的 INI 文件.

I am running a set of tasks on multiple sets of nodes. I have a dedicated INI file for each set of node with groups defined.

对于其中一个组,有一个节点 ('nfs_server') 我想例外.我创建了一个有条件定义的主机的剧本,例如:

For one of the groups, there is one node ('nfs_server') for which I want to make an exception. I have created a playbook with conditionally defined hosts like:

  hosts: "{{ nfs_server if monitoring_enabled else '' }}"
  gather_facts: yes
  become: yes
  tasks:
    - debug: var=monitoring_enabled
    - name: install monitoring
      when: monitoring_enabled
      include: extras/setup_monitoring.yml

我尝试在清单或 group_vars 文件中定义变量的地方,但总是收到未定义变量的错误:

Where I tried to define the variable in either the inventory, or the group_vars file, but I alwasy get the error that the variable is not defined:

<代码>错误!'hosts' 字段有一个无效值,其中包括一个未定义的变量.错误是:'monitoring_enabled' 未定义.

当我将主机(临时)更改为全部"时,运行完成,并且我看到变量已正确评估:

When I change the hosts (temporarily) to 'all', the run completes, and I see that the variable is properly evaluated:

ok: [master1] =>{ "monitoring_enabled": "false";}

WA1:通过使用children"语句在我的清单中定义一个额外的组,我确实找到了一个相当丑陋的解决方法.然后必须为我的所有节点使用适当的节点定义该组我想在其中添加额外监控的服务器,并在特殊集的库存中清空.

WA1: I did find an imho rather ugly workaround by defining an additional group in my inventory using the 'children' statement. This group must then be defined with the proper nodes for all my servers where I want to add the extra monitoring, and empty in the inventory for the exceptional set.

WA2:我还在 我可以在 playbook 中使用来自 Web 服务的清单数据吗? 其中add_host"用于创建动态组.我已经能够完成这项工作,但又不是很干净.

WA2: I also found a workaround in Can I use inventory data from a web service within a playbook? where the 'add_host' is used to create a dynamic group. I've been able to make this work, but again not very clean.

问题:有没有更简单的方法来有条件地定义一个播放的主机集?

question: Is there an easier way to define the set of hosts for a play conditionally?

强烈的偏好是我也可以为布尔值设置一个默认值,这样我就不需要在任何地方定义它.

Strong preference would be such that I can have also a default value for the boolean, such that I do not need to define it everywhere.

谢谢,M.

推荐答案

关于你的发现

... 使用子项"在我的库存中定义一个额外的组陈述.然后必须使用适当的节点定义该组我所有的服务器...

... defining an additional group in my inventory using the 'children' statement. This group must then be defined with the proper nodes for all my servers ...

我不认为这是一种解决方法.如果您的基础设施以某种方式结构化,您提到的方法将具有一些优势.假设您的库存看起来像

I wouldn't consider that as a workaround. If your infrastructure is somehow structured your mentioned approach would have some advantages. Let's assume your inventory looks like

[infra:children]
prod
qa
dev

webserver
database

[infra:vars]
# No global variables yet

[prod:children]
webserver_prod
database_prod

[qa:children]
webserver_qa
database_qa

[dev:children]
webserver_dev
database_dev

# Logical services nodes

[webserver:children]
webserver_prod
webserver_qa
webserver_dev

[database:children]
database_prod
database_qa
database_dev

# List of all hosts

# [prod:children]

[webserver_prod]
web.prod.example.com

[database_prod]
primary.prod.example.com
secondary.prod.example.com

# [qa:children]

[webserver_qa]
web.qa.example.com

[database_qa]
primary.qa.example.com
secondary.qa.example.com

# [dev:children]

[webserver_dev]
web.dev.example.com

[database_dev]
primary.dev.example.com
secondary.dev.example.com

可以将剧本、任务等应用到该图中的某些节点,例如

it is than possible to apply playbooks, tasks, etc. just to certain nodes in that graph like

ansible-inventory dev --graph
ansible-inventory database_dev --graph
...

一切都取决于库存的结构,但之后会更容易.

All depdends on the structure of the inventory but makes it easier afterwards.

这篇关于如何有条件地为 Ansible 中的游戏定义主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆