Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####---------------------------------- Tasks --------------------####
- #### Full playbook ####
- ---
- - name: "Creating the zomato webserver infra"
- hosts: localhost
- become: true
- vars:
- region: ap-south-1
- project_name: zomato
- project_env: dev
- region: ap-south-1
- state: present
- state_instance: present
- tags:
- - never
- - infra
- tasks:
- - name: "Install amazon.aws collection"
- community.general.ansible_galaxy_install:
- type: collection
- name: amazon.aws
- tags:
- - requirements
- - name: "Creating a new ssh keypair"
- become: false
- community.crypto.openssh_keypair:
- path: "{{ project_name }}-{{ project_env }}"
- passphrase: ""
- - name: "Installing python3-pip in localhost"
- yum:
- name: python3-pip
- state: latest
- tags:
- - requirements
- - name: "Installing pip3 modules for amazon.aws"
- pip:
- name: "{{ item }}"
- with_items:
- - boto3
- - botocore
- tags:
- - requirements
- - name: "creating a new EC2 key pair using lookup function"
- become: false
- amazon.aws.ec2_key:
- region: "{{ region }}"
- name: "{{ project_name }}-{{ project_env }}"
- key_material: "{{ lookup('file','{{ project_name }}-{{ project_env }}.pub') }}"
- state: "{{ state }}"
- - name: "Creating a Second key pair with default values"
- amazon.aws.ec2_key:
- region: "{{ region }}"
- name: "{{ project_name }}"
- state: "{{ state }}"
- register: key_status
- - name: "Printing the private_key value"
- when: key_status.changed == true
- debug:
- var: key_status
- - name: "Copying the private key to the current location"
- become: false
- when: key_status.changed == true
- copy:
- content: "{{ key_status.key.private_key }}"
- dest: "{{ project_name }}.pem"
- mode: 0400
- - name: "Create a security group name {{ project_name }}-{{ project_env }}"
- amazon.aws.ec2_security_group:
- name: "{{ project_name }}-{{ project_env }}"
- description: "{{ project_name }}-{{ project_env }}"
- region: "{{ region }}"
- state: "{{ state }}"
- rules:
- - proto: tcp
- ports:
- - 80
- - 22
- - 443
- cidr_ip: 0.0.0.0/0
- tags:
- Name: "{{ project_name }}-{{ project_env }}-webserver"
- Env: "{{ project_env }}"
- Project: "{{ project_name }}"
- register: sg_status
- tags:
- - prov
- - destroy
- - name: "set_fact"
- set_fact:
- project_name: "{{ project_name }}"
- project_env: "{{ project_env }}"
- region: "{{ region }}"
- sg_status: "{{ sg_status }}"
- tags:
- - prov
- - destroy
- - name: "Creating {{ project_name }}-{{ project_env }} ec2-intsances"
- amazon.aws.ec2_instance:
- name: "{{ project_name }}-{{ project_env }}-webserver"
- state: "{{ state_instance }}"
- region: "{{ region }}"
- instance_type: t2.micro
- key_name: "{{ project_name }}-{{ project_env }}"
- image_id: ami-0b9ecf71fe947bbdd
- exact_count: 4
- filters:
- "tag:Name": "{{ project_name }}-{{ project_env }}-webserver"
- "tag:Env": "{{ project_env }}"
- "tag:Project": "{{ project_name }}"
- security_groups:
- - "{{ sg_status.group_id }}"
- tags:
- Name: "{{ project_name }}-{{ project_env }}-webserver"
- Env: "{{ project_env }}"
- Project: "{{ project_name }}"
- register: instance_status
- tags:
- - prov
- - name: "Creating a dynamic Inventory"
- add_host:
- hostname: "{{ item.private_ip_address }}"
- groups: "{{ project_name }}-{{ project_env }}"
- ansible_ssh_hosts: "{{ item.private_ip_address }}"
- ansible_ssh_user: ec2-user
- ansible_ssh_private_key_file: "{{ project_name }}-{{ project_env }}"
- ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
- ansible_python_interpreter: auto_silent
- with_items:
- - "{{ instance_status.instances }}"
- tags:
- - prov
- - name: "wait for 60 sec"
- wait_for:
- timeout: 60
- tags:
- - prov
- - name: "Provisioning httpd and php for {{ hostvars.localhost.project_name }}-{{ hostvars.localhost.project_env }}"
- vars:
- packages:
- - httpd
- - php-fpm
- httpd_owner: apache
- hosts: "{{ hostvars.localhost.project_name }}-{{ hostvars.localhost.project_env }}"
- become: true
- tags:
- - prov
- - never
- tasks:
- - name: "Install packages for php-website"
- when: hostvars.localhost.instance_status.changed == true
- yum:
- name: "{{ packages }}"
- state: present
- - name: "Sevice restart"
- when: hostvars.localhost.instance_status.changed == true
- service:
- name: "{{ item }}"
- state: started
- enabled: true
- with_items:
- - "{{ packages }}"
- - name: "Setting the index.html file"
- when: hostvars.localhost.instance_status.changed == true
- copy:
- content: "<h1><center>{{ hostvars.localhost.project_name }}-{{ hostvars.localhost.project_env }} Version 1</center></h1>"
- dest: /var/www/html/index.html
- owner: "{{ httpd_owner }}"
- group: "{{ httpd_owner }}"
- - name: "Delete aws infra"
- hosts: localhost
- tags:
- - destroy
- tasks:
- - name: "Deleting {{ project_name }}-{{ project_env }} ec2-intsance"
- amazon.aws.ec2_instance:
- region: "{{ region }}"
- filters:
- "tag:Name": "{{ project_name }}-{{ project_env }}-webserver"
- "tag:Env": "{{ project_env }}"
- "tag:Project": "{{ project_name }}"
- state: absent
- - name: "Delete the security group name {{ project_name }}-{{ project_env }}"
- amazon.aws.ec2_security_group:
- region: "{{ region }}"
- group_id: "{{ sg_status.group_id }}"
- state: absent
- - name: "Deleting key pair {{ project_name }}-{{ project_env }}"
- amazon.aws.ec2_key:
- name: "{{ item }}"
- region: "{{ region }}"
- state: absent
- with_items:
- - "{{ project_name }}-{{ project_env }}"
- - "{{ project_name }}"
- - name: "Deleting the keypair in localmachine"
- file:
- path: "{{ item }}"
- state: absent
- with_items:
- - "{{ project_name }}-{{ project_env }}.pub"
- - "{{ project_name }}-{{ project_env }}"
- - "{{ project_name }}.pem"
- #### Output with infra and prov tags ####
- [ec2-user@ip-172-31-15-6 ~]$ ansible-playbook ansible.yml -t infra,prov --skip-tags=requirements
- PLAY [Creating the zomato webserver infra] *****************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [localhost]
- TASK [Creating a new ssh keypair] **************************************************************************************
- changed: [localhost]
- TASK [set_fact] ********************************************************************************************************
- ok: [localhost]
- TASK [creating a new EC2 key pair using lookup function] ***************************************************************
- changed: [localhost]
- TASK [Creating a Second key pair with default values] ******************************************************************
- changed: [localhost]
- TASK [Printing the private_key value] **********************************************************************************
- ok: [localhost] => {
- "key_status": {
- "changed": true,
- "failed": false,
- "key": {
- "fingerprint": "eb:2a:a3:ac:f2:6b:5b:2a:5c:ae:72:b8:ee:89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7\noqMSchYvXLWkiWtzc/aiI03TIkm3UoCbTt4rhz6szR+Wdu9fYuIO8CBGdLUQkin3\nc9FYG1haHftFpFDCxta2bzy+VbDihglQHaS5msjfAoGBAK86LD/g6z+LenC1bh/x\n7wJ6Wp2m8DAe4aySyNBP8sRl/k/aM2EZtzTrHuijq011kOE+MC7iuN9tkB3PjBUD\nxRx7aytmSvop4pnFYi3DjRAkV0TU3MfRGGzt/fcQU41Ia1EVOyi3/bhiLD37aQpy\nL8KI+fnxFE296EQextOlIvbRAoGAQqGti7xVD6on/sO2eQvniqy9+l4ZeE755Pw4\nQ/Ap/tAH8VA6nGZCGpEVS5TvcxkUld1FaU/PxXyezcEuS8EjLXnYfAtw1YlJV5hU\nXQlxHlObvcz/tTUPNgHRaVWMPI3BaALdhiQqRkEL86Zj4PxR5gNH79hcGGFwG3Ha\nf0Mi0e0CgYBDsO/cYs6TTjs+BvJ9OcAiJMgdkJVvJWFBZckMYvOg5ZV35Y1rawrb\nM9hb835AioNVQlaZ/FRxcs9A9c/YTuShRWwyTH6S1erx/ah1ekg9O/qTmgYj778F\ndSPq14Utvuy51lKzIgW0RkAW59zjhd7Jf0AAytFG8CnpfUSkVzTTrA==\n-----END RSA PRIVATE KEY-----",
- "tags": {}
- },
- "msg": "key pair created"
- }
- }
- TASK [Copying the private key to the current location] *****************************************************************
- changed: [localhost]
- TASK [Create a security group name zomato-dev] *************************************************************************
- changed: [localhost]
- TASK [Creating 3 ec2-intsance] *****************************************************************************************
- changed: [localhost]
- TASK [Creating a dynamic Inventory] ************************************************************************************
- TASK [wait for 60 sec] *************************************************************************************************
- ok: [localhost]
- PLAY [Provisioning httpd and php for zomato-dev] ***********************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.39.23]
- ok: [172.31.44.39]
- ok: [172.31.46.17]
- TASK [Install packages for php-website] ********************************************************************************
- changed: [172.31.39.23]
- changed: [172.31.44.39]
- changed: [172.31.46.17]
- TASK [Sevice restart] **************************************************************************************************
- changed: [172.31.39.23] => (item=httpd)
- changed: [172.31.44.39] => (item=httpd)
- changed: [172.31.46.17] => (item=httpd)
- changed: [172.31.39.23] => (item=php-fpm)
- changed: [172.31.44.39] => (item=php-fpm)
- changed: [172.31.46.17] => (item=php-fpm)
- TASK [Setting the index.html file] *************************************************************************************
- changed: [172.31.39.23]
- changed: [172.31.46.17]
- changed: [172.31.44.39]
- PLAY [Delete aws infra] ************************************************************************************************
- PLAY RECAP *************************************************************************************************************
- 172.31.39.23 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.44.39 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.46.17 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- localhost : ok=11 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- #### output after updating ec2-instance to 4 ####
- [ec2-user@ip-172-31-15-6 ~]$ ansible-playbook ansible.yml -t prov --skip-tags=requirements
- PLAY [Creating the zomato webserver infra] *****************************************************************************
- TASK [set_fact] ********************************************************************************************************
- ok: [localhost]
- TASK [Create a security group name zomato-dev] *************************************************************************
- ok: [localhost]
- TASK [Creating zomato-dev ec2-intsances] *******************************************************************************
- changed: [localhost]
- TASK [Creating a dynamic Inventory] ************************************************************************************
- TASK [wait for 60 sec] *************************************************************************************************
- ok: [localhost]
- PLAY [Provisioning httpd and php for zomato-dev] ***********************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.39.23]
- ok: [172.31.46.17]
- ok: [172.31.44.39]
- ok: [172.31.47.178]
- TASK [Install packages for php-website] ********************************************************************************
- ok: [172.31.39.23]
- ok: [172.31.46.17]
- ok: [172.31.44.39]
- changed: [172.31.47.178]
- TASK [Sevice restart] **************************************************************************************************
- ok: [172.31.39.23] => (item=httpd)
- ok: [172.31.44.39] => (item=httpd)
- ok: [172.31.46.17] => (item=httpd)
- ok: [172.31.39.23] => (item=php-fpm)
- ok: [172.31.46.17] => (item=php-fpm)
- ok: [172.31.44.39] => (item=php-fpm)
- changed: [172.31.47.178] => (item=httpd)
- changed: [172.31.47.178] => (item=php-fpm)
- TASK [Setting the index.html file] *************************************************************************************
- ok: [172.31.44.39]
- ok: [172.31.39.23]
- ok: [172.31.46.17]
- changed: [172.31.47.178]
- PLAY [Delete aws infra] ************************************************************************************************
- PLAY RECAP *************************************************************************************************************
- 172.31.39.23 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.44.39 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.46.17 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 172.31.47.178 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- localhost : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- #### output when no change ####
- [ec2-user@ip-172-31-15-6 ~]$ ansible-playbook ansible.yml -t prov --skip-tags=requirements
- PLAY [Creating the zomato webserver infra] *****************************************************************************
- TASK [set_fact] ********************************************************************************************************
- ok: [localhost]
- TASK [Create a security group name zomato-dev] *************************************************************************
- ok: [localhost]
- TASK [Creating zomato-dev ec2-intsances] *******************************************************************************
- ok: [localhost]
- TASK [Creating a dynamic Inventory] ***********************************************************************************
- TASK [wait for 60 sec] *************************************************************************************************
- ok: [localhost]
- PLAY [Provisioning httpd and php for zomato-dev] ***********************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [172.31.39.23]
- ok: [172.31.44.39]
- ok: [172.31.47.178]
- ok: [172.31.46.17]
- TASK [Install packages for php-website] ********************************************************************************
- skipping: [172.31.46.17]
- skipping: [172.31.39.23]
- skipping: [172.31.44.39]
- skipping: [172.31.47.178]
- TASK [Sevice restart] **************************************************************************************************
- skipping: [172.31.46.17] => (item=httpd)
- skipping: [172.31.46.17] => (item=php-fpm)
- skipping: [172.31.46.17]
- skipping: [172.31.39.23] => (item=httpd)
- skipping: [172.31.39.23] => (item=php-fpm)
- skipping: [172.31.39.23]
- skipping: [172.31.44.39] => (item=httpd)
- skipping: [172.31.44.39] => (item=php-fpm)
- skipping: [172.31.44.39]
- skipping: [172.31.47.178] => (item=httpd)
- skipping: [172.31.47.178] => (item=php-fpm)
- skipping: [172.31.47.178]
- TASK [Setting the index.html file] *************************************************************************************
- skipping: [172.31.46.17]
- skipping: [172.31.39.23]
- skipping: [172.31.44.39]
- skipping: [172.31.47.178]
- PLAY [Delete aws infra] ************************************************************************************************
- PLAY RECAP *************************************************************************************************************
- 172.31.39.23 : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
- 172.31.44.39 : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
- 172.31.46.17 : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
- 172.31.47.178 : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
- localhost : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- #### Terminating the infra ####
- [ec2-user@ip-172-31-15-6 ~]$ ansible-playbook ansible.yml -t destroy --skip-tags=requirements
- [WARNING]: No inventory was parsed, only implicit localhost is available
- [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
- 'all'
- [WARNING]: While constructing a mapping from /home/ec2-user/ansible.yml, line 7, column 5, found a duplicate dict key
- (region). Using last defined value only.
- PLAY [Creating the zomato webserver infra] *****************************************************************************
- TASK [Create a security group name zomato-dev] *************************************************************************
- ok: [localhost]
- TASK [set_fact] ********************************************************************************************************
- ok: [localhost]
- [WARNING]: Could not match supplied host pattern, ignoring: zomato-dev
- PLAY [Provisioning httpd and php for zomato-dev] ***********************************************************************
- skipping: no hosts matched
- PLAY [Delete aws infra] ************************************************************************************************
- TASK [Gathering Facts] *************************************************************************************************
- ok: [localhost]
- TASK [Deleting zomato-dev ec2-intsance] ********************************************************************************
- changed: [localhost]
- TASK [Delete the security group name zomato-dev] ***********************************************************************
- changed: [localhost]
- TASK [Deleting key pair zomato-dev] ************************************************************************************
- changed: [localhost] => (item=zomato-dev)
- changed: [localhost] => (item=zomato)
- TASK [Deleting the keypair in localmachine] ****************************************************************************
- changed: [localhost] => (item=zomato-dev.pub)
- changed: [localhost] => (item=zomato-dev)
- changed: [localhost] => (item=zomato.pem)
- PLAY RECAP *************************************************************************************************************
- localhost : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Advertisement
Add Comment
Please, Sign In to add comment