I have many Ansible playbooks that will get included in much larger playbooks. For example:

- name: Add user me
  user: name=me comment="Me" uid=9999 groups=somegroup
- name: Add my ssh public key
  authorized_key: user=me key="{{ lookup('file', 'id_rsa.pub') }}"

Is there a way pass in a hostname and run this as a standalone task from the command line, or do I need to create another playbook to run these two tasks by themselves?

5 Answers 5


There are two requirements to achieve this:-

  1. the host you're trying to target must be in an inventory file
  2. you need to tag the tasks in the role you want to run

    - name: Add user me
      user: name=me comment="Me" uid=9999 groups=somegroup
      tags: this_role
    - name: Add my ssh public key
      authorized_key: user=me key="{{ lookup('file', 'id_rsa.pub') }}"
      tags: this_role

Then you can:-

ansible-playbook foo.yml -i hosts -t this_role --limit host.example.com

Not that this is still running the playbook that contains the complete play, but it's limiting the tasks that run to just those that match the tag. The remaining tasks will be skipped.

  • The docs and forum posts do give the impression that running a small piece of a larger whole is the answer (as opposed to running individual, self-contained parts).
    – Pyzo
    Feb 8, 2014 at 15:28

Both user and authorized_key are modules. So you can call these with ansible -m from any script

ansible all -i host.example.com, -m user -a 'name=me comment="Me" uid=9999 groups=somegroup'
ansible all -i host.example.com, -m authorized_key -a "user=me key=\"{{ lookup('file', 'id_rsa.pub') }}\""

Note the trailing comma after the hostname.

Another approach would be to use include or include_tasks as in

ansible all -i host.example.com, -m include_tasks -a /path/to/some/taskfile.yml

And using include_role could look like

ansible all -i localhost, -c local --playbook-dir /path/to/roles -m include_role -a name=test

Although include might become deprecated in the future (in favour of include_tasks or include_role)


  • ...
  • This module will still be supported for some time but we are looking at deprecating it in the near future.

Sure, you should be able to do something like:

$ ansible-playbook foo.yml -i hosts -u user --limit host.example.com
  • When I run the above playbook like this I get ERROR: hosts declaration is required
    – Pyzo
    Feb 5, 2014 at 23:23

I ran into this problem as well.

I worked around the problem by creating a small runner playbook, which I then call with a paramater.

> tree
├── ansible.cfg
├── inventory
├── taskrunner.yml
├── tasks
└── hello.yml

> cat taskrunner.yml:
  - hosts: all
    - name: "Runner will try to run task: {{ task }}"
      include: "{{ task }}"

> cat tasks/hello
  - name: print hello
    shell: echo hello world

> ansible-playbook taskrunner.yml -e task=tasks/hello.yml

You can create an inventory and assign variables to subset of specific group of hosts, then use conditions under tasks you would execute when this condition is met based on the defined variables.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .