# How to use roles from the command line?

**URL:** <https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390>\
**Category:** Ansible Project\
**Created:** [June 3, 2014, 3:06am UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390 "2014-06-03T03:06:07Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![john.mee](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@john.mee](https://forum.ansible.com/u/john.mee)\
**Post date:** [June 3, 2014, 3:06am UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/1 "2014-06-03T03:06:07Z")

</div>

I want to develop/test my new role on a virtual node thence apply just that one play, um role, against a host that really matters.  
How do I do that without having to edit any files between getting it finished and applying it to production?

# this doesn’t work…

% ansible-playbook -i testing roles/thisone?  
% ansible-playbook -i production roles/thisone?

–tags thisone

looked promising but it would mean going through everything applying tags, and then trying to remember what I called them all.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [June 3, 2014, 3:47am UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/2 "2014-06-03T03:47:37Z")

</div>

If you’re just testing that one role, why not make a simple playbook that calls just that?

- hosts: all  
roles:
- thisone

That should be all you need, since you’re pointing it at different inventory sources.

---

<div class="post-metadata">

**Author:** ![john.mee](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@john.mee](https://forum.ansible.com/u/john.mee)\
**Post date:** [June 3, 2014, 4:58am UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/3 "2014-06-03T04:58:37Z")

</div>

> without having to edit any files between getting it finished…

Methodological issue, not practical: it feels dirty to edit a file between the test and the application.  
Is creating and editing a new file the only way to get from the “tested” codebase to applying it to production hosts?

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [June 3, 2014, 5:35am UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/4 "2014-06-03T05:35:11Z")

</div>

john.mee@caradvice.com.au napisał:

> > without having to edit any files between getting it finished...
> 
> Methodological issue, not practical: it feels dirty to edit a file  
> between  
> the test and the application.  
> Is creating and editing a new file the only way to get from the  
> "tested"  
> codebase to applying it to production hosts?

I'm using a combination of tags and --limit. I tag roles at the play level:

- hosts: db  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;- {name: common, tags: common}  
&nbsp;&nbsp;- {name: postgresql, tags: postgresql}

And then use "ansible-playbook -i staging/inv -l db --tags postgresql" to only run that one role.

On the other hand, do you really want to run only that new role, and whole of it?  
It might be enough to just use --limit, or you might want to use more tags inside role (I'm using "bbb" for trying only some tasks when debugging).

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [June 3, 2014, 4:19pm UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/5 "2014-06-03T16:19:59Z")

</div>

A role can have default variables overridden, depending on what it is it could be a simple task or a more complicated group of tasks…

A play can include more than one role, and is used to tie the role(s) to one or more hosts. So you probably do need to create a playbook like the one that James listed… then in your case you use something similar to your original attempt…

ansible-playbook -i testing thisplay # Apply the role to all of the hosts in the testing inventory  
ansible-playbook -i production thisplay # Apply the role to all of the hosts in the production inventory  
ansible-playbook -i training thisplay -l justonehost # Apply the role to justonehost in the training inventory

No changes need to be made to any files between these different commands

I hope that this helps,

Adam

---

<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:** [June 6, 2014, 12:20pm UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/6 "2014-06-06T12:20:31Z")

</div>

“Methodological issue, not practical: it feels dirty to edit a file between the test and the application.”

I wouldn’t look at it this way.

To edit the role, perhaps, likely, yes.

But the idea that a playbook is simply mapping roles to hosts – and keeping playbooks at top level very simple – should be ok.

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [June 28, 2015, 6:23pm UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/7 "2015-06-28T18:23:30Z")

</div>

I agree with you. I find it a bit tedious to have to create a playbook just to apply a role.

You might be interested in this PR, which I just submitted:

[https://github.com/ansible/ansible/pull/11416](https://github.com/ansible/ansible/pull/11416)

---

<div class="post-metadata">

**Author:** ![Greg\_DeKoenigsberg1](https://avatars.discourse-cdn.com/v4/letter/g/838e76/32.png) [@Greg\_DeKoenigsberg1](https://forum.ansible.com/u/Greg_DeKoenigsberg1)\
**Post date:** [June 29, 2015, 4:27am UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/8 "2015-06-29T04:27:02Z")

</div>

Interesting.

Not sure what the impediments are. It would certainly make Galaxy  
testing easier, though.

Hmmmm.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 29, 2015, 12:51pm UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/9 "2015-06-29T12:51:11Z")

</div>

fact gathering is one issue I see, off the bat. The template module is  
hardcoded to error on adhoc, because of this.

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [June 29, 2015, 3:04pm UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/10 "2015-06-29T15:04:32Z")

</div>

I’m not following. Can you elaborate on this?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 2, 2015, 10:50pm UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/11 "2015-07-02T22:50:49Z")

</div>

in current ansible stable's template:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not self.runner.is\_playbook:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise errors.AnsibleError("in current versions of ansible,  
templates are only usable in playbooks")

I was looking of ways around this to allow for users to actually use  
template ad-hoc, i was thinking of chainging ansible -m setup|ansible  
-m template and consume the facts as 'extra vars', but I have not had  
time to get into that.

---

<div class="post-metadata">

**Author:** ![Dave\_Hatton](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@Dave\_Hatton](https://forum.ansible.com/u/Dave_Hatton)\
**Post date:** [July 3, 2015, 6:40am UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/12 "2015-07-03T06:40:16Z")

</div>

Maybe you have discountred something like this? I I have a simple stub playbook that I pass parameters to.

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [July 5, 2015, 6:33am UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/13 "2015-07-05T06:33:46Z")

</div>

Thanks, Dave! Yeah, that's what we do now. I have a very similar playbook that we use to run a single role and that one playbook keeps us from having a zillion little playbooks. It works but it feels a little weird and and perhaps too clever.

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [July 6, 2015, 6:52pm UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/14 "2015-07-06T18:52:24Z")

</div>

OK, well I ended building a little thing. I haven’t played with it much. Feedback welcome.

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [July 6, 2015, 6:53pm UTC](https://forum.ansible.com/t/how-to-use-roles-from-the-command-line/15390/15 "2015-07-06T18:53:02Z")

</div>

It would help I would guess if I gave the URL:

[https://github.com/msabramo/ansible\_role\_apply](https://github.com/msabramo/ansible_role_apply)
