Table of Contents
Installing ansible
pip install ansible-core
ansible --versionansible [core 2.15.13]
config file = None
configured module search path = ['/home/redhat/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/redhat/.local/lib/python3.9/site-packages/ansible
ansible collection location = /home/redhat/.ansible/collections:/usr/share/ansible/collections
executable location = /home/redhat/.local/bin/ansible
python version = 3.9.16 (main, Jul 5 2024, 00:00:00) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2)] (/usr/bin/python3)
jinja version = 3.1.4
libyaml = TrueStatic inventory
ansible.cfg
[defaults]
inventory=hosts
interpreter_python=auto_silent
host_key_checking=false
private_key_file=cfreire-key-20260618inventory hosts file
[webservers]
15.224.102.191
[dbservers]
13.39.83.19
Test the inventory
$ ansible-inventory all --graph
@all:
|--@ungrouped:
|--@webservers:
| |--15.224.102.191
|--@dbservers:
| |--13.39.83.19
$ ansible all -m ping
13.39.83.19 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3.9"
},
"changed": false,
"ping": "pong"
}
15.224.102.191 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3.9"
},
"changed": false,
"ping": "pong"
}Gather facts playbook
test-facts.yml
---
- name: Test connection and gather_facts
hosts: all
gather_facts: true
tasks:
- name: Ping {{ inventory_hostname }}
ansible.builtin.ping:
- name: Print facts
ansible.builtin.debug:
var: ansible_facts
Run the playbook
ansible-playbook test-facts.yml
# Generate report
ansible-playbook test-facts.yml > test-facts-report.txtDynamic inventory
https://docs.ansible.com/ansible/latest/collections/amazon/aws/docsite/aws_ec2_guide.html
Install amazon plugin
# install amazon module
ansible-galaxy collection install amazon.aws:9.5.2
# verify is collection is installed
ansible-galaxy collection listInstall python requirements
pip install botocore boto3Create dynamic inventory file
plugin: amazon.aws.aws_ec2
aws_ec2.yml
# aws_ec2.yml
plugin: amazon.aws.aws_ec2
# The access key for your AWS account.
aws_access_key: XXXXXXXXXXXXXXXXXX
# The secret access key for your AWS account.
aws_secret_key: XXXXXXXXXX/XXXXXXXXXXXXXX
regions:
- eu-west-3
Change ansible.cfg inventory
[defaults]
inventory=aws_ec2.yml # <---- change here
interpreter_python=auto_silent
host_key_checking=false
Test the inventory
$ ansible-inventory all --graph
@all:
|--@ungrouped:
|--@aws_ec2:
| |--ec2-18-185-116-134.eu-central-1.compute.amazonaws.com
| |--ec2-18-194-197-78.eu-central-1.compute.amazonaws.com
# test ssh connection
ansible -m ping allAdd groups
# aws_ec2.yml
plugin: amazon.aws.aws_ec2
# The access key for your AWS account.
aws_access_key: XXXXXXXXXXXXXXXXXXXXXXXX
# The secret access key for your AWS account.
aws_secret_key: XXXXXXXXXXXX/XXXXXXXXXXXXXXXXX
regions:
- eu-west-3
keyed_groups:
- key: tags['Role']
prefix: cfreire
filters:
tag:Group: cfreire
instance-state-name : running
# verify inventory
ansible-inventory all --graph
@all:
|--@ungrouped:
|--@aws_ec2:
| |--ec2-15-224-102-191.eu-west-3.compute.amazonaws.com
| |--ec2-13-39-83-19.eu-west-3.compute.amazonaws.com
|--@cfreire_webserver:
| |--ec2-15-224-102-191.eu-west-3.compute.amazonaws.com
|--@cfreire_database:
| |--ec2-13-39-83-19.eu-west-3.compute.amazonaws.com
# test access to inventory servers
ansible cfreire_servers -m ping
# Test facts
ansible-playbook test-facts.yml