Ansible 7 of 9: Conditionals

Background

Use conditionals to control the execution of tasks and playbooks based on specific conditions or values.

1 of 16. Open Cloud9

2 of 16. Provision the infrastructure using Terraform

Update the "main.tf" with a host03. The host03 will be a different OS.

resource "aws_instance" "host03" { ami = "ami-01e5ff16fd6e8c542" instance_type = "t2.micro" key_name = "tcb-ansible-key" vpc_security_group_ids = [aws_security_group.secgroup.id] provisioner "local-exec" { command = "sleep 30; ssh-keyscan ${self.private_ip} >> ~/.ssh/known_hosts" } tags = { Name = "host03" } } output "host03_private_ip" { value = aws_instance.host03.private_ip } pwd cd ansible-tasks terraform apply -auto-approve

3 of 16. update the inventory file "hosts" with Private IP per terraform output

Make sure to add an entry for the host03. Note that host03 is running Debian, and the default user on Debian is 'admin'. Use the ping module to validate connectivity to the hosts.

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 ansible all -m ping

4 of 16. Try the gather_facts module against host01.

You can get all sorts of information with "gather_facts".

ansible host01 -m gather_facts

5 of 16. Try the setup module against host02

ansible host02 -m setup

6 of 16. You can filter the output of the setup module

Try a filter for "ansible_distribution" and "python_version"

ansible host01 -m setup -a "filter=ansible_distribution" ansible host02 -m setup -a "filter=ansible_distribution" ansible host03 -m setup -a "filter=ansible_distribution" ansible host01 -m setup -a "filter=ansible_python_version" ansible host02 -m setup -a "filter=ansible_python_version" ansible host03 -m setup -a "filter=ansible_python_version"

7 of 16. You can also use grep to grab specific parts of the output

ansible all -m setup | grep 'ansible_hostname\|ansible_os_family\|ansible_python_version\|ansible_pkg_mgr'

8 of 16. Create the playbook: install-webserver-conditional.yml

It refers to ansible_distribution, which you should be familiar with. Here, it should conditionally install a web server using the appropriate method, dependent upon the package manager for the operating system. Note that the cache update is included in the tasks.

- name: Installing Apache hosts: all become: yes tasks: - name: Setup Apache - Debian apt: update_cache: yes name: apache2 state: latest when: ansible_distribution == 'Debian' - name: Start Apache - Debian service: name: apache2 state: started when: ansible_distribution == 'Debian' - name: Setup Apache - RHEL yum: name: httpd state: latest when: ansible_distribution == 'RedHat' - name: Start - RHEL service: name: httpd enabled: true state: started when: ansible_distribution == 'RedHat'

9 of 16. Note: You can use the "in" operator to compare multiple values

when: ansible_distribution in ["Debian", "Ubuntu"]

10 of 16. Run the Ansible Playbook to install the webservers

This will use your conditionals to choose which installer to run, depending on the host OS

ansible-playbook install-webserver-conditional.yml

11 of 16. Check the status of apache on the hosts.

Note: The RedHat hosts are checked differently than the Debian hosts.

ansible all -m shell -a "systemctl status httpd" -b ansible all -m shell -a "systemctl status apache2" -b

12 of 16. Connect to the web servers via public IP.

Check the URLs, and validate that web server is running on the hosts.

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 16. Create an index.html file

Populate index.html with the contents below

touch index.html <html> <head> <style> body { background-color: #000000; color: #ffffff; font-family: Arial, sans-serif; } .container { display: flex; justify-content: center; align-items: center; height: 100vh; flex-direction: column; text-align: left; } img { max-width: 300px; margin-bottom: 20px; } </style> </head> <body> <div class="container"> <img src="https://thecloudbootcamp.com/wp-content/uploads/2020/09/The-Cloud-Bootcamp-big-transparent-1024x1024.png.webp" alt="Your Image"> <h1>Congratulations!</h1> <p>CATS: HOW ARE YOU GENTLEMEN !!</p> <p>CATS: ALL YOUR BASE ARE BELONG TO US.</p> <p>CATS: YOU ARE ON THE WAY TO DESTRUCTION.</p> <p>CAPTAIN: WHAT YOU SAY !!</p> <p>CATS: YOU HAVE NO CHANCE TO SURVIVE MAKE YOUR TIME.</p> <p>CATS: HA HA HA HA ....</p> </div> </body> </html>

14 of 16. Copy the sample index.html to host02

Obviously, validate that the file displays correctly.

ansible host02 -m copy -a "src=index.html dest=/var/www/html" -b

15 of 16. Note: the file copy can be included in your .yml file

- name: copy index.html copy: src: ~/my-local-directory/index.html dest: /var/www/html/ ansible-playbook install-webserver-conditional.yml

16 of 16. Cleanup

Destroy the infrastructure using Terraform

cd ansible-tasks 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

References

Conditionals - Ansible documentation

Ansible playbooks - Ansible documentation

Discovering variables: facts and magic variables - Ansible documentation

ansible.builtin.gather_facts module – Gathers facts about remote hosts - Ansible documentation

ansible.builtin.setup module - Gathers facts about remote hosts - Ansible documentation

ansible.builtin.shell module – Execute shell commands on targets - Ansible documentation

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

ansible.builtin.apt module – Manages apt-packages - Ansible documentation

Compiling and Installing - Apache HTTP Server Version 2.4

Managing software with the DNF tool Red Hat Enterprise Linux 9 | Red Hat Customer Portal

What is yum and how do I use it? - Red Hat Customer Portal

AptCLI - Debian Wiki

Getting started with systemctl | Enable Sysadmin

How to use systemctl to manage Linux services | Enable Sysadmin

Writing Your First Playbook :: Ansible Labs for AnsibleFest

Command: init | Terraform | HashiCorp Developer

Command: plan | Terraform | HashiCorp Developer

Command: apply | Terraform | HashiCorp Developer

Command: destroy | Terraform | HashiCorp Developer

All Your Base Are Belong to Us | Know Your Meme

All your base are belong to us

All Your Base Are Belong To Us

All your base are belong to us news report

Comments

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