In here I will examile a real use case example by creating and executing Ansible playbooks. I will install Nginx in all hosts and explore the different options for targeting OS variations with Ansible packages.
Use Ansible to install and configure project website, taking into account variances between the linux distributions of Nginx on both CentOS and Ubuntu.
Use Jinja2 templates to customise the website.
Let's first create our ansible config file ansible.cfg:
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: linux
# Vars: variables that will apply to the play, on all target systems
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
tasks:
- name: Install EPEL
yum:
name: epel-release
update_cache: yes
state: latest
when: ansible_distribution == 'CentOS'
# Handlers: the list of handlers that are executed as a notify key from a task
# Three dots indicate the end of a YAML document
...
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: linux
# Vars: variables that will apply to the play, on all target systems
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
tasks:
- name: Install EPEL
yum:
name: epel-release
update_cache: yes
state: latest
when: ansible_distribution == 'CentOS'
- name: Install Nginx CentOS
yum:
name: nginx
update_cache: yes
state: latest
when: ansible_distribution == 'CentOS'
- name: Install Nginx Ubuntu
apt:
name: nginx
update_cache: yes
state: latest
when: ansible_distribution == 'Ubuntu'
# Handlers: the list of handlers that are executed as a notify key from a task
# Three dots indicate the end of a YAML document
...
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: linux
# Vars: variables that will apply to the play, on all target systems
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
tasks:
- name: Install EPEL
yum:
name: epel-release
update_cache: yes
state: latest
when: ansible_distribution == 'CentOS'
- name: Install Nginx
package:
name: nginx
state: latest
# Handlers: the list of handlers that are executed as a notify key from a task
# Three dots indicate the end of a YAML document
...
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: linux
# Vars: variables that will apply to the play, on all target systems
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
tasks:
- name: Install EPEL
yum:
name: epel-release
update_cache: yes
state: latest
when: ansible_distribution == 'CentOS'
- name: Install Nginx
package:
name: nginx
state: latest
- name: Restart nginx
service:
name: nginx
state: restarted
# Handlers: the list of handlers that are executed as a notify key from a task
# Three dots indicate the end of a YAML document
...
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: linux
# Vars: variables that will apply to the play, on all target systems
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
tasks:
- name: Install EPEL
yum:
name: epel-release
update_cache: yes
state: latest
when: ansible_distribution == 'CentOS'
- name: Install Nginx
package:
name: nginx
state: latest
- name: Restart nginx
service:
name: nginx
state: restarted
notify: Check HTTP Service
# Handlers: the list of handlers that are executed as a notify key from a task
handlers:
- name: Check HTTP Service
uri:
url: http://{{ ansible_default_ipv4.address }}
status_code: 200
# Three dots indicate the end of a YAML document
...
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: linux
# Vars: variables that will apply to the play, on all target systems
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
tasks:
- name: Install EPEL
yum:
name: epel-release
update_cache: yes
state: latest
when: ansible_distribution == 'CentOS'
- name: Install Nginx
package:
name: nginx
state: latest
- name: Restart nginx
service:
name: nginx
state: restarted
notify: Check HTTP Service
- name: Template index.html-base.j2 to index.html on target
template:
src: index.html-base.j2
dest: "{{ nginx_root_location }}/index.html"
mode: 0644
# Handlers: the list of handlers that are executed as a notify key from a task
handlers:
- name: Check HTTP Service
uri:
url: http://{{ ansible_default_ipv4.address }}
status_code: 200
# Three dots indicate the end of a YAML document
...
---
# YAML documents begin with the document separator ---
# The minus in YAML this indicates a list item. The playbook contains a list
# of plays, with each play being a dictionary
-
# Hosts: where our play will run and options it will run with
hosts: linux
# Vars: variables that will apply to the play, on all target systems
vars_files:
- vars/logos.yaml
# Tasks: the list of tasks that will be executed within the play, this section
# can also be used for pre and post tasks
tasks:
- name: Install EPEL
yum:
name: epel-release
update_cache: yes
state: latest
when: ansible_distribution == 'CentOS'
- name: Install Nginx
package:
name: nginx
state: latest
- name: Restart nginx
service:
name: nginx
state: restarted
notify: Check HTTP Service
- name: Template index.html-logos.j2 to index.html on target
template:
src: index.html-logos.j2
dest: "{{ nginx_root_location }}/index.html"
mode: 0644
# Handlers: the list of handlers that are executed as a notify key from a task
handlers:
- name: Check HTTP Service
uri:
url: http://{{ ansible_default_ipv4.address }}
status_code: 200
# Three dots indicate the end of a YAML document
...