We start with installing Ansible using root.


step 1 - run "yum install ansible" to install it on one machine with CenOS 7;

step 2 - configuring ansible hosts

Open the file /etc/ansible/hosts, in this file, add:

[servers]
host1 ansible_ssh_host=user_name@server_ip
host2 ansible_ssh_host=user_name@server_ip

We test ansible to connect to host1 and host2 using command:

ansible -m ping all

However, it failed and presented the error:

host1 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
    "unreachable": true
}

We addressed this problem by generating private and public keys on this server, and copying the public key to the server that ansible attempted to connect.

On local computer, generate a SSH key pair by the following command:

ssh-keygen

The key pair will be created within ~/.ssh directory.

The password can be neglected.

To copy the public key to the server, typing 

ssh-copy-id username@remote_host

To test the ssh connection, run

ssh username@remote_host

After SSH key authentication is established, we come back to ansible and run

ansible -m ping all

Now we can see that

host1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Reference

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-centos-7