Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
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 at 15:28

Sure, you should be able to do something like:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.