Priyanka Saggu I speak here {^_^}

The Ansible progress so far!

August 2, 2019

[Note: This post will cover the work progress from last 2 days, i.e. August 1st and 2nd.]

I am learning ansible now. It was not a really smooth passage to the point where I am right now in ansible. But today, with literally lots of efforts, I finally managed to run some first few ansible-playbooks on a remote node.

(Maybe, it's not ansible which is hard to learn, but my curiosity/stupidity to jump into multiple things at the same time, made it an absolute pain in the brain.)

From past 4-5 days, I was just struggling to get myself a proper setup, where I can test my ansible-playbooks. And it was not happening only. It may be because I was thinking like I can experiment on multiple things at the same time (like working on Docker containers for the VMs). If I try to put my problems in a chronological order, it will look something like this:

  • The first thought was to get myself an alternative solution for GCP VM instances and I finally ended up working with Docker containers. That was an addition to me, but a real exhausting frustration at the same time. For continuous 2 days, I was just recursively downloading the minimal dependencies and packages on the bare docker containers. And I finally decided to give up after Kushal's advice to not work on multiple nodes, but just start with one at a time. And I thank him no end for this. :)
  • In between, there was a time when I had a temporary setup of 4 VM nodes using docker containers. And I was able to SSH into all the three "end nodes" from the one main "control node". I assumed that it will work for ansible as well, but it proved me wrong. I was trying to use ansible "ping module" to ping the other three nodes. But sadly, it was not happening in Docker containers. Everytime, the nodes were unreachable. So, that was the moment when I finally decided to leave docker behind.
  • After docker, I came to my usual GCP VMs. And yeah, the "ping module" worked this time. But again while running ansible playbooks for automating stuffs like nginx-setup and apache-setup, I met with another problem of "SSH aborting with too many authentication failures" and "Authentication or permission failure". It was ultimately resulting into nothing but unreachable nodes. After lots of tinkering on web and amendments in ssh and ansible config files. I finally got the one-liner solution for this. And this time, the happiness was no end. :D

# In your ansible.cfg change "remote_temp" variable to the following.

remote_tmp = /tmp/.ansible-${USER}/tmp

  • So, now, the nodes were reachable through ansible and I was trying to write plays for setting up apache in the remote node (Ubuntu 18.04 machine). The next error came while using "apt" module and it was:-

Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)\nE: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend)

And I did exhaustive search and tried almost every possible solution on web to check this, but it didn't happened to be solved. So, finally had to change the playbook.

Now, after changing the playbook and adding certain other values, like setting up "become" and "become_method" values to provide sudo permissions. It happened.

Finally the ansible-playbook did the work and made changes on the remote node as it was supposed to do.πŸŽ‰ πŸŽ‰πŸŽ‰ πŸŽ‰


For everyone who want to start with ansible, I am compiling up the resources which I am using.

  1. Ansible Documentation (official documentation)
  2. YAML Sntax (Very important for starting writing ansible-playbooks. I found this extremely useful.)
  3. Ansible Tutorial for Beginners.
  4. How to Install and Configure β€˜Ansible’ Automation Tool for IT Management – Part 1
  5. How to Use Anisble Playbooks to Automate Complex Tasks on Multiple Remote Servers – Part 2
  6. How to Automate Simultaneous WordPress Deployments in Multiple Linux Servers Using Ansible – Part 3
  7. Getting started: Writing your first playbook.
  8. ansible-examples.

Besides, adding my first two working playbooks here. :)

  • The most basic and simple one. [Just to ping all the hosts and check if they respond or not. ]

# playbook.yml
---
- hosts: webservers
  tasks:
    - action: ping
...

# To run an ansible playbook, use the command below.
$ ansible-playbook playbook.yml

#OUTPUT (because I have cowsay, so you will see my output from the ASCII cows. :) )

 ___________________
< PLAY [webservers] >
 -------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 ________________________
< TASK [Gathering Facts] >
 ------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [priyankasaggu119@35.184.187.20]
 _____________
< TASK [ping] >
 -------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [priyankasaggu119@35.184.187.20]
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

priyankasaggu119@35.184.187.20 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

  • And one for the nginx setup.

# nginx.yml
---
- hosts: webservers
  sudo: yes
  tasks:
    - name: Installs nginx web server
      apt: pkg=nginx state=present update_cache=true
      notify:
        - start nginx

  handlers:
    - name: start nginx
      service: name=nginx state=started
...

$ ansible-playbook nginx.yml

#OUTPUT
 ___________________
< PLAY [webservers] >
 -------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 ________________________
< TASK [Gathering Facts] >
 ------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [priyankasaggu119@35.184.187.20]
 __________________________________
< TASK [Installs nginx web server] >
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 [WARNING]: Could not find aptitude. Using apt-get instead

ok: [priyankasaggu119@35.184.187.20]
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

priyankasaggu119@35.184.187.20 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    
ignored=0   

That's all for ansible today.


August 2 is Daddy's birthday.

HAPPY BIRTHDAY PAPA :D

and there was an interview too. (I think I will reach a count of some 35-40 in some time.)