Run tasks only when matches 2 tags

644 views
Skip to first unread message

Brandon Hilkert

unread,
Dec 5, 2013, 3:24:47 PM12/5/13
to ansible...@googlegroups.com
I tried something like this in a task, but it didn't work

  tags: "{{ 'production' && 'web' }}"


I have a task that uploads some web assets to S3. This is needed for a "web" tag, but only for "production". In staging, it's unnecessary, so I have tags like "web,production", and "web,staging" and I'd like for them to behave differently.

I was trying to avoid making specific tags like "web-production" and "web-staging"

Thanks!

Michael DeHaan

unread,
Dec 5, 2013, 3:41:51 PM12/5/13
to ansible...@googlegroups.com
Yeah you can't just make syntax up and expect it to mean something.

Sounds like you should just use a new play in the PB and simply use host groups to select.

-- Michael
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Gonzalo Servat

unread,
Dec 5, 2013, 11:09:29 PM12/5/13
to ansible-project
I actually ran into this requirement today. For example:

roles:
  - { role: network_config, tags: netconf }

Inside that role, I might have a bunch of tasks defined that I want to run only for a particular tag so ideally I'd like to be able to specify:

ansible-playbook .... -t netconf:ifcfg -t netconf:routes

... to run all tasks tagged with 'ifcfg' or 'routes', or I can choose to run all tasks in the network_config role:

ansible-playbook .... -t netconf

Can't do that with the current state of Ansible, right?

Michel Blanc

unread,
Dec 6, 2013, 1:12:38 AM12/6/13
to ansible...@googlegroups.com
On 06/12/2013 05:09, Gonzalo Servat wrote:
> I actually ran into this requirement today. For example:
>
> roles:
> - { role: network_config, tags: netconf }
>
> Inside that role, I might have a bunch of tasks defined that I want to
> run only for a particular tag so ideally I'd like to be able to specify:
>
> ansible-playbook .... -t netconf:ifcfg -t netconf:routes
>
> ... to run all tasks tagged with 'ifcfg' or 'routes', or I can choose to
> run all tasks in the network_config role:
>
> ansible-playbook .... -t netconf
>
> Can't do that with the current state of Ansible, right?

AFAIK no. As a workaround, in 'foo' role, I define tag 'foo' everywhere,
then 'foo:install', 'foo:config', 'foo:bar', 'install', 'config', 'bar'
... where appropriate, so I can execute a '-t config' to configure
roles only, or '-t foo:config' to configure foo role only, etc...

This is tedious but I don't think there is another way.

Set operations other than the current 'union' could be great (run all
tasks with tag X and Y, run all tasks except those with tags X) :
install only some roles, run only configuration steps for all roles
(skipping not apt/yum long operations), ...

Another neat feature would be to tag all tasks for a role (e.g. set the
tag 'foo' for all plays in role foo). I don't think this is actually
possible (?), something like (tasks/main.yml) :

- { include: install.yml, set_tags: [ 'foo', 'install' ] }
- { include: config.yml, set_tags: [ 'foo', 'config' ] }

M
--
Michel Blanc
{ :github => "@leucos", :twitter => "@b9m", :gpg => "0X24B35C22" }

Michael DeHaan

unread,
Dec 6, 2013, 8:21:46 AM12/6/13
to ansible...@googlegroups.com
I'd welcome pull requests to add boolean support for tags (though when adding it, it should be done such that --list-hosts and --list-tasks is also aware of it, so ... utility functions and that sort of thing)

This syntax would look just like the host spec:

--hosts "foo:&bar"

as of course

--hosts "foo:bar"

would mean foo or bar.


  


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Reply all
Reply to author
Forward
0 new messages