08. Ansible Practice

Practice Exercise 1

Practice Exercise 1

Get some valuable practice with the skills in this lesson by working through these exercises!

  • Install Ansible.
    sudo apt install -y ansible
  • Create a new directory called nginx_ansible.
    mkdir nginx_ansible/roles/nginx/tasks
  • Create an Ansible role to install Nginx.
  • Run the job.
  • Take a screenshot of your run of Ansible, and compare it to my screenshot of my Ansible run below.

Some things to get you started:

main.yaml:
# Install Nginx
- hosts: localhost
  connection: local
  become: True
  become_user: root
  roles:
    - nginx
roles/nginx/tasks/main.yaml:
- name: Install Nginx
  apt:
  name: nginx
  state: present
  when: ansible_facts['os_family'] == 'Debian'
Inventory:

localhost ansible_connection=local

Practice Exercise 1 Screenshot

My Ansible Run to Compare to Yours

My Ansible Run to Compare to Yours

Practice Exercise 2

Practice Exercise 2

  • Create a template to generate your configuration file.
  • Extend your existing role as follows:
roles/nginx/tasks/main.yaml:
- name: Install Nginx
  apt:
    name: nginx
    state: present
  when: ansible_facts['os_family'] == 'Debian'
- name: generate Nginx site config
  template:
      src: site.conf.j2
      dest: /etc/nginx/sites-enabled/site.conf
      owner: root
      group: root
      mode: 0744
roles/nginx/templates/site.conf.j2:
server {
        listen   80;
        server_name  {{ SERVER_NAME }};
        root   {{ FILE_PATH }};
        location / {
                index  index.php index.html index.htm;
                autoindex off;    #enable listing of directory index
        }
}
roles/nginx/vars/main.yaml: (use your public IP address of the EC2 instance)
SERVER_NAME: http://3.19.66.42
FILE_PATH: /var/www/html
  • Run the Ansible role again and compare your results against my screenshot below:

Exercise 2

My Results After Running Ansible Role

My Results After Running Ansible Role

Practice Exercise 3

Practice Exercise 3

Adding to your existing Ansible

roles/nginx/templates/index.html:
<!doctype html>
   <html>
     <head>
         <title>Welcome {{ STUDENT_NAME }} to Ansible</title>
     </head>
     <body>
       <p><strong>Configure your world your way &#153;</strong></p>
     </body>
   </html>
roles/nginx/vars/main.yaml: (use your name and IP address)
SERVER_NAME: 3.19.66.42
FILE_PATH: /var/www/html
STUDENT_NAME: Olin Wread
roles/nginx/tasks/main.yaml:
- name: Install Nginx
  apt:
    name: nginx
    state: present
  when: ansible_facts['os_family'] == 'Debian'
- name: Generate Nginx site config
  template:
      src: site.conf.j2
      dest: /etc/nginx/sites-enabled/site.conf
      owner: root
      group: root
      mode: 0744
- name: Generate index.html
  template:
      src: index.html.j2
      dest: /var/www/html/index.html
      owner: root
      group: root
      mode: 0744
- name: Enable Nginx service
  systemd:
    name: nginx
    daemon_reload: yes
    enabled: yes
    state: started

Take a screenshot after you visit the webpage to verify it works and compare it to mine below:

Practice Exercise 3

Nginx

Nginx