Laureline's Wiki

Laureline's Wiki

Lab 07: Configuration Management

This is an old revision of the document!


Lab 07: Configuration Management

By Michaël Rohrer & Laureline David

Task 1: Install Ansible

DONE

Yoda:~ frederic$ ansible --version
ansible 2.3.0.0
  config file = 
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Apr 23 2017, 16:50:35) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]

Task 2: Create a VM on Amazon Web Services

DONE

ssh -i id_rsa ubuntu@ec2-54-147-90-78.compute-1.amazonaws.com
...
ubuntu@ip-172-31-38-253:~$

Task 3: Configure Ansible to connect to the managed VM

DONE

Yoda:playbooks frederic$ ansible testserver -i hosts -m ping
Enter passphrase for key '/Users/frederic/.ssh/id_rsa': 
Enter passphrase for key '/Users/frederic/.ssh/id_rsa': 
testserver | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
Yoda:playbooks frederic$ ansible testserver -m ping

Enter passphrase for key '/Users/frederic/.ssh/id_rsa': 
testserver | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
Yoda:playbooks frederic$ ansible testserver -m command -a uptime
Enter passphrase for key '/Users/frederic/.ssh/id_rsa': 
testserver | SUCCESS | rc=0 >>
 12:44:43 up 31 min,  2 users,  load average: 0.00, 0.01, 0.05

Task 4: Install web application

DONE

Yoda:playbooks frederic$ ansible webservers -m ping
Enter passphrase for key '/Users/frederic/.ssh/id_rsa': 
testserver | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
Yoda:playbooks frederic$ ansible-playbook web.yml

PLAY [Configure webserver with nginx] **********************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
Enter passphrase for key '/Users/frederic/.ssh/id_rsa': 
ok: [testserver]

TASK [install nginx] ***************************************************************************************************************************************************************************************
changed: [testserver]

TASK [copy nginx config file] ******************************************************************************************************************************************************************************
changed: [testserver]

TASK [enable configuration] ********************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy index.html] *************************************************************************************************************************************************************************************
changed: [testserver]

TASK [restart nginx] ***************************************************************************************************************************************************************************************
changed: [testserver]

PLAY RECAP *************************************************************************************************************************************************************************************************
testserver                 : ok=6    changed=4    unreachable=0    failed=0

Task 5: Test Desired State Configuration principles

Return to the output of running the web.yml playbook the first time. There is one task that Ansible marked as ok.

Yoda:playbooks frederic$ ansible-playbook web.yml

PLAY [Configure webserver with nginx] **********************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [testserver]

TASK [install nginx] ***************************************************************************************************************************************************************************************
changed: [testserver]

TASK [copy nginx config file] ******************************************************************************************************************************************************************************
changed: [testserver]

TASK [enable configuration] ********************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy index.html] *************************************************************************************************************************************************************************************
changed: [testserver]

TASK [restart nginx] ***************************************************************************************************************************************************************************************
changed: [testserver]

PLAY RECAP *************************************************************************************************************************************************************************************************
testserver                 : ok=6    changed=4    unreachable=0    failed=0

Which one?

The task that Ansible marked as ok are the followings :

  1. TASK [Gathering Facts]
  2. TASK [enable configuration]

Do you have a possible explanation?

The garhering facts task is probably a task in which ansible read the configuration file to gather the information needed to do its job.

The enable configuration task creates a symbolic link with a file contained in the sites-available folder to enable it. The default file targeted by the ansible task is probably created at the installation of nginx and already linked to the default file in the sites-enabled folder. That's why nothing had to change.

Re-run the web.yml playbook a second time. In principle nothing should have changed. Compare Ansible's output with the first run.

Yoda:playbooks frederic$ ansible-playbook web.yml

PLAY [Configure webserver with nginx] **********************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [testserver]

TASK [install nginx] ***************************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy nginx config file] ******************************************************************************************************************************************************************************
ok: [testserver]

TASK [enable configuration] ********************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy index.html] *************************************************************************************************************************************************************************************
ok: [testserver]

TASK [restart nginx] ***************************************************************************************************************************************************************************************
changed: [testserver]

PLAY RECAP *************************************************************************************************************************************************************************************************
testserver                 : ok=6    changed=1    unreachable=0    failed=0

Which tasks are marked as changed?

We can see that only the task TASK [restart nginx] is marked as changed.

Any surprises?

It'is not a surprise because we ordered the server to do so. In any case a rstart of the nginx server will be triggered, by that way the state of the server will allways change.

In the playbook comment out update_cache=yes and re-run the playbook.

Yoda:playbooks frederic$ ansible-playbook web.yml

PLAY [Configure webserver with nginx] **********************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [testserver]

TASK [install nginx] ***************************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy nginx config file] ******************************************************************************************************************************************************************************
ok: [testserver]

TASK [enable configuration] ********************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy index.html] *************************************************************************************************************************************************************************************
ok: [testserver]

TASK [restart nginx] ***************************************************************************************************************************************************************************************
changed: [testserver]

PLAY RECAP *************************************************************************************************************************************************************************************************
testserver                 : ok=6    changed=1    unreachable=0    failed=0

SSH into the managed server. Modify the nginx configuration file /etc/nginx/sites-available/default, for example by adding a line with a comment. Re-run the playbook.

Yoda:playbooks frederic$ ansible-playbook web.yml

PLAY [Configure webserver with nginx] **********************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [testserver]

TASK [install nginx] ***************************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy nginx config file] ******************************************************************************************************************************************************************************
changed: [testserver]

TASK [enable configuration] ********************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy index.html] *************************************************************************************************************************************************************************************
ok: [testserver]

TASK [restart nginx] ***************************************************************************************************************************************************************************************
changed: [testserver]

PLAY RECAP *************************************************************************************************************************************************************************************************
testserver                 : ok=6    changed=2    unreachable=0    failed=0

What does Ansible do to the file and what does it show in its output?

Ansible simply replace the modified file with the one specified in the task copy nginx config file.

Do something more drastic like completely removing the homepage and repeat the previous question.

Yoda:playbooks frederic$ ansible-playbook web.yml

PLAY [Configure webserver with nginx] **********************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [testserver]

TASK [install nginx] ***************************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy nginx config file] ******************************************************************************************************************************************************************************
ok: [testserver]

TASK [enable configuration] ********************************************************************************************************************************************************************************
ok: [testserver]

TASK [copy index.html] *************************************************************************************************************************************************************************************
changed: [testserver]

TASK [restart nginx] ***************************************************************************************************************************************************************************************
changed: [testserver]

PLAY RECAP *************************************************************************************************************************************************************************************************
testserver                 : ok=6    changed=2    unreachable=0    failed=0

What does Ansible do to the file and what does it show in its output?

We can see that the deleted index.html file has been replaced with the one specified in the task copy index.html.

Task 6: Adding a handler for nginx restart

Task 7: Add more managed servers