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.