You are here: Links of Interest » HEIG-VD » [CLD] Cloud Computing » Lab 07: Configuration Management
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
heig:cld:lab07 [2017/05/15 14:27] – mike | heig:cld:lab07 [2017/05/15 17:09] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 7: | Line 7: | ||
DONE | DONE | ||
- | < | + | < |
Yoda:~ frederic$ ansible --version | Yoda:~ frederic$ ansible --version | ||
ansible 2.3.0.0 | ansible 2.3.0.0 | ||
Line 19: | Line 19: | ||
DONE | 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 ===== | ===== Task 3: Configure Ansible to connect to the managed VM ===== | ||
+ | |||
+ | DONE | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | Enter passphrase for key '/ | ||
+ | Enter passphrase for key '/ | ||
+ | testserver | SUCCESS => { | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | Enter passphrase for key '/ | ||
+ | testserver | SUCCESS => { | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | Enter passphrase for key '/ | ||
+ | testserver | SUCCESS | rc=0 >> | ||
+ | | ||
+ | </ | ||
===== Task 4: Install web application ===== | ===== Task 4: Install web application ===== | ||
+ | |||
+ | DONE | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | Enter passphrase for key '/ | ||
+ | testserver | SUCCESS => { | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | {{ : | ||
===== Task 5: Test Desired State Configuration principles ===== | ===== 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.** | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | **Which one?** | ||
+ | |||
+ | The tasks that Ansible marked as ok are the followings : | ||
+ | |||
+ | - //TASK [Gathering Facts]// | ||
+ | - //TASK [enable configuration]// | ||
+ | |||
+ | **Do you have a possible explanation? | ||
+ | |||
+ | The //garhering facts// task is a task in which Ansible try to connect to the distant servers to gather the information needed to do its job. | ||
+ | |||
+ | The //enable configuration// | ||
+ | |||
+ | **Re-run the web.yml playbook a second time. In principle nothing should have changed. Compare Ansible' | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | **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.** | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | **SSH into the managed server. Modify the nginx configuration file / | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | **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.** | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | **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 6: Adding a handler for nginx restart ===== | ||
+ | |||
+ | Here is the output of the Ansible command: | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | 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] | ||
+ | |||
+ | PLAY RECAP ************************************************************************************************************************************************************************************************* | ||
+ | testserver | ||
+ | </ | ||
+ | |||
+ | We can see that this time the nginx server hasn't been restarted after the ansible command which was the comportment we wanted. | ||
+ | |||
+ | **Copy the modified playbook into the lab report.** | ||
+ | |||
+ | <code yaml> | ||
+ | - name: Configure webserver with nginx | ||
+ | hosts: webservers | ||
+ | sudo: True | ||
+ | tasks: | ||
+ | - name: install nginx | ||
+ | apt: name=nginx # | ||
+ | notify: | ||
+ | - restart nginx | ||
+ | - name: copy nginx config file | ||
+ | copy: src=files/ | ||
+ | notify: | ||
+ | - restart nginx | ||
+ | - name: enable configuration | ||
+ | file: > | ||
+ | dest=/ | ||
+ | src=/ | ||
+ | state=link | ||
+ | notify: | ||
+ | - restart nginx | ||
+ | - name: copy index.html | ||
+ | template: src=templates/ | ||
+ | notify: | ||
+ | - restart nginx | ||
+ | handlers: | ||
+ | - name: restart nginx | ||
+ | service: name=nginx state=restarted | ||
+ | </ | ||
===== Task 7: Add more managed servers ===== | ===== Task 7: Add more managed servers ===== | ||
+ | |||
+ | **Re-run the web.yml playbook. What do you observe in Ansible' | ||
+ | |||
+ | We can see that everything goes as expected. No tasks has been trigerred for the first server (IP: 54.147.90.78) which was already up to date. For the newly added server 4 tasks were trigered and the nginx server restarted as expected. | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | PLAY [Configure webserver with nginx] ********************************************************************************************************************************************************************** | ||
+ | |||
+ | TASK [Gathering Facts] ************************************************************************************************************************************************************************************* | ||
+ | ok: [52.206.206.57] | ||
+ | |||
+ | TASK [install nginx] *************************************************************************************************************************************************************************************** | ||
+ | ok: [54.147.90.78] | ||
+ | changed: [52.206.206.57] | ||
+ | |||
+ | TASK [copy nginx config file] ****************************************************************************************************************************************************************************** | ||
+ | ok: [54.147.90.78] | ||
+ | changed: [52.206.206.57] | ||
+ | |||
+ | TASK [enable configuration] ******************************************************************************************************************************************************************************** | ||
+ | ok: [54.147.90.78] | ||
+ | ok: [52.206.206.57] | ||
+ | |||
+ | TASK [copy index.html] ************************************************************************************************************************************************************************************* | ||
+ | ok: [54.147.90.78] | ||
+ | changed: [52.206.206.57] | ||
+ | |||
+ | RUNNING HANDLER [restart nginx] **************************************************************************************************************************************************************************** | ||
+ | changed: [52.206.206.57] | ||
+ | |||
+ | PLAY RECAP ************************************************************************************************************************************************************************************************* | ||
+ | 52.206.206.57 | ||
+ | 54.147.90.78 | ||
+ | </ | ||
+ | |||
+ | **Test the new server by pointing your web browser to it.** | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | **What happens if a server is not reachable? Shut down the second instance and re-run the playbook.** | ||
+ | |||
+ | We can see that Ansible try to reach the server in vain so it change the status of the server to unreachable. We can also see that Ansible continue the other tasks on the available servers. | ||
+ | |||
+ | <code bash> | ||
+ | Yoda: | ||
+ | |||
+ | PLAY [Configure webserver with nginx] ********************************************************************************************************************************************************************** | ||
+ | |||
+ | TASK [Gathering Facts] ************************************************************************************************************************************************************************************* | ||
+ | fatal: [52.206.206.57]: | ||
+ | ok: [54.147.90.78] | ||
+ | |||
+ | TASK [install nginx] *************************************************************************************************************************************************************************************** | ||
+ | ok: [54.147.90.78] | ||
+ | |||
+ | TASK [copy nginx config file] ****************************************************************************************************************************************************************************** | ||
+ | ok: [54.147.90.78] | ||
+ | |||
+ | TASK [enable configuration] ******************************************************************************************************************************************************************************** | ||
+ | ok: [54.147.90.78] | ||
+ | |||
+ | TASK [copy index.html] ************************************************************************************************************************************************************************************* | ||
+ | ok: [54.147.90.78] | ||
+ | to retry, use: --limit @/ | ||
+ | |||
+ | PLAY RECAP ************************************************************************************************************************************************************************************************* | ||
+ | 52.206.206.57 | ||
+ | 54.147.90.78 | ||
+ | </ | ||
+ | |||