Ansible 5 of 9: Playbooks 2 of 2
Background
This is a continuation of the prior article
Ansible 4 of 9: Playbooks 1 of 2
1 of 6. Open Cloud9
2 of 6. Install nginx using ansible (yum and shell modules)
Update cache, install nginx, start nginx, and remove nginx. Check the status after starting to confirm that it started up. Attempt to reach the webserver, to validate that it works. Remove the webserver. Finally, validate the webserver stops working post removal.
ansible all -m yum -a "update_cache=yes name=nginx state=latest" -b
ansible all -m shell -a "systemctl status nginx"
ansible all -m shell -a "systemctl start nginx" -b
ansible all -m shell -a "systemctl status nginx"
3 of 6. Check HTTP to the webserver
You can use the AWS CLI to grab the public IPs. After checking the site via web browser, remove nginx.
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,State:State.Name}' \
--output table
ansible all -m yum -a "name=nginx state=absent" -b
ansible all -m shell -a "systemctl status nginx"
4 of 6. Create a playbook for installing nginx
We would use a playbook, so that if we had to do this repetitive task in the future, we would not have to run a series of commands to install the nginx web service. Create the file install-webserver.yml inside the ansible-tasks folder, then populate it with the below content
pwd
touch install-webserver.yml
- name: Installing & starting ngnix
hosts: all
become: yes
tasks:
- name: Installing ngnix
yum:
update_cache: yes
name: nginx
state: latest
- name: Starting NGINX
shell: systemctl start nginx
- name: Enable the NGINX service during boot process
service:
name: nginx
enabled: yes
5 of 6. Install NGINX using ansible playbook.
Make sure to check to validate that the webserver is reachable. Because playbooks are reusable, you could run this exact same playbook at a future time. Imagine if you had a more extensive automation to deploy, and you can easily see how useful it is to leverage a playbook.
ansible-playbook install-webserver.yml
ansible all -m shell -a "systemctl status nginx"
6 of 6. Destroy the environment.
Check the console and validate that the instances were terminated.
terraform destroy
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,PublicIP:PublicIpAddress,PrivateIP:PrivateIpAddress,State:State.Name}' \
--output table
References
YAML Ain’t Markup Language (YAML™) version 1.2
Ansible playbooks - Ansible Documentation
ansible.builtin.yum module – Manages packages with the yum package manager
Command: init | Terraform | HashiCorp Developer
Command: plan | Terraform | HashiCorp Developer
Comamnd: apply | Terraform | HashiCorp Developer
Comamnd: destroy | Terrafrom | HashiCorp Developer
Provisioners | Terraform | Hashicorp Developer
Verify and Keep Control of Host Keys with Terraform
Getting started with systemctl | Enable Sysadmin
How to use systemctl to manage Linux services | Enable Syadmin
Comments
Post a Comment