Ansible in an easy way

rohit verma
Jul 10 · 2 min read

I have started my journey with ansible in the year 2016. I have a linux background and I am fluent in shell scripting. I found it hard to adopt ansible as a tool to replace bash.

To kickstart my coding skill with ansible , I have taken below steps.

> Start working on adhoc commands first, for instances

#ansible localhost -m setup // Will give you environment variable details of your localhost

#ansible -m copy -a “src=/tmp/test.txt dest=/tmp/test_copy.txt” ip_address // You don’t have to put entry in the host file in order to run adhoc command on single host.

> After getting used to of commands, then start working on small playbook to replace my daily scripting work.

> Small playbooks do not require to have complete ansible directory structure, with host file and main.yml can do the trick.

for example:

host file: hosts ( Containing 10 ip/hostname with name webservers )

ansible playbook to update centos os machines, excluding application packages.

To execute , you just have to type #ansible-playbook -i hosts main.yml

However everything is not so simple in production environment, you might need to authenticate with another user and you have rsa key of that user.

then, #ansible-playbook -i hosts main.yml -u user — private-key=rsa

Now lets just review what is in the playbook.

> Yaml file always start with three hyphen (-).

> gather_facts give you environment details of client host, in case you need to use them, for example: {{ ansible_hostname }} give you client hostname.

> become directive allow you to run task as a privilege user(root).

> serial stands for number of hosts concurrently run in the play.

> tasks specify the number of task which need to execute in the play.

Mentioned playbook is very basic and someone new to ansible can start kicking off as it mainly contain shell/command module. However same can be done via shell script but with little difficulty.