2

I'm trying to get some very basic things working with Ansible 2.1.1.0 on OS X.

My targets are all Ubuntu 16.04, so the very first thing I have to do is get it to install python2.7, as without that even 'ping' won't work.

I have an inventory set up like this in servers:

[dbservers]
192.168.x.x set_hostname=db

[webservers]

[servers:children]
dbservers
webservers

And this is my play in init.yml:

---
- hosts: servers
  gather_facts: False
  become: yes
  tasks:
     - name: apt-get update
       raw: /usr/bin/apt-get update -qq
     - name: Install python 2.7
       raw: test -e /usr/bin/python || /usr/bin/apt-get install -q -y python2.7 python-simplejson

I check that it's selecting the right hosts by specifying the dbservers group, which is a subset of servers (there is only 1 anyway), which looks right to me:

# ansible dbservers -i servers --list-hosts
  hosts (1):
    192.168.x.x

The command I'm running is this:

# ansible dbservers -i servers init.yml

and that's when I get the error ERROR! Missing target hosts. I don't understand how it can have hosts and no hosts at the same time! What am I doing wrong?

5

You are trying to run a playbook with the ansible command. The ansible command is for add-hoc commands.

Try executing your playbook with ansible-playbook command:

ansible-playbook -i servers init.yml
| improve this answer | |
  • Thanks, I knew it had to be something simple! It didn't occur to me that these would be handled separately. I had been wondering how conflicts between CLI targets and hosts directives were resolved - and the answer is, they're not! – Synchro Sep 2 '16 at 11:15
  • Your last paragraph is wrong: You can do anything with raw, and installing the Python prerequisites is a very common thing to do with it. – Michael Hampton Sep 2 '16 at 14:26
  • You are right. It was even on the page I linked to. I deleted that paragraph. My fault. – Henrik Pingel Sep 2 '16 at 14:52
-1

You have a [servers:children] section, but no [servers] section in your hosts file. So when you set hosts: servers there is no matching group.

| improve this answer | |
  • That doesn't help. It still fails if I specify all, and doesn't explain why it matches hosts when I ask it to --list-hosts. – Synchro Sep 2 '16 at 7:36
  • 2
    Also, this is not true: the docs show that setting a group property creates a group implicitly. – Synchro Sep 2 '16 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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