When to call a specific task from a role?

43 views
Skip to first unread message

Gilberto Valentin

unread,
Mar 14, 2016, 4:24:55 PM3/14/16
to Ansible Project
I am creating a role with ansible-galaxy init role_name. One of the tasks in that role will be to install_app.yml and another to remove_app.yml (one to install and one to remove). Is it possible to call one task vs the other within that role or do I need to create a whole new role for that specific task?

Timothy Appnel

unread,
Mar 14, 2016, 5:31:40 PM3/14/16
to Ansible Project
This is a question I've pondered in my own work. There is no one specific or recommended way Ansible prescribes for doing this. What follows are my own personal thoughts and mine alone.

You can do separate role as you suggest. Not ideal because it puts to much distance between closely related functionality for my liking.

You could tag tasks one or the other -- however you must remember to pass a tag. No sure this is practical in that it's too easy to mess up.

You can implement something with some combinations of params/variables, conditionals, includes and/or blocks. This is one I think has the most promise though the number of ways this could be implemented are pretty extensive in number. 

<tim/>


Timothy Appnel
Principal Architect
Ansible by Red Hat

+1 718.404.6429 | ansible.com 

GitHub: tima

Twitter: @appnelgroup


On Mon, Mar 14, 2016 at 4:24 PM, Gilberto Valentin <gvale...@gmail.com> wrote:
I am creating a role with ansible-galaxy init role_name. One of the tasks in that role will be to install_app.yml and another to remove_app.yml (one to install and one to remove). Is it possible to call one task vs the other within that role or do I need to create a whole new role for that specific task?

--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/77c726a2-29ea-4898-ab05-32b82d5af207%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gilberto Valentin

unread,
Mar 15, 2016, 10:06:56 AM3/15/16
to Ansible Project
Thanks for the input. I think I am going to start by creating two separate roles (one for install and one for removal). When I gain enough knowledge of Ansible, I'll add some conditions within one role to execute one task vs the other.

Dick Davies

unread,
Mar 15, 2016, 10:05:23 PM3/15/16
to ansible list
I've done things like this before with some success:


playbook:$ cat roles/ci/tasks/main.yml
---

- include: deploy.yml
when: ci_rollback is not defined or ci_rollback == ""

- include: rollback.yml
when: ci_rollback is defined and ci_rollback != ""
playbook:$

and then if you need to uninstall, just set ci_rollback=yowza during
ansible invocation.
Reply all
Reply to author
Forward
0 new messages