我试图通过能够选择性地选择使用标签的执行步骤来尽可能重复使用。到目前为止,我还没有找到合适的东西。
这是我为角色启用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
我曾经有一个循环来复制这两个文件,但是在安全升级使我将其分为3个相同的步骤之后,我需要能够激活/停用自动启动的事实。我希望会有较少的详细方法。
然后,由于它是一个角色,我希望能够独立运行它,所以我需要创建一个特定的剧本:
---
- hosts: all
gather_facts: yes
tasks:
- name: run security-upgrade role with 'noreboot' option
include_role:
name: security-upgrades
这效果很好,但是我似乎无法执行应互斥的最后两个步骤之一。将 tags:
添加到剧本没有用处,这不能使我有选择地执行一个选项。
您可以分别在任务中使用when: security-upgrades_noreboot is defined
和when: security-upgrades_noreboot is undefined
。您将需要通过剧本中的变量,并使它运行一个任务或另一个任务:
---
- hosts: all
gather_facts: yes
tasks:
- name: run security-upgrade role with 'noreboot' option
include_role:
name: security-upgrades
vars:
security-upgrades_noreboot: yes
这应该跳过具有when: security-upgrades_noreboot is undefined
的任务。如果您不通过VAR,则此任务将运行,但另一个任务会跳过。