Saturday, December 12, 2020

Ansible - EC2 instance creation using ansible

Ansible - EC2 instance creation using ansible..

1. Write your playbook
-> Collect all the manual steps to create an EC2 instance. Google for EC2 instance creation using ansible..
# cat aws-ec2.yaml
- hosts: localhost # 192.168.56.5 - use your own control node)
  tasks:
  - ec2_instance:
      region: us-east-1
      image_id: ami-04d29b6f966df1537
      instance_type: t2.micro
      #image: t2.micro
      vpc_subnet_id: subnet-e261d2ec
      security_group: sg-f5b18ad2
      key_name: kt-2020-k
      name: os_from_ansible
      state: present
      aws_access_key: AKIA6DEA42GA2PGZJ7G3
      aws_secret_key: 3IYF568qVJ8I#RZYnUV2OPG8/XDKVrhDfJRJPnbc

2. Run your playbook
[root@master wk-dec9]# ansible-playbook aws-ec2.yaml
PLAY [localhost] *****************************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [localhost]
TASK [ec2_instance] **************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (botocore or boto3) on master's Python /usr/bin/python3.6. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}
PLAY RECAP ***********************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

3. Review the error and Install boto3
[root@master wk-dec9]# pip3 install boto3
Successfully installed boto3-1.16.33 botocore-1.19.33 s3transfer-0.3.3 urllib3-1.26.2

4. Re-run your playbook
[root@master wk-dec9]# ansible-playbook aws-ec2.yaml
PLAY [localhost] *****************************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [localhost]
TASK [ec2_instance] **************************************************************************************
changed: [localhost]
PLAY RECAP ***********************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[root@master wk-dec9]#

5. Playbook content ..
[root@master wk-dec9]# cat aws-ec2.yaml
- hosts: localhost # 192.168.56.4 - your own control node)
  tasks:
  - ec2_instance:
      region: us-east-1
      image_id: ami-04d29b6f966df1537
      instance_type: t2.micro
      #image: t2.micro
      vpc_subnet_id: subnet-e251d2ec
      security_group: sg-f7a18ad2
      key_name: kb-2020-key
      name: os_from_ansible
      state: present
      aws_access_key: AKIC6HXA42MR2PGZJ7G3
      aws_secret_key: 3IYF590qVJ8ISpZYnUV92PG8/XDKVrhHsJcMPnbc

No comments:

Post a Comment