Laureline's Wiki

Laureline's Wiki

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
heig:cld:lab07 [2017/05/15 16:28] – [Task 6: Adding a handler for nginx restart] mikeheig:cld:lab07 [2017/05/15 17:09] (current) – external edit 127.0.0.1
Line 7: Line 7:
 DONE DONE
  
-<code>+<code bash>
 Yoda:~ frederic$ ansible --version Yoda:~ frederic$ ansible --version
 ansible 2.3.0.0 ansible 2.3.0.0
Line 19: Line 19:
 DONE DONE
  
-<code>+<code bash>
 ssh -i id_rsa ubuntu@ec2-54-147-90-78.compute-1.amazonaws.com ssh -i id_rsa ubuntu@ec2-54-147-90-78.compute-1.amazonaws.com
 ... ...
Line 29: Line 29:
 DONE DONE
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible testserver -i hosts -m ping 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': 
Line 39: Line 39:
 </code> </code>
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible testserver -m ping Yoda:playbooks frederic$ ansible testserver -m ping
  
Line 50: Line 50:
 </code> </code>
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible testserver -m command -a uptime Yoda:playbooks frederic$ ansible testserver -m command -a uptime
 Enter passphrase for key '/Users/frederic/.ssh/id_rsa':  Enter passphrase for key '/Users/frederic/.ssh/id_rsa': 
Line 56: Line 56:
  12:44:43 up 31 min,  2 users,  load average: 0.00, 0.01, 0.05  12:44:43 up 31 min,  2 users,  load average: 0.00, 0.01, 0.05
 </code> </code>
 +
 ===== Task 4: Install web application ===== ===== Task 4: Install web application =====
  
 DONE DONE
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible webservers -m ping Yoda:playbooks frederic$ ansible webservers -m ping
 Enter passphrase for key '/Users/frederic/.ssh/id_rsa':  Enter passphrase for key '/Users/frederic/.ssh/id_rsa': 
Line 69: Line 70:
 </code> </code>
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible-playbook web.yml Yoda:playbooks frederic$ ansible-playbook web.yml
  
Line 75: Line 76:
  
 TASK [Gathering Facts] ************************************************************************************************************************************************************************************* TASK [Gathering Facts] *************************************************************************************************************************************************************************************
-Enter passphrase for key '/Users/frederic/.ssh/id_rsa':  
 ok: [testserver] ok: [testserver]
  
Line 103: Line 103:
 **Return to the output of running the web.yml playbook the first time. There is one task that Ansible marked as ok.**  **Return to the output of running the web.yml playbook the first time. There is one task that Ansible marked as ok.** 
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible-playbook web.yml Yoda:playbooks frederic$ ansible-playbook web.yml
  
Line 132: Line 132:
 **Which one?** **Which one?**
  
-The task that Ansible marked as ok are the followings : +The tasks that Ansible marked as ok are the followings : 
  
   - //TASK [Gathering Facts]//   - //TASK [Gathering Facts]//
Line 139: Line 139:
 **Do you have a possible explanation?** **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 //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// 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 in this task. 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 in this task.
Line 145: Line 145:
 **Re-run the web.yml playbook a second time. In principle nothing should have changed. Compare Ansible's output with the first run.** **Re-run the web.yml playbook a second time. In principle nothing should have changed. Compare Ansible's output with the first run.**
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible-playbook web.yml Yoda:playbooks frederic$ ansible-playbook web.yml
  
Line 182: Line 182:
 **In the playbook comment out update_cache=yes and re-run the playbook.** **In the playbook comment out update_cache=yes and re-run the playbook.**
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible-playbook web.yml Yoda:playbooks frederic$ ansible-playbook web.yml
  
Line 211: Line 211:
 **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.** **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.**
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible-playbook web.yml Yoda:playbooks frederic$ ansible-playbook web.yml
  
Line 244: Line 244:
 **Do something more drastic like completely removing the homepage and repeat the previous question.** **Do something more drastic like completely removing the homepage and repeat the previous question.**
  
-<code>+<code bash>
 Yoda:playbooks frederic$ ansible-playbook web.yml Yoda:playbooks frederic$ ansible-playbook web.yml
  
Line 274: Line 274:
  
 We can see that the deleted index.html file has been replaced with the one specified in the task //copy index.html//. 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 =====
Line 287: Line 285:
  
 TASK [Gathering Facts] ************************************************************************************************************************************************************************************* TASK [Gathering Facts] *************************************************************************************************************************************************************************************
-Enter passphrase for key '/Users/frederic/.ssh/id_rsa':  
 ok: [testserver] ok: [testserver]
  
Line 339: Line 336:
 </code> </code>
  
 +===== Task 7: Add more managed servers =====
  
 +**Re-run the web.yml playbook. What do you observe in Ansible's output?**
 +
 +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:playbooks frederic$ ansible-playbook web.yml
 +
 +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              : ok=6    changed=4    unreachable=0    failed=0   
 +54.147.90.78               : ok=5    changed=0    unreachable=0    failed=0
 +</code>
 +
 +**Test the new server by pointing your web browser to it.**
 +
 +{{ :heig:cld:cld07_t3.png?nolink |}}
 +
 +**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:playbooks frederic$ ansible-playbook web.yml
 +
 +PLAY [Configure webserver with nginx] **********************************************************************************************************************************************************************
 +
 +TASK [Gathering Facts] *************************************************************************************************************************************************************************************
 +fatal: [52.206.206.57]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 52.206.206.57 port 22: Operation timed out\r\n", "unreachable": true}
 +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 @/Users/frederic/Desktop/CLD_Lab07/playbooks/web.retry
 +
 +PLAY RECAP *************************************************************************************************************************************************************************************************
 +52.206.206.57              : ok=0    changed=0    unreachable=1    failed=0   
 +54.147.90.78               : ok=5    changed=0    unreachable=0    failed=0
 +</code>
  
-===== Task 7: Add more managed servers =====