Ansible 2 of 9: Inventory/Ad-Hoc Configuration

1 of 12. Open AWS Cloud9

2 of 12. Setup folders and files

Create a folder ansible-tasks
The ansible-tasks folder will be placed inside your 'environment' folder
Inside of ansible-tasks folder, create a file called hosts
Insert the Private IPs of the Hosts (host01 and host02) inside of the hosts file

pwd ls mkdir ansible-tasks cd ansible-tasks touch hosts echo 172.31.89.75 > hosts echo 172.31.18.118 >> hosts

If you don't know the private IP of your instances, you can find it easily:

Note: This example depends upon tagging your instances with names beginning with 'host'

aws ec2 describe-instances \ --filters 'Name=tag:Name,Values=host*' \ --query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value|[0],State.Name,PrivateIpAddress]' \ --output text | column -t

3 of 12. Use the ping module to test connectivity.'

Note: You will get an error unless you specify the correct login information, to include SSH key. Notice that the Debian host fails, even if you specify the key file, because Debian uses a default user 'admin' instead of 'ec2-user' like the RedHat host.

For ease of use, move your key file to the ansible-tasks folder

cd ansible-tasks mv ../tcb-ansible-key.pem .

ansible -i hosts all -m ping

ansible -i hosts all -m ping -e "ansible_ssh_private_key_file=tcb-ansible-key.pem"

4 of 12. Update the "hosts" inventory file

We will add the user information for each host AND we will add the SSH key file information. (This way, we do not have to specify those parameters at runtime.)

Try the ping module afterwards. Note: you can try the username at runtime, but it will only be able to connect to hosts where that username is valid.

host01 ansible_host=172.31.89.75 ansible_user=admin host02 ansible_host=172.31.18.118 ansible_user=ec2-user [all:vars] ansible_ssh_private_key_file=/home/ec2-user/environment/ansible-tasks/tcb-ansible-key.pem ansible -i hosts all -m ping

5 of 12. Add a group for the webservers to the "hosts" inventory file.

Follow-up by using the ping module against the webservers group

[webservers] host01 ansible -i hosts webservers -m ping

6 of 12. Check your Ansible config.

Note: You don't have a config file yet.

ansible-config ansible --version

7 of 12. Let's create a sample ansible config file.

This file will be placed inside the ansible-tasks folder. Copy from the sample ansible config file at Github

ansible.cfg

8 of 12. Make the ping module the default module.

Follow-up by re-testing ansible against your inventory file 'hosts'. Do not specify the ping module.

module_name = ping ansible -i hosts all

9 of 12. Set the default inventory file as 'hosts'

Uncomment and set inventory to include the path to your 'hosts' file

Follow-up by re-testing. Do not specify the inventory file

inventory = /etc/ansible/hosts,/home/ec2-user/environment/ansible-tasks/hosts ansible all

10 of 12. Check the ansible-inventory

ansible-inventory --graph

11 of 12. Check ansible-config version

Try the --version switch.

ansible-config --version

12 of 12. Try the dump option.

Non-default changes will be highlighted.

ansible-config dump

References

Ansible Configuration Settings (Latest)

Ansible Configuration Settings (2.9)

ansible.cfg

describe-instances

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