Ansible 运行角色,并在剧本中定义特定标签

问题描述 投票:0回答:1

用例

我有一个剧本

test.yml
,其中执行多个角色:

- name: 'create cnames'
  hosts: sysops_setup_grafana_server

  roles:
  - role: test.sysops.vcenter_vm
  - role: test.sysops.certificate

角色内

test.sysops.vcenter_vm
包含多个任务:

---
- name: 'configure active directory'
  ansible.builtin.include_tasks: ad.yml
  tags:
  - 'vcenter_vm'
  - 'vcenter_vm:ad:computer'

- name: 'configure eam'
  ansible.builtin.include_tasks: eam.yml
  tags:
  - 'vcenter_vm'
  - 'vcenter_vm:eam'
  - 'vcenter_vm:eam:service_ou'
  - 'vcenter_vm:eam:groups'


- name: 'configure dns'
  ansible.builtin.include_tasks: dns.yml
  tags:
  - 'vcenter_vm'
  - 'vcenter_vm:dns'
  - 'vcenter_vm:dns:arecords'
  - 'vcenter_vm:dns:cnames'
  - 'vcenter_vm:dns:cleanup'

问题

我希望剧本

test.yml
使用标签
test.sysops.vcenter_vm
运行角色
vcenter_vm:dns:cnames
,而不在
ansible-playbook test.yml --tags 'vcenter_vm:dns:cnames'
命令中显式设置标签。

标签_运行

我知道 TAGS_RUN 配置设置存在。但由于我在共享环境中工作,我不想更改 ansible.cfg 文件中的任何内容。

ansible
1个回答
0
投票

如何在playbook运行期间定义标签?

可以在运行时访问标签读取变量内容,但以后无法设置或更改它们。

我知道

TAGS_RUN
配置设置存在。但由于我在共享环境中工作,我不想更改
ansible.cfg
文件中的任何内容。

无需每次都更改配置文件,有以下选项。一个最小的示例手册

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - debug:
      msg: "Show given tags: {{ ansible_run_tags }}"
    tags: always

通过

调用
ANSIBLE_RUN_TAGS='test' ansible-playbook tags.yml

将产生

的输出
TASK [debug] ***********************
ok: [localhost] =>
  msg: 'Show given tags: [''test'']'
© www.soinside.com 2019 - 2024. All rights reserved.