我有一个剧本
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 文件中的任何内容。
如何在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'']'