Ansible运行所有任务,即使使用了标记

Ansible运行所有任务,即使使用了标记,ansible,ansible-2.x,ansible-inventory,ansible-facts,Ansible,Ansible 2.x,Ansible Inventory,Ansible Facts,这是我的任务,有两个标签 - name: Set custom iptables rules iptables_raw: name: 'iptables_custom_rules' rules: '{{ iptables_custom_rules }}' tags: 'commonrules' - name: Set XXX iptable rules iptables_raw: name: 'iptables_wsg_rules' rules:

这是我的任务,有两个标签

- name: Set custom iptables rules
  iptables_raw:
    name: 'iptables_custom_rules'
    rules: '{{ iptables_custom_rules }}'
  tags: 'commonrules'


- name: Set XXX iptable rules
  iptables_raw:
    name: 'iptables_wsg_rules'
    rules: '{{ iptables_wsg_rules }}'
  tags: 'wsgrules'
在iptable.yml文件中,我包含了带有标记的角色

- hosts: iptables
  roles:
    - { role: "Iptables", tags: "commonrules" }
它应该只运行带有commonrules的标记,但当我运行playbook时,它会运行所有任务。

标记仅在带有的ansible playbook的命令行上(取消)激活。
剧本和角色中的标签声明仅用于声明激活这些任务和角色的标签

默认情况下,ansible运行时就好像指定了所有标签一样

因此,如果您只想运行带有“commonrules”标记的任务,您必须:

  • 从角色部分删除标记(否则它将应用于角色的所有任务)
  • 使用选项调用ansible playbook
    ——标记commonrules

  • 如果要动态应用任务,最好的方法是基于仅为需要应用任务的主机定义的标志,使用
    when
    条件

    - name: Set custom iptables rules
      iptables_raw:
        name: 'iptables_custom_rules'
        rules: '{{ iptables_custom_rules }}'
      when: commonrules | default(False)
    
    - name: Set XXX iptable rules
      iptables_raw:
        name: 'iptables_wsg_rules'
        rules: '{{ iptables_wsg_rules }}'
      when: wsgrules | default(False)
    
    然后,在这个例子中,类似于:

    # group_vars/common.yml
    commonrules: True
    
    # group_vars/wsg.yml
    wsgrules: True
    
    甚至更好的是,您甚至可以在when条件下测试
    iptables\u XXX\u规则
    变量的存在性,因此任务将仅对定义了这些变量的主机执行。

    标记仅在
    ansible playbook
    的命令行上使用。
    剧本和角色中的标签声明仅用于声明激活这些任务和角色的标签

    默认情况下,ansible运行时就好像指定了所有标签一样

    因此,如果您只想运行带有“commonrules”标记的任务,您必须:

  • 从角色部分删除标记(否则它将应用于角色的所有任务)
  • 使用选项调用ansible playbook
    ——标记commonrules

  • 如果要动态应用任务,最好的方法是基于仅为需要应用任务的主机定义的标志,使用
    when
    条件

    - name: Set custom iptables rules
      iptables_raw:
        name: 'iptables_custom_rules'
        rules: '{{ iptables_custom_rules }}'
      when: commonrules | default(False)
    
    - name: Set XXX iptable rules
      iptables_raw:
        name: 'iptables_wsg_rules'
        rules: '{{ iptables_wsg_rules }}'
      when: wsgrules | default(False)
    
    然后,在这个例子中,类似于:

    # group_vars/common.yml
    commonrules: True
    
    # group_vars/wsg.yml
    wsgrules: True
    

    或者更好的是,您甚至可以在when条件下测试
    iptables\u XXX\u规则
    变量的存在性,因此任务将仅对定义了这些变量的主机执行。

    感谢您提供的信息。我有不同的节点,这些节点具有不同的iptable规则集,一些节点应用commonrules,一些节点应用X规则,其他节点应用Y规则,处理它的最佳方式是什么。更新了我的回答我可以使用这样的方式吗-hosts:iptables角色:-{role:“Iptables”,当:name=='Set custom Iptables rules'}时,我不确定我是否在这里说了“name”,如果我错了,请更正我。不,您不能将未来的任务名称用作条件。但您可以使用以下标志之一调用角色:
    {role:iptable,commonrules:True}
    这就是我要找的……非常感谢:)谢谢你提供的信息。我有不同的节点,它们有不同的iptable规则集,有些节点应用commonrules,有些节点应用X规则,而其他节点应用Y规则,处理它的最佳方式是什么。更新了我的答案我可以使用类似的东西吗-hosts:iptables角色:-{role:“iptables”,when:name=='Set custom iptables rules'},我不确定我是否在这里说了“name”,如果我错了,请更正我。不,您不能使用未来任务名称作为条件。但您可以使用以下标志之一调用角色:
    {角色:iptable,commonrules:True}
    这就是我要找的……非常感谢:)