Ansible: In a playbook, filter a role by tag(s) without passing at the command-line

半世苍凉 提交于 2019-12-03 10:27:16

问题


In Ansible 1.7, I can use --tags from the command-line to only run a subset of that playbooks tasks.

But I'm wanting to bake into my playbook to run a set of roles with only tasks that match tags. That is, I don't want to have to pass this in via the command-line since it will be the same every time.

At first I thought it was this command, but this does the opposite: tagging tasks with these tags instead of filtering them out based on this.

roles:
  - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }

I can imagine implementing this using conditionals but tags would be a much more elegant way of achieving this.


回答1:


You only have the following options with the current version of Ansible:

  1. Specify the tags on the command line
  2. Use a variable instead of a tag to conditionally run tasks
  3. Split your webserver role into multiple roles and use role dependencies for the common tasks

This feature request has come up on the mailing list a few times and I haven't seen any indication from the dev team that it will be added as a new feature.




回答2:


The use of conditionals can meet your needs.

You can see more information in my answer to another person's similar question here.



来源:https://stackoverflow.com/questions/25674649/ansible-in-a-playbook-filter-a-role-by-tags-without-passing-at-the-command-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!