7.4.8 Lab - Use Ansible To Automate Installing A Web Server - ILM
7.4.8 Lab - Use Ansible To Automate Installing A Web Server - ILM
Version)
Instructor Note: Red font color or gray highlights indicate text that appears in the instructor copy only.
Objectives
Part 1: Launch the DEVASC VM
Part 2: Configure Ansible
Part 3 Verify Communications with the Local Webserver
Part 4: Create Ansible Playbooks to Automate Webserver Installation
Part 5: Add Options to Your Ansible Playbook for Apache Web Servers
Background / Scenario
In this lab, you will first configure Ansible so that it can communicate with a webserver application. You will
then create a playbook that will automate the process of installing Apache on the webserver. You will also
create a customized playbook that installs Apache with specific instructions.
Required Resources
1 PC with operating system of your choice
Virtual Box or VMWare
DEVASC Virtual Machine
Instructions
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 9 www.netacad.com
Lab - Use Ansible to Automate Installing a Web Server
Install SSH
devasc@labvm:~$ sudo apt-get install openssh-server
Install sshpass
devasc@labvm:~$ sudo apt-get install sshpass
c. The credentials devasc and Cisco123! are admin credentials for the DEVASC VM. The IPv4 address
you will use for this lab is 192.0.2.3. This is a static IPv4 address on the VM under the dummy0 interface,
as shown in the output for the ip addr command.
devasc@labvm:~/labs/devnet-src/ansible$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen
1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group
default qlen 1000
link/ether 08:00:27:97:ae:11 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
valid_lft 45882sec preferred_lft 45882sec
inet6 fe80::a00:27ff:fe97:ae11/64 scope link
valid_lft forever preferred_lft forever
3: dummy0: <BROADCAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group
default qlen 1000
link/ether a6:44:a7:e8:6a:9e brd ff:ff:ff:ff:ff:ff
inet 192.0.2.1/32 scope global dummy0
valid_lft forever preferred_lft forever
inet 192.0.2.2/32 scope global dummy0
valid_lft forever preferred_lft forever
inet 192.0.2.3/32 scope global dummy0
valid_lft forever preferred_lft forever
inet 192.0.2.4/32 scope global dummy0
valid_lft forever preferred_lft forever
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 9 www.netacad.com
Lab - Use Ansible to Automate Installing a Web Server
Step 1: Use the ping module to verify that Ansible can ping the webserver.
Use the Ansible ping module to verify communications with the devices listed within the webservers group of
your hosts inventory file.
devasc@labvm:~/labs/devnet-src/ansible/ansible-apache$ ansible webservers -m
ping
192.0.2.3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
devasc@labvm:~/labs/devnet-src/ansible/ansible-apache$
If multiple devices were listed under the webservers group in your hosts inventory file, the output would
indicate similar information for each device.
Step 2: Use the command module to verify Ansible can communicate with the webserver.
Use the Ansible command module to verify communications with the devices listed within the webservers
group of your hosts inventory file. In this example you send the argument -a "/bin/echo hello world" to ask
the local webserver to respond with “hello world”.
devasc@labvm:~/labs/devnet-src/ansible/ansible-apache$ ansible webservers -m
command -a "/bin/echo hello world"
192.0.2.3 | CHANGED | rc=0 >>
hello world
devasc@labvm:~/labs/devnet-src/ansible/ansible-apache$
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 9 www.netacad.com
Lab - Use Ansible to Automate Installing a Web Server
devasc@labvm:~/labs/devnet-src/ansible/ansible-apache$
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 9 www.netacad.com
Lab - Use Ansible to Automate Installing a Web Server
b. Add the following information to the file. Make sure you use the proper YAML indentation. Every space
and dash is significant. You may lose some formatting if you copy and paste. The highlighted text is
explained in the next step.
---
- hosts: webservers
become: yes
tasks:
- name: INSTALL APACHE2
apt: name=apache2 update_cache=yes state=latest
handlers:
- name: RESTART APACHE2
service: name=apache2 state=restarted
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 9 www.netacad.com
Lab - Use Ansible to Automate Installing a Web Server
The PLAY RECAP should display ok and failed=0 indicating a successful playbook execution.
Part 5: Add Options to Your Ansible Playbook for Apache Web Servers
In a production environment, the Apache2 default installation is typically customized for the specific features
needed by the organization. An Ansible playbook can help automate these configuration tasks, as well. In this
part, you will customize your playbook by specifying that the Apache server use a different port number.
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 9 www.netacad.com
Lab - Use Ansible to Automate Installing a Web Server
handlers:
- name: RESTART APACHE2
service: name=apache2 state=restarted
This playlist is very similar to the previous one with the addition of two tasks that have the webservers
listen on port 8081 instead of port 80.
The lineinfile module is used to replace existing lines in the /etc/apache2/ports.conf and
/etc/apache2/sites-available/000-default.conf files. You can search the Ansible documentation for more
information on the lineinfile module.
Step 2: Examine the two files that will be modified by the playbook.
Display the files /etc/apache2/ports.conf and /etc/apache2/sites-available/000-default.conf. Notice the
webserver is currently listening on port 80.
devasc@labvm:~/labs/devnet-src/ansible/ansible-apache$ cat
/etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
Listen 80
<IfModule ssl_module>
Listen 443
<output omitted>
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 9 www.netacad.com
Lab - Use Ansible to Automate Installing a Web Server
devasc@labvm:~/labs/devnet-src/ansible/ansible-apache$
devasc@labvm:~/labs/devnet-src/ansible/ansible-apache$
Listen 8081
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 9 www.netacad.com
Lab - Use Ansible to Automate Installing a Web Server
b. Open the Chromium web browser and enter the IPv4 address for your new server. But this time specify
8081 as the port number, 192.0.2.3:8081, to see the default Apache2 web page.
Note: Although you can see in the ports.conf file that Apache2 is also listening on port 443, this is for
secure HTTP. You have not yet configured Apache2 for secure access. This, of course, would be added
to your Ansible playbook, but is beyond the scope of this course.
End of document
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 9 www.netacad.com