Ansible deployment and testing

Manoj Chandran
2 min readJul 27, 2019

--

Objectives : To Deploy and Test an Ansible host machine.

This is a simple blogpost to demonstrate Ansible installation and running playbook. For the purpose of explaining the installation and deployment, I have created two CentOS machine in cloud. Assigned one as control host and second one as workstation. You can follow through the commands for the deployment.

First step is to get the package and install. Run the following commands on the control host:

:~$ sudo yum install epel-release
:~$ sudo yum install ansible

Once the installation is complete, create an `ansible` user on both the control host and workstation.
User will help to set the privileges in control host and workstation. On each host, run the noted commands below.

:~$ sudo useradd ansible
:~$ sudo passwd ansible

Ansible doesn’t have any agent installed in the workstation. Simple SSH connection is used by control host to communicate with workstation. We need to configure a pre-shared key for Ansible that allows the user to log in from `control` to `workstation` without a password.

We will be using previously created ‘ansible’ user to establish an ssh connection and control. Assuming you are logged into control host, run the following commands providing the appropirate passwords when prompted and default options otherwise:

:~$ sudo -i -u ansible 
:~$ ssh-keygen (accept default options by pressing enter )
:~$ ssh-copy-id workstation (provide ansible user a password)
:~$ logout

Once the ‘ansible’ user in set up in host, configure the Ansible user on the workstation host so that Ansible may sudo without a password.

Log into the workstation host and run the following commands:

:~$ sudo visudo
Add text at the end of the file that is opened:
ansible ALL=(ALL) NOPASSWD: ALL
Save file:
(:wq in vim)

Now we will create a simple inventory consisting of the workstation.

note : The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate.

Create a simple inventory in `/home/ansible/inventory` consisting of only the `workstation` host. On the control host as the ansible user run the following commands:

:~$ vim /home/ansible/inventory Add the below text to the file and save.[server]
workstation

We have set for ansible host and workstation, will create an ansible playbook to install git. On the control host as the ansible user run the following commands:

note : The playbook is the core component of any Ansible configuration. An Ansible playbook contains one or multiple plays, each of which define the work to be done for a configuration on a managed server.

Add the following text to the file:
:~$ vim /home/ansible/inventory

:~$ cat <<EOF > ./git-install.yml
— — — # install git on target host
— hosts: workstation
become: yes
tasks:
— name: install git
yum:
name: git
state: latest
EOF
:~$ ansible-playbook -i /home/ansible/inventory /home/ansible/git-install.yml

Hope this was helpful, continue leaning by experimenting more on Ansible.

--

--

Manoj Chandran

Software developer and Technology enthusiast from India.