A quick tutorial on Ansible

Hello all! Today, we had an ansible session in #dgplug by trishnaguha. Before the session, I just had an idea about ansible, that it is used in sort of YAML deployment or something. But never really tried it before. It was a nice experience using ansible. Let me give you a quick wrap up of the session.

What is Ansible?

Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.

On a simple note you can automate tasks with ansible 🙂

Read about this tool from Ansible Documentation.

The Tutorial Begins

Prerequisite:

  • GNU/Linux
  • Ansible >= 2.6.0
  • SSH key-pair
  • openssh-server

Step by Step:

1. Run sshd (if it is not running):

$ sudo systemctl start sshd

2. Copy your ssh key to localhost

$ ssh-copy-id <username>@127.0.0.1

3. Run first ansible command

$ ansible all -i "localhost," -c local -m ping

It returned SUCCESS pong

4. Say hello to localhost 🙂

$ ansible all -i "localhost," -c local -m shell -a '/bin/echo hello'

It returned hello

5. Create a directory and go inside it:

$ mkdir demo; cd demo

6. Create a file here:

$ touch hosts

7. Put some content in the file:

 $ echo "localhost ansible_connection=local" >> hosts

This file hosts  is known as inventory.

The way we added localhost in our custom inventory, we call it ungrouped hosts.

See default hosts file of ansible in your system at /etc/ansible/hosts

8. Run ansible using our custom inventory.

$ ansible all -i hosts -m shell -a '/bin/echo hello'

It returned:  hello

9. Edit the inventory now (to make localhost a grouped host).

Put [webserver] (groub label) above localhost ansible_connection=local and the content of hosts file becomes.

[webserver]
localhost ansible_connection=loca

10. Run ansible again using group name.

$ ansible webserver -i hosts -m shell -a '/bin/echo hello'

Till now, all the ansible commands we have used are called ad-hoc commands, which is something that you might type in to do something really quick, but don’t want to save for later.

11. Now have a look at playbook

As Trishna informed us about “playbook” in her own words:

Till now we were passing all operations need to be executed via command line argument. We would not want to run these modules/task as argument every time we want to configure something as it will neither be feasible if we want to execute multiple operations at a time and we want the operations to be saved.

This is where the term “playbook” comes into play. Playbook is a YAML file that contains one or more plays where each play contains target host and performs a series of tasks on the host or group of hosts, specified in the play.

And a bit about modules:

Modules are the programs that perform the actual work of the tasks of a play. The modules referenced in the playbook are copied to the managed hosts. Then they are executed, in order, with the arguments specified in the playbook

Argument -m  in above ansible commands specified the module to use.

12. Create a playbook file (.yml)

$ touch demo.yml

13. Put content in demo.yml (care about indentation)

- hosts: webserver
  connection: local

  tasks:
  - shell: /bin/echo hello

Explanation of the content:

webserver name of group.
– Using the connection plugin we want to communicate with the host.
– The keyword tasks contains the operations that are to be performed on the destination host.
– Each operation <module (shell) with its arguments/options> are called task. We can add multiple tasks under this section.

14. Run playbook

$ ansible-playbook demo.yml -i hosts -v

ansible was the command we were using for ad-hoc commands, whereas ansible-playbook is the command for running playbook.

15. Edit playbook file

Now we tried 2 tasks in playbook, content of demo.yml becomes

- hosts: webserver
  connection: local

  tasks:
  - shell: /bin/echo hello

  - ping:

16. Run playbook again

$ ansible-playbook demo.yml -i hosts -v

17. Create a custom ansible.cfg

Certain settings in Ansible are adjustable via a configuration file: ansible.cfg

Default configuration can be found here: /etc/ansible/ansible.cfg

Let’s create our own custom ansible.cfg

$ touch ansible.cfg

18. Add following contents to ./ansible.cfg

[defaults]
inventory=hosts

Explaination of the content:

[defaults]  is the tag in ansible.cfg file, where we can pass certain configuration for our playbooks.
– Here inventory=hosts means we are telling ansible to use the inventory file “hosts”

19. Run playbook again (Note: we do not have -i hosts anymore)

$ ansible-playbook demo.yml -v

Ansible will always look for ansible.cfg first in the current directory then in the default directory.

20. Read more about ansible 🙂

Useful references

Conclusion

With this, we have reached the end of this post, overall I found ansible to be a great tool in which definitely there is so much to learn. I would like to thank Trishna Guha for giving an amazing session!

Thank you!

See you in the next post 🙂

 

 

2 thoughts on “A quick tutorial on Ansible

Leave a reply to Jason Braganza Cancel reply