简体   繁体   English

如何在ansible_playbook中覆盖主机变量

[英]How to override hosts variable in ansible_playbook

I am new to anisble... 我是新来的...

I want to make sure mongo is running on all the hosts with tag tag_role_mongo and node is running on tag_role_node 我想确保mongo在所有带有tag_role_mongo标签的主机上运行,​​并且node在tag_role_node上运行

is it possible to override hosts variable 是否可以覆盖主机变量

hosts: {{ item.tag_name }} 
  tasks:
   command: // check ps output {{ item.process_name }} 
with_items:
  - tag_name: tag_role_mongo
    process_name: "mongo"
  - tag_name: tag_role_node
    process_name: "node"

I am pretty sure, my syntax is not, correct, my question is it even possible to do such thing using a playbook. 我很确定,我的语法不正确,正确的是,我的问题是甚至可以使用剧本来做这种事情。 In all the playbook examples, hosts is fixed or can be overridden from command line using extra-args option. 在所有剧本示例中,主机都是固定的,或者可以使用extra-args选项从命令行覆盖。

Any examples would be very helpful 任何例子都将非常有帮助

I'm not fully sure if I understand your question correctly but tags are not added on a host-level but on task-level - see the documentation . 我不确定我是否正确理解了您的问题,但是标记不是在主机级别而是在任务级别添加-请参阅文档 What you probably mean is to execute the same command for two different groups (mongo and node). 您可能的意思是对两个不同的组(mongo和node)执行相同的命令。 For this you can just split your playbook in two parts: 为此,您可以将剧本分为两部分:

hosts: mongo_hosts 
tasks:
  command: ...

hosts: node_hosts
tasks:
  command: ...

Not very nice but should solve the problem. 不是很好,但是应该解决问题。 You can also create a role and pass to this role just the process name you want to check for, so maintenance become easier. 您还可以创建一个角色,然后将要检查的流程名称传递给该角色,因此维护变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM