在Ansible中有选择地启用播放执行步骤的最佳方法

在Ansible中有选择地启用播放执行步骤的最佳方法,ansible,Ansible,我试图通过有选择地选择带有标记的执行步骤,使Ansible角色尽可能可重用。到目前为止,我还没有找到合适的方法 以下是我为在linux机器上启用安全更新的角色所做的尝试: 角色摘录: - name: Copy the 20auto-upgrades templates to enable automatic security updates copy: src: 20auto-upgrades dest: /etc/apt/apt.conf.d/20auto-upgrades

我试图通过有选择地选择带有标记的执行步骤,使Ansible角色尽可能可重用。到目前为止,我还没有找到合适的方法

以下是我为在linux机器上启用安全更新的角色所做的尝试:

角色摘录:

- name: Copy the 20auto-upgrades templates to enable automatic security updates
  copy:
    src: 20auto-upgrades
    dest: /etc/apt/apt.conf.d/20auto-upgrades
    owner: root
    group: root
    mode: 0644
  become: yes

- name: Copy the 50unattended-upgrades templates to configure security updates with AUTOMATIC REBOOT
  copy:
    src: 50unattended-upgrades-reboot
    dest: /etc/apt/apt.conf.d/50unattended-upgrades
    owner: root
    group: root
    mode: 0644
  become: yes
  tags: 
    - reboot

- name: Copy the 50unattended-upgrades templates to configure security updates with NO AUTOMATIC REBOOT
  copy:
    src: 50unattended-upgrades-noreboot
    dest: /etc/apt/apt.conf.d/50unattended-upgrades
    owner: root
    group: root
    mode: 0644
  become: yes
  tags:
    - noreboot
我曾经有一个循环来复制这2个文件,但在安全升级后,我需要能够激活/停用autoreboot,这一事实使我将其分为3个相同的步骤。我希望有一种不那么冗长的方式

然后,因为这是一个角色,我希望能够独立运行它,所以我需要创建一个特定的剧本:

---
- hosts: all
  gather_facts: yes
  tasks:
  - name: run security-upgrade role with 'noreboot' option
    include_role:
      name: security-upgrades
这很有效,但我似乎不能只执行最后两个应该相互排斥的步骤中的一个。
添加
标签:
到剧本中是没有用的,它不能让我有选择地执行一个选项。

当定义了安全升级\u noreboot时,可以使用
;当分别定义了任务时,可以使用
安全升级\u noreboot。您需要在playbook中传递变量,并让它运行一个或另一个任务:

---
- hosts: all
  gather_facts: yes
  tasks:
  - name: run security-upgrade role with 'noreboot' option
    include_role:
      name: security-upgrades
    vars: 
      security-upgrades_noreboot: yes

当:security-upgrades\u noreboot未定义时,应跳过具有
的任务。如果您没有通过var,此任务将运行,但另一个任务将被跳过。

谢谢Grigory,这就是我要找的。我想另一种方法是在jinja模板中为
50无人参与升级
设置一个条件变量。然后,我可能能够在要复制的文件上保持循环,并避免重复相同的步骤。