Ansible 3 of 9: Modules

Background

1 of 10. Open Cloud9

2 of 10. Validate host01 and host02 are still responsive.

Make sure that you're in the correct directory [where the 'hosts' inventory file exists]

pwd ls hosts cat hosts ansible -i hosts host01 -m ping ansible -i hosts host02 -m ping

3 of 10. Try using the command module to run commands on targets

Run commands for date, disk free info (in Kbytes), and operating system release. Please note that you could target all within your inventory file (hosts) or a specific entry within that file

ansible -i hosts all -m command -a "date" ansible -i hosts host02 -m command -a "df -k" ansible -i hosts host01 -m command -a "cat /etc/os-release" ansible -i hosts host01 -m command -a "apache2 -v"

4 of 10. Use the apt module to install apache2 on host01

Install apache2 on host01. Note: to avoid errors, you need to update the cache first.

ansible host01 -m apt -a "update_cache=yes" -become

ansible host01 -m apt -a "name=apache2 state=latest" -b

ansibe -i hosts host01 -m command -a "whereis apache2" ansible -i hosts host01 -m command -a "/usr/sbin/apache2 -v"

5 of 10. Modify security group, enabling Port 80 to host01.

You will want to test Apache2, by attempting HTTP to the Public IP.If the security group was appropriately modified, it should work great. Check the snippet below for an example of how to obtain the public IP of an Instance.

aws ec2 authorize-security-group-ingress \ --group-id sg-0cac6f47065813337 \ --protocol tcp \ --port 80 \ --cidr 0.0.0.0/0 aws ec2 describe-instances \ --filters Name=key-name,Values=tcb-ansible-key \ --query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value, PrivateIP:PrivateIpAddress, PublicIP:PublicIpAddress}' \ --output table

6 of 10. Use the shell module to check and stop apache2

After stopping apache2, the status should show the service as stopped, and if you check the page, it should no longer be accessible.

ansible host01 -m shell -a "systemctl status apache2" ansible host01 -m shell -a "systemctl stop apache2" -b

7 of 10. Use the service module to restart apache2

The status should now show as started and, if you check the web page again, it should be accessible.

ansible host01 -m service -a "name=apache2 state=started" -b ansible host01 -m shell -a "systemctl status apache2"

8 of 10. Use the copy module to copy a file to host01

Note that the file won't be there before you copy it there. The file contents should match what you placed in the file.

pwd ls echo "You down with BGP? We Love Oranges AS Oranges Mean Pure Refreshment. When applying in London in April, make love." >> dump ansible host01 -m command -a 'ls /tmp/dump' ansible host01 -m copy -a "src=dump dest=/tmp" ansible host01 -m command -a 'ls /tmp/dump' ansible host01 -m command -a "cat /tmp/dump"

9 of 10. Use the file command to delete the file you copied to host01

ansible host01 -m command -a 'ls /tmp/dump' ansible host01 -m file -a 'path=/tmp/dump state=absent' ansible host01 -m command -a 'ls /tmp/dump'

10 of 10. Terminate the instances host01 and host02

After running the command, you can validate the terminated instances in the console

aws ec2 terminate-instances --instance-ids i-021c0c8d1c9d4697b i-049525becc527c1b8 aws ec2 describe-instances \ --filters Name=key-name,Values=tcb-ansible-key \ --query 'Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key==`Name`]|[0].Value,State:State.Name}' \ --output table

References

Using Ansible modules and plugins

Introduction to modules

Index of all modules

Inventory plugins

ansible.builtin.command module - Execute commands on targets

ansible.builtin.apt module - Manages apt-packages

ansible.builtin.shell module - Execute shell commands on targets

ansible.builtin.copy module - Copy files to remote locations

ansible.builtin.file module - Manage files and file properties

ansible.builtin.service module - Manage services

Linux tools: du vs. df

Getting started with systemctl

describe-security-groups - AWS CLI 1.32.18 Command Reference

Work with security groups - Amazon Elastic Compute Cloud

BGP Path Manipulation + Goofy Mnemonic

Mnemonic For BGP Attributes

A Border Gateway Protocol 4 (BGP-4)

Naughty by Nature - O.P.P. (Official Music Video)

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