Ansible 9 of 9: Roles

1 of 13. Open Cloud9

2 of 13. Provision the infrastructure using Terraform

pwd terraform apply -auto-approve

3 of 13. Update the inventory file "hosts"

While updating the file, also add host02 to the webservers group.

aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,State:State.Name}' \ --output table cat hosts

4 of 13. Create an Ansible Role for webserver

This will create a "roles" directory, and a webserver directory, complete with all the sub-folders for a role.

ansible-galaxy role init roles/webserver

5 of 13. Upload the image devops.png to the roles/webserver/files folder

You can upload files from the File menu.

6 of 13. Update the file roles/webserver/handlers/main.yml

- name: Restarting Apache service: name: httpd state: restarted

7 of 13. Update the file roles/webserver/defaults/main.yml

This creates a variable for the cloud_provider

cloud_provider: "aws"

8 of 13. Create a template file index.html.j2 under roles/webserver/templates

Note the reference to your "cloud_provider" variable.

<!DOCTYPE html> <html> <head> <style> body { background-color: #000; color: #fff; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; font-family: Arial, sans-serif; } .content { text-align: center; } </style> </head> <body> <div class="content"> <b> Hello Bootcamper!</b> <h1> This is a webserver running in an EC2 Instance: {{ ansible_hostname }}</h1> <h3> This webserver was configured using Ansible Roles.</h3> <h4> Powered by {{ cloud_provider }}</h4> <img src="devops.png" alt="DevOps Image"> </div> </body> </html>

9 of 13. Update the file roles/webserver/tasks/main.yml

Note: The 'Restarting Apache' here refers to the "Restarting Apache" in the"roles/webserver/handlers/main.yml" file.

- name: Installing Apache yum: name: httpd state: present - name: Starting Apache service: name: httpd state: started enabled: true - name: Copying files copy: src=devops.png dest=/var/www/html/ - name: Generating Template template: src: index.html.j2 dest: /var/www/html/index.html notify: - Restarting Apache

10 of 13. Create the playbook setup-webserver-roles.yml

This will be inside the ansible-tasks folder. Note that "hosts: webservers" refers to the entry in the "hosts" file and "webserver" refers to the role in the roles folder.

- name: Setting up Apache as Webserver hosts: webservers become: true roles: - webserver

11 of 13. Run the Ansible playbook

ansible-playbook setup-webserver-roles.yml

12 of 13. Test the website.

Via web browser, connect to host01 and host02 public IP and view the updated websites You can find their public IP in your AWS console, or via the AWS CLI provided here.

aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,State:State.Name}' \ --output table

13 of 13. Destroy the infrastructure

terraform destroy -auto-approve aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,State:State.Name}' \ --output table


Roles - Ansible documentation

Ansible Galaxy

Ansible playbooks - Ansible documentation

How to build your inventory - Ansible Documentation

ansible.builtin.yum module – Manages packages with the yum package manager - Ansible Documentation

ansible.builtin.service module – Manage services - Ansible Documentation

ansible.builtin.copy module – Copy files to remote locations - Ansible Documentation

ansible.builtin.template module – Template a file out to a target host - Ansible Documentation

Compiling and Installing - Apache HTTP Server Version 2.4

Command: init | Terraform | HashiCorp Developer

Command: plan | Terraform | HashiCorp Developer

Command: apply | Terraform | HashiCorp Developer

Command: destroy | Terraform | HashiCorp Developer


Popular posts from this blog

Containing the Chaos! | A Three-Part Series Demonstrating the Usefulness of Containerization to HumanGov

Ansible is the Answer! | A Three-Part Series Demonstrating the Usefulness of Ansible to HumanGov