- Ansible Server - Get Click here to install on RHEL 8 and click here to install on Amazon Linux
Optional Step to add ssh to all ec2 instances in on go ! or to setup ssh in ansible hosts Click here
To get help on dynamic inventory please follow Ansible Official Document To get help on dynamic inventory please follow AWS Official Document
- Download ec2.py and ec2.ini files
- Create IAM Programmatic access user with EC2 full access on AWS console if you are using ec2 instance as your ansible server
IAM
→users
→Add user
EC2
→Select-your-server
→Actions
→Security
→Modify IAM role
→Add or Update IAM role
- Export IAM user credentials on the Ansible server. if you are using your own machine as a server Get an IAM role and get secret keys from AWS account.
- Install AWS CLI on your machine.
sudo apt-get update
sudo apt-get install awscli
- Configure aws cli
aws configure
Enter your aws Access and secret Access keysAWS Access Key ID [****************DQ7G]:
AWS Secret Access Key [****************W9mW]:
Default region name [us-east-1]:
export AWS_ACCESS_KEY_ID='1bc123'
export AWS_SECRET_ACCESS_KEY='abc123'
- install python-pip and boto3
sudo apt-get install python
sudo apt install python3-pip
pip install boto3
pip3 - version
4. To export keys permanently make sure that you have installed pip and boto and add credentials ~/.boto file
5. add executing permissions to ec2.py script
chmod 755 ec2.py
- test the script
./ec2.py - list
- List out servers that are running on ap-south-1a AZ
ansible -i ec2.py ap-south-1a - list-hosts
- How to Run playbook with tags
- We can run ansible-playbook for our sever with some specific tag by giving hosts name as a tag in our playbook
- name: Aerospike Dynamic Inventory
hosts: tag_dev_view
gather_facts: no
# vars_files:
- then we can run our playbook as
ansible-playbook -i ec2.py main.yml
in this as we are giving tags in playbook tag is the combination oftag_dev_view
tag → tag dev → key view →value
- In this we can give multiple tags to our ansible server to identify environment and name Because we could have the same name in multiple environments.
- For this we can get our hosts to all AWS we don't have any hosts as it's dynamic inventory.
- Now we will be providing tags in command while running the ansible-playbook
ansible -i ec2.py - limit "tag_App_backend:&tag_Environment_staging:&tag_Usage_clock_worker" -m ping all
ansible-playbook -i ec2.py - limit "tag_App_backend:&tag_Environment_staging:&tag_Usage_clock_worker" main.yml
The module is maintained by Professor Xplorer
Licensed. See LICENSE for full details.