How to install and configure ansible on Ubuntu 16.04?
1 Answers
I don't use Ansible
but I found this simple to follow tutorial from Digital Ocean, and these are the steps:
Installation:
sudo apt-add-repository ppa:ansible/ansible sudo apt-get update sudo apt-get install ansible
Configuring Ansible Hosts:
Edit
sudo nano /etc/ansible/hosts
:Since the tutorial uses dummy hosts these would be added in a real situation:
[group_name] alias ansible_ssh_host=your_server_ip
The group_name is an organizational tag that lets you refer to any servers listed under it with one word. The alias is just a name to refer to that server. So in our scenario, we are imagining that we have three servers we are going to control with Ansible. These servers are accessible from the Ansible server by typing:
ssh root@your_server_ip
We will assume that our servers' IP addresses are
192.0.2.1
,192.0.2.2
, and192.0.2.3
. We will set this up so that we can refer to these individually as host1, host2, and host3, or as a group as servers:[servers] host1 ansible_ssh_host=192.0.2.1 host2 ansible_ssh_host=192.0.2.2 host3 ansible_ssh_host=192.0.2.3
With our current settings, if we tried to connect to any of these hosts with Ansible, the command would fail (assuming you are not operating as the root user). This is because your SSH key is embedded for the root user on the remote systems and Ansible will by default try to connect as your current user. A connection attempt will get this error:
host1 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true }
On the Ansible server, we're using a user called demo. Ansible will try to connect to each host with ssh demo@server. This will not work if the demo user is not on the remote system. We can create a file that tells all of the servers in the "servers" group to connect using the root user. To do this, we will create a directory in the Ansible configuration structure called group_vars. Within this folder, we can create YAML-formatted files for each group we want to configure:
sudo mkdir /etc/ansible/group_vars sudo nano /etc/ansible/group_vars/servers
Add this in there:
--- ansible_ssh_user: root
- If you want to specify configuration details for every server, regardless of group association, you can put those details in a file at /etc/ansible/group_vars/all. Individual hosts can be configured by creating files under a directory at /etc/ansible/host_vars.
Using Simple Ansible Commands:
ansible -m ping all # Output: host1 | SUCCESS => { "changed": false, "ping": "pong" } host3 | SUCCESS => { "changed": false, "ping": "pong" } host2 | SUCCESS => { "changed": false, "ping": "pong" }
The "all" means all hosts. We could just as easily specify a group:
ansible -m ping servers
We could also specify an individual host:
ansible -m ping host1
We can specify multiple hosts by separating them with colons:
ansible -m ping host1:host2
The "shell" module lets us send a terminal command to the remote host and retrieve the results. For instance, to find out the memory usage on our host1 machine, we could use:
ansible -m shell -a 'free -m' host1 # Result: host1 | SUCCESS | rc=0 >> total used free shared buffers cached Mem: 3954 227 3726 0 14 93 -/+ buffers/cache: 119 3834 Swap: 0 0 0
More information:

- 36,677
-
Thanks for replying this but now i am getting and error when i run the ping command :- 192.168.1.78 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n", "unreachable": true } – user757423 Nov 08 '17 at 08:31
-
did you set up the root access as stated in that tutorial? – George Udosen Nov 08 '17 at 09:34
-
Yes I did it that also tried changing and modifying the ssh-keygen test on the client system those are workoing fine but still the issue persist. – user757423 Nov 08 '17 at 12:17