tman
tman

Reputation: 425

Ansible, call specific role tasks via tags isn't working, all tasks are being ran

I'm using Ansible 2.3.2.0, and am calling a role that is running 4 different tasks.

When I call the role via a playbook, and specify specific tags, the entire role and all tasks are being run. I saw where this was a known issue after ansible 2.0, and the suggested fix was to add a static directive under include statements. I tried that, but still all tasks were run. Here is the role statement in the playbook...

roles:
    - {role: ansible-role-auto-deploy, tags: [ 'ami', 'launch_config', 'asg']}

And here is the roles' main.yml

---

- include: ami.yml
  static: yes
  tags:
    - ami

- include: launch_config.yml
  static: yes
  tags:
    - launch_config

- include: asg.yml
  static: yes
  tags:
    - asg

- include: ami_lc_cleanup.yml
  static: yes
  tags:
    - ami_lc_cleanup

When I run this with the roles statement above, I would expect ami, launch_config, and asg tasks to be run, but in addition the ami_lc_cleanup task is also being run.

The tasks all work, I just don't know how to limit which tasks are being run. Does anyone have any suggestions on how to get it to work (with being able to specify tasks via tags)?

Upvotes: 0

Views: 5017

Answers (1)

clemens321
clemens321

Reputation: 2098

Conditional includes should not be defined as static using static: no.

Edit

Assuming from your comments you misunderstood the tags statement in your playbook. This is not to included roles with the defined tags, but to include a role based on tags given at the command line.

Which tags are active in your play is defined by the command line only (as far as I know, I'm still learning ansible, too).

If you want to define a condition for the role in your playbook you should use variables/facts for that.

Call your playbook using ansible-playbook --tags ami,launch_config,asg to skip the cleanup process. When defining the tags in your playbook you prevent the role from being executed when ami_lc_cleanup is defined as a tag on the commandline without any of the other three tags.

Upvotes: 1

Related Questions