HOW TO SET UP ANSIBLE DYNAMIC INVENTORY
20th 0f march, 2025
1. Ensure that python3 and pip3 are installed on your ansible server
a. Confirm python version:
#
python3 --version
2. Step 2: Install the boto3 library.
Ansible uses the boot core to make API calls to AWS to retrieve ec2
instance details.
a. Install pip3 but before that, update packages
# sudo apt update
# sudo apt-get install python3-pip –y
3. step 3: Create an inventory directory under /opt and cd into the
directory.
# sudo mkdir -p /opt/ansible/inventory
# cd /opt/ansible/inventory
4. Step 4: Create a file named aws_ec2.yaml in the inventory
directory and vi into the file.
# touch aws_ec2.yaml
# sudo vi aws_ec2.yaml
Copy the following configuration to the file. If you are running an
ansible server outside the AWS environment, replace add your
AWS access key and secret to the config file.
--plugin: aws_ec2
aws_access_key: XXXXXXX
aws_secret_key: XXXXXXX
keyed_groups:
- key: tags
prefix: tag
HOW TO GENERATE ACCESS AND SECRETE KEY FROM AWS
ACCOUNT AND CONNECT TO OUR CLI
a. Go to your aws account, click on the top tight profile
section.
b. Click on security credentials.
c. Under access key, click on create access key and retrieve
the details.
Access key: AKIAUNPLKPRVE5G5APO
Secret key: VRfPLEhHy6CR4X+ui7Z/WF4nKDUH6+N7L/lQg17
TO CONNECT AWS CLOUD ACCOUNT TO OUR CLI DO THE
FOLLOWING
Run the following command and input the access and secret
key:
# aws configure
Check to confirm if your key is saved in the directory ~/.aws.
a.
# cd ~/.aws
# ls
c. # cat credentials
b.
Ansible ssh configuration
1. Clone the ansible-configuration repo
[ https://github.com/Lion-Technology-Solutions/ansibleconfiguration-management.git ] .
2. In your local CLI switch directory to /etc/ansible and;
3. Remove the existing ansible.cfg file from /etc/ansible using
the commands below;
Commands and steps
a. cd /etc/ansible
b. sudo rm -rf ansible.cfg
4. cd ~ to go back to your home directory and
Access the repo that was cloned above;
# cd ansible-configuration-management
5. Copy the ansible.cfg to /etc/ansible
# sudo cp ansible.cfg /etc/ansible/
Test Dynamic inventory:
Execute the following command to list the dynamic inventory
groups.
ansible-inventory -i /opt/ansible/inventory/aws_ec2.yaml --list
ansible-inventory -i /opt/ansible/inventory/aws_ec2.yaml --graph
Ssh Connections with dynamic inventory List all remote hosts in
the aws account
ansible-inventory -i /opt/ansible/inventory/aws_ec2.yaml --list
ansible-inventory -i /opt/ansible/inventory/aws_ec2.yaml --graph
List of different hosts groups:
1.
_Name_stage
2.
_Name_prod
3.
_Name_web
4.
_Name_manage_nodes
Test connections with remote host:
ANSIBLE_HOST_KEY_CHECKING=False ansible -i /opt/ansible/inventory/aws_ec2.yaml
_Name_prod –m ping --private-key ~/.ssh/maven-key.pem -u ubuntu
ANSIBLE_HOST_KEY_CHECKING=False = [This is to enable automatic host key
connection]
-i /opt/ansible/inventory/aws_ec2.yaml = [our ansible inventory path]
_Name_prod = [name of one of our host groups]
-m ping = [module]
--private-key ~/.ssh/maven-key.pem = [our keypair path]
-u ubuntu = [the default user of our remote hosts] this depends on the OS or the
default user