# Adding ansible-playbook --list-tags myplaybook.yaml

**URL:** <https://forum.ansible.com/t/adding-ansible-playbook-list-tags-myplaybook-yaml/14726>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [March 7, 2014, 3:31pm UTC](https://forum.ansible.com/t/adding-ansible-playbook-list-tags-myplaybook-yaml/14726 "2014-03-07T15:31:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Nelson](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Michael\_Nelson](https://forum.ansible.com/u/Michael_Nelson)\
**Post date:** [March 7, 2014, 3:31pm UTC](https://forum.ansible.com/t/adding-ansible-playbook-list-tags-myplaybook-yaml/14726/1 "2014-03-07T15:31:45Z")

</div>

Hi!

Just a heads-up that I’m thinking of adding a --list-tags option to ansible-playbook. Let me know if there’s a reason why that wouldn’t be a good idea, or it’s not needed etc.

My use-case is that I’d like to know in advance whether a tag is included in the playbook, rather than running the playbook and erroring. I can currently work around this by parsing the error output of `ansible-playbook --tags random-hash myplaybook`, but I’d like a nicer way 🙂

More background for anyone interested:

I’m using ansible to make writing juju-charms [1] much more fun. juju has a number of lifecycle hooks that are run as a service relates to other services (eg., an app server to a database backend). These hooks can be written in anything you like, but we’ve created some helpers so that when a specific hook is called, it just results in a local ansible playbook being run using the --tags hook-name . So all the state can be declared with all the goodness of playbooks and/or roles, rather than bash scripts or python code.

Currently as a charm author, you need to register the ansible-supported hook names by default [2], but I’d like to make that transparent.

Any feedback welcome, I’ll get started on a pull-request if I don’t hear back.  
Thanks

[1] [https://juju.ubuntu.com/charms/](https://juju.ubuntu.com/charms/)  
[2] eg [https://github.com/absoludity/charm-bootstrap-ansible/blob/master/hooks/hooks.py](https://github.com/absoludity/charm-bootstrap-ansible/blob/master/hooks/hooks.py)  
[3] [http://micknelson.wordpress.com/2013/11/08/juju-ansible-simpler-charms/](http://micknelson.wordpress.com/2013/11/08/juju-ansible-simpler-charms/)

---

<div class="post-metadata">

**Author:** ![Michael\_Nelson](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Michael\_Nelson](https://forum.ansible.com/u/Michael_Nelson)\
**Post date:** [March 7, 2014, 3:52pm UTC](https://forum.ansible.com/t/adding-ansible-playbook-list-tags-myplaybook-yaml/14726/2 "2014-03-07T15:52:27Z")

</div>

Actually, I’ve just taken a look at the code and seen that, given that I’m already using python helpers for the ansible support, I could just use ansible.playbook.Play().compare\_tags() directly. So no change needed 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_Nelson](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Michael\_Nelson](https://forum.ansible.com/u/Michael_Nelson)\
**Post date:** [April 16, 2014, 7:38am UTC](https://forum.ansible.com/t/adding-ansible-playbook-list-tags-myplaybook-yaml/14726/3 "2014-04-16T07:38:42Z")

</div>

Just getting back to this - it seems quite complicated to get the tags  
for a playbook... here's the helper function that I have:

{{{  
def get\_tags\_for\_playbook(playbook\_path):  
&nbsp;&nbsp;&nbsp;&nbsp;"""Return all tags within a playbook."""  
&nbsp;&nbsp;&nbsp;&nbsp;# utils imported first to avoid circular import failure.  
&nbsp;&nbsp;&nbsp;&nbsp;import ansible.utils  
&nbsp;&nbsp;&nbsp;&nbsp;import ansible.callbacks  
&nbsp;&nbsp;&nbsp;&nbsp;import ansible.playbook  
&nbsp;&nbsp;&nbsp;&nbsp;stats = ansible.callbacks.AggregateStats()  
&nbsp;&nbsp;&nbsp;&nbsp;callbacks = ansible.callbacks.PlaybookRunnerCallbacks(stats)  
&nbsp;&nbsp;&nbsp;&nbsp;runner\_callbacks =  
ansible.callbacks.PlaybookRunnerCallbacks(stats)  
&nbsp;&nbsp;&nbsp;&nbsp;playbook = ansible.playbook.PlayBook(playbook=playbook\_path,

callbacks=callbacks,

runner\_callbacks=runner\_callbacks,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stats=stats)  
&nbsp;&nbsp;&nbsp;&nbsp;myplay = ansible.playbook.Play(playbook, ds=playbook.playbook[0],

basedir=os.path.dirname(playbook\_path))

&nbsp;&nbsp;&nbsp;&nbsp;\_, playbook\_tags = myplay.compare\_tags()  
&nbsp;&nbsp;&nbsp;&nbsp;playbook\_tags.remove('all')  
&nbsp;&nbsp;&nbsp;&nbsp;return playbook\_tags  
}}}

Perhaps it is worth a feature request - let me know if this should be  
easier or if I've missed something above?

Thanks.  
-Michael

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 17, 2014, 2:19am UTC](https://forum.ansible.com/t/adding-ansible-playbook-list-tags-myplaybook-yaml/14726/4 "2014-04-17T02:19:02Z")

</div>

So yeah.

There’s been interest in adding such before, my requirement is usually that it uses the Playbook API to do these things.

Some things are going to require a minor bit of refactoring to make some of that later.

I generally want to avoid special code specific to /usr/bin/ansible commands that are not contained in playbook API code.

I’d generally think needing the stats class would be “not it”.

Workaround for now has been to just specify an invalid tag and it will list available tags, so that is pretty close, but it happens  
as a result of a “live” run, so I understand that’s not quite what you want.

Example:

ansible-playbook foo.yml --list-tags NotATagNameJustShowMeTheTags

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 17, 2014, 2:19am UTC](https://forum.ansible.com/t/adding-ansible-playbook-list-tags-myplaybook-yaml/14726/5 "2014-04-17T02:19:18Z")

</div>

Sorry that --list-tags was meant to be just “–tags”

ansible-playbook foo.yml --tags NotATagNameJustShowMeTheTags

---

<div class="post-metadata">

**Author:** ![Andriy\_Podanenko](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Andriy\_Podanenko](https://forum.ansible.com/u/Andriy_Podanenko)\
**Post date:** [January 19, 2015, 4:06pm UTC](https://forum.ansible.com/t/adding-ansible-playbook-list-tags-myplaybook-yaml/14726/6 "2015-01-19T16:06:19Z")

</div>

Are there any activity for this ?  
It is really must have feature because of impossibility to get all tags within huge playbooks, developed by large team.

четвер, 17 квітня 2014 р. 05:19:18 UTC+3 користувач Michael DeHaan написав:
