Ansible is a great tool in doing configuration on any OS what we have to launch an OS using Ansible. So it can be possible using Ansible. But ansible is meant for configuration and for provisioning OS, we can use Terraform. Though Ansible we can manage configuration as well as provisioning there I am using Ansible to provision ec2 instance and also for configuring web server inside that ec2 instance.
Task Description :-
- Provision EC2 instance through ansible.
- ️Retrieve the IP Address of instance using dynamic inventory concept.
- Configure the web server through ansible.
1. Install necessary library
pip3 install boto
pip3 install boto3
2. Changes in Configuration file of ansible
3. Ansible Playbook to launch EC2 Instance
After retrieving the public IP of the newly launched ec2 instance. we will be copying that public IP in a file named as hosts.txt. Here, i have used the copy module and in that copy module i have used the content parameter.
4. Running Playbook
You can also use ansible-vault to store Access key ID and Secret access key. There wont be much change in code.
ansible-vault create ec2vault.yml#######and store Access key ID and Secret access key.
Changes in Playbook
Running Playbook with vault
5. Now Retrieving the IP Address of instance using a dynamic inventory concept.
Now For Proceeding we need IP Of Our Instance Launched on aws so we be using dynamic inventory concept using this concept we can get the ip of launched instance on aws using one python program that u can get from ansible github.
- Make a folder that will contain all the information’s of inventory.
- Step-2: Go inside /etc/ansible/ansible.cfg directory and set the path of your inventory folder which you made earlier.
- Step-3: After saving the inventory path, download pre-created python ec2.py and ec2.ini files. So follow the below syntax to download them.
wget https://github.com/ansible/ansible/blob/stable-2.9/contrib/inventory/ec2.pywget https://github.com/ansible/ansible/blob/stable-2.9/contrib/inventory/ec2.ini
- Step-4: Convert both files into an executable mode.
chmod +x ec2.pychmod +x ec2.init
- Step-5: Open your ec2.py with help of an editor like vim and vi and comment “from ansible.module_utils import ec2 as ec2_utils” line which is existing at 172 in your ec2.yml file.
from ansible.module_utils import six
#from ansible.module_utils import ec2 as ec2_utils
from ansible.module_utils.six.moves import configparser
Note: If your controller node has Python3 then replace the location of shebang(#!) from #!/usr/bin/env python to #!/usr/bin/python3.eg:
EC2 external inventory script
Generates inventory that Ansible can understand by making API request to
AWS EC2 using the Boto library.
- Step-6: Now open the ec2.ini file and give your access and secret key which will be mentioned at the bottom of the ec2.ini file.
[credentials]# The AWS credentials can optionally be specified here. Credentials specified
# here are ignored if the environment variable AWS_ACCESS_KEY_ID or
# AWS_PROFILE is set, or if the boto_profile property above is set.
# Supplying AWS credentials here is not recommended, as it introduces
# non-trivial security concerns. When going down this route, please make sure
# to set access permissions for this file correctly, e.g. handle it the same
# way as you would a private SSH key.
# Unlike the boto and AWS configure files, this section does not support
aws_access_key_id = AXXXXXXXXXXXXXX
aws_secret_access_key = XXXXXXXXXXXXXXXXXXX
- Now your dynamic inventory configuration is done. If your AWS Account has any launched instance the check with the below command.
[root@controller_node~]$ ansible all --list-hosts
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
188.8.131.52 #######this is the public ip
6. Steps for configuring the webserver through ansible.
Now For Configuring webserver in aws instance we need to do ssh and for this we will require private key.
Transfer your private key to the controller node by using winscp software.
chmod 400 sar_ansible.pem
7. Writing Playbook
I have combined all the above task into one playbook known as main playbook
import_playbook module imports the playbook
Running playbook all together
As you can see one EC2 instance was running already but when I ran the notebook again one more EC2 instance launched