Pages

Thursday, January 21, 2016

Getting Ansible to Work


In the previous article we have seen what is Ansible and installing Ansible. In this article we will see how we can start working with Ansible. This article provides you with the basic Usage and commands available. For the testing purpose we will be creating a Vagrant virtual machine and then use the Ansible tool on that machine.

1) Create a Vagrant Machine - For more details on Configuring Vagrant, check here.   For the virtual box that we configure we use the below vagrant file,

[root@vx111a docker]# cat Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "CentOS2"
  config.vm.box_url = "https://saleseng.s3.amazonaws.com/boxfiles/CentOS-6.3-x86_64-minimal.box"
  config.vm.host_name = "dev.foohost.vm"
  config.vm.network "private_network", ip: "172.16.202.96"

 config.vm.provider :virtualbox do |vb|
     vb.name = "foohost"
 end
end

Once the file is saved as Vagrantfile , run the “vagrant up” command to start the Virtual box. Once the Virtual machine is up and running, the machine is assigned with the IP address “172.16.202.96”.

2) Generate SSH keys - Once the Virtual Machine is up and running, Lets get the Status of the Virtual machine whether it is Up and running using the Ping Command. For this we will use the Ansible.

Before making Ansible to get the status of the Remote machine , we need to configure the SSH keys so that the Host machine can connect to the Remote machine. For this run the “ssh-keygen” command.

[root@vx111a docker]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
8e:d3:a5:5d:f6:a4:7f:5b:b0:e1:1e:5e:f3:3c:16:63 root@vx111a.jas.com
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|                 |
|        S . o +  |
|       + + o = E |
|      o + . . *.=|
|       .     + *=|
|              =o=|
+-----------------+

Once this is done , we will now have the Public key and private keys available. The public key is available in the ~/.ssh/ id_rsa.pub file.

3) Copy the SSH key - Copy the contents of the file and add that to the authorized_keys file in the remote machine (vagrant Virtual machine ) .For this use the “vagrant ssh” command which will allow you to login to the running Virtual machine that we created.
Create a authorized_keys file under ~/.ssh/ location ( if not available) and copy the above public key contents to the file.

4) Ping the remote Machine – Now we have the host machine and remote machine with SSH setup we can now start using Ansible. For this create a hosts definition file. The default host definition is located in /etc/ansible/hosts. You can use a custom hosts definition (located outside /etc, for example) by defining them elsewhere and passing the -i [host-file location] parameter to the Ansible command. We will create a sample Host file as

[root@vx111a docker]# cat hosts
[servers]
172.16.202.96

[dev]
172.16.202.96

Now in the host file we have defined the IP address of the Server that we want to manage ( in this case it the vagrant Virtaul box).

Now once the host file Is configured , run the Ansible command as,

[root@vx111a docker]# ansible -i $PWD/hosts all -m ping -u vagrant
172.16.202.96 | success >> {
    "changed": false,
    "ping": "pong"
}

Lets take a look at the command that we ran above,
  • ansible is the command which runs one task at a time.
  • all tells Ansible to run this task on all the hosts in the inventory file ( Host file).
  •  -m means “use this Ansible module”, and ping is the name of the module. The ping module contacts the host and proves that it’s listening.
  • -u means “use the user that passed after the –u”.  in this case it is vagrant.

So we are running a Ansible command on the remote machine to get the Ping Status by using the more user vagrant.

More to Come in the next articles. Happy learning 

1 comment :