How to start with ansible

In this article i will show you how to install and configure ansible and how to run a basic playbook.

In this article i will show you how to install and configure ansible and how to run a basic playbook.

We have two computers, the control computer and the target computer. The control computer will be used to install ansible and run all ansible commands from there.

The target computer will be used as a remote computer that we want to execute commands through ansible.

I assume that both computers run Centos 7.

Install Ansible on control computer

Type the following on control computer, those commands will install the epel repository and ansible its self.$ su - root
# yum install epel-release # yum install ansible

Create ansible user and configure passwordless login and sudo

Next step is to create the user ansible, enter the following commands on both computers, enter a password that you will remember.$ sudo useradd ansible
$ sudo passwd ansible

Now from the control computer we will create an ssh key and copy it to the target host to allow passwordless login, enter the following$ sudo -i -u ansible
$ ssh-keygen

Accept all the defaults of ssh-keygen by pressing enter to all questions.

Now copy the key to the target computer, it will ask for the password of the ansible user we created before.$ ssh-copy-id target-computer-name

Next step is to allow the ansible user on the target computer to run without sudo password, enter the following on target computer.$ sudo visudo

Add the following at the end of the file and save changesansible ALL=(ALL)       NOPASSWD: ALL

Create inventory on control computer

Now we need to create our inventory, the inventory is nothing more than a list of individual computers or grouped computers that we pass as a parameter to ansible in order to execute commands to them.

Type the following on the control computer$ mkdir ~/ansible
$ cd ~/ansible
$ vi inventory

Now in the editor enter the hostname of the target computer and save changeshostname-of-the-target-computer

In my case the hostname of the target computer is “workstation”

Verify that ansible can communicate with the target computer

To verify that we did everything good and the target computer is reachable from the control computer as the ansible user enter the following$ ansible -i ~/ansible/inventory all -m ping

if everything gone well you should see something like the following on the ansible output, SUCCESS indicates that the ping command executed successfully.workstation | SUCCESS => {
   "ansible_facts": {
       "discovered_interpreter_python": "/usr/bin/python"
   },
   "changed": false,
   "ping": "pong"
}

Note: the ping module of ansible HAS NOTHING similiar with the ping command that works on the network level.

Create and run your first playbook

Playbooks are nothing more than yaml files that describe what commands to run on the remote hosts. Enter the following and save file as ~/ansible/nginx_install.yml--- # install nginx on target host
- hosts: workstation
 become: yes
 tasks:
 - name: install nginx
   yum:
     name: nginx
     state: latest

Run the playbook with the ansible-playbook command$ cd ~/ansible
$ ansible-playbook -i /home/ansible/ansible/inventory /home/ansible/ansible/nginx_install.ymlPLAY [workstation] ***************************************************************************************************************************************************************************TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [workstation]TASK [install nginx] *************************************************************************************************************************************************************************
changed: [workstation]PLAY RECAP ***********************************************************************************************************************************************************************************
workstation                : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0[ansible@control ansible]$

Nottice in the output that changed=1, this means that ansible did a change on the target computer, which in our case was installing nginx.

Lets run the command again and see what happens$ ansible-playbook -i /home/ansible/ansible/inventory /home/ansible/ansible/nginx_install.ymlPLAY [workstation] ***************************************************************************************************************************************************************************TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [workstation]TASK [install nginx] *************************************************************************************************************************************************************************
ok: [workstation]PLAY RECAP ***********************************************************************************************************************************************************************************
workstation                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

From the output we can see that changed is equal to zero, this is because yum was allready installed so, ansible did not any action.

Explain the yml statements

I will not get into details how a yml file is formated, i will only explain the statements.--- # install nginx on target host
- hosts: workstation
 become: yes
 tasks:
 - name: install nginx
   yum:
     name: nginx
     state: latest

“hosts:workstation” run on workstation host

“become: yes” become root user and execute the commands

“tasks:” the tasks that we want to perform

“-name: install nginx” the name of the task

“yum:” use the yum module

“name: nginx” the name of the package to install is nginx

“state: latest” install the latest version available on the repository

Epilog

I hope you found my article interesting and usefull :)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…