1

After going thru some reading, I just created my first Ansible Playbook to ping hosts.

First I made sure to create the SSH keys for the admin server and copy this to the target servers:

$ # ssh-keygen -t rsa -b4096

# ssh-copy-id administrator@nap-01.vm

# ssh-copy-id administrator@nap-02.vm

Then I added target servers to the /etc/ansible/hosts file:

[testing]
nap-01.vm
nap-02.vm

First I tested using the ping module. All went good:

$ ansible -i /etc/ansible/hosts testing -m ping -u administrator
nap-02.vm | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
nap-01.vm | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

I have several users in the testing servers, so I'm using the administrator user account.

Next, I tried to do the same action (ping) using a playbook:

$ cat test.yml
---
- name: "Get ping response"
  hosts: testing
  tasks:
  - action: ping
    register: hello
  - debug: msg="{hello.stdout}"


But after running it:

$ ansible-playbook test.yml

I get the following output:

PLAY [Get ping response] *******************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************
fatal: [nap-01.vm]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: oscar@nap-01.vm: Permission denied (publickey,password).", "unreachable": true}
fatal: [nap-02.vm]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: oscar@nap-02.vm: Permission denied (publickey,password).", "unreachable": true}

PLAY RECAP *********************************************************************************************************************************************************************************
nap-01.vm                  : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0
nap-02.vm                  : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

So it seems like the remote is not allowing to connect via SSH and the permission is being denied. However, since ping worked via the module, I'm guessing perhaps I'm missing something at YAML file that indicates to connect to the "administrator" user like in the first test.

Any advice on this issue would be appreciated.

Thank you.

olg32
  • 305
  • 2
  • 6
  • 19

2 Answers2

2

The error is

Failed to connect to the host via ssh: oscar@nap-01.vm

There are a couple of options how to connect as administrator@nap-01.vm*

1) Specify the remote_user in the command line

$ ansible-playbook test.yml -u administrator

2) Specify the remote_user in the playbook

- name: "Get ping response"
  hosts: testing
  remote_user: administrator
  tasks:
  ...

3) Specify the ansible_user in the inventory

[testing]
nap-01.vm ansible_user=administrator
nap-02.vm ansible_user=administrator

4) Set ANSIBLE_REMOTE_USER


Notes
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

From your 1st action :

ansible -i /etc/ansible/hosts testing -m ping -u administrator

You are telling ansible to user user "administrator" and it is able to connect to the host as the required keys are configured.

When you are running a playbook, it is saying :

fatal: [nap-01.vm]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: oscar@nap-01.vm: Permission denied (publickey,password).", "unreachable": true}

This is because, it is using user "oscar" to connect to the target host. Pass "-u administrator" to your playbook. It should work.

Or if you want to run all ansible tasks using "administrator", change the settings in ansible.cfg file. Look for "remote user" entry as below :

# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
#remote_user = root
saurabh14292
  • 1,281
  • 2
  • 10
  • 12