如何在ansible_剧本中重写hosts变量

如何在ansible_剧本中重写hosts变量,ansible,ansible-inventory,Ansible,Ansible Inventory,我是新来的茴香 我想确保mongo在所有带有tag\u role\u mongo的主机上运行,节点在tag\u role\u node上运行 可以重写hosts变量吗 hosts: {{ item.tag_name }} tasks: command: // check ps output {{ item.process_name }} with_items: - tag_name: tag_role_mongo process_name: "mongo" - ta

我是新来的茴香

我想确保mongo在所有带有tag\u role\u mongo的主机上运行,节点在tag\u role\u node上运行

可以重写hosts变量吗

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"
我很确定,我的语法是不正确的,我的问题是,甚至可以用剧本来做这样的事情。 在所有playbook示例中,主机是固定的,或者可以使用额外的args选项从命令行重写主机


任何示例都会非常有用

我不确定是否正确理解您的问题,但标记不是在主机级别添加的,而是在任务级别添加的-请参阅。您可能的意思是对两个不同的组mongo和node执行相同的命令。 为此,您可以将剧本分为两部分:

hosts: mongo_hosts 
tasks:
  command: ...

hosts: node_hosts
tasks:
  command: ...
不是很好,但应该可以解决问题。您还可以创建一个角色,并将要检查的进程名称传递给该角色,这样维护就更容易了