Cassie
Cassie

Reputation: 3099

"Could not resolve hostname" Ansible

I have created my first ansible playbook according to this tutorial, so it looks like this:

---
- hosts: hostb
  tasks:
      - name: Create file
        file:
            path: /tmp/yallo
            state: touch

- hosts: my_hosts
  sudo: yes
  tasks:
      - name: Create user
        user:
            name: mario
            shell: /bin/zsh

      - name: Install zlib
        yum:
            name: zlib
            state: latest

However, I can not figure out which hosts I should put into my hosts file. I have something like this for now:

[my_hosts]
hostA
hostB

Obviously, it is not working and I get this:

ssh: Could not resolve hostname hostb: Name or service not known

So how should I change my hosts file? I am new to ansible so I would be very grateful for some help!

Upvotes: 4

Views: 24593

Answers (2)

Sruthi
Sruthi

Reputation: 1

ansible is case sensitive host name in your inventory file is hostB and in your playbook is hostb i think way its showing " Name or service not known" error

change your host name in the playbook to hostB

Upvotes: 0

Prav
Prav

Reputation: 2885

Ok so the Ansible inventory can be based on following format:

  • HostName => IP Address
  • HostName => DHCP or Hosts file hostname reference localhost/cassie.local
  • Create your own alias => hostname ansible_host=IP Address
  • Group of hosts => [group_name]

That is the most basic structure you can use.

Example

# Grouping
[test-group]

# IP reference
192.168.1.3

# Local hosts file reference
localhost

# Create your own alias
test ansible_host=192.168.1.4

# Create your alias with port and user to login as
test-2 ansible_host=192.168.1.5 ansible_port=1234 ansible_user=ubuntu

Grouping of hosts will only end when the end of file or another group detected. So if you wish to have hosts that don't belong to a group, make sure they're defined above the group definition.

I.E. everything in the above example is belong to test-group, and if you do following; it will execute on all of the hosts:

ansible test-group -u ubuntu -m ping


Upvotes: 5

Related Questions