SHARE
TWEET

Untitled

a guest May 26th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---
  2.   - name: Provision an EC2 Instance
  3.     hosts: local
  4.     connection: local
  5.     gather_facts: False
  6.     tags: provisioning
  7.     # Necessary Variables for creating/provisioning the EC2 Instance
  8.     vars:
  9.       instance_type: t2.micro
  10.       security_group: ansible-webserver # Change the security group name here
  11.       image: ami-719fb712 # This is an AMI i created myself
  12.       keypair: agix-key # This is one of my keys that i already have in AWS
  13.       region: ap-southeast-2 # Change the Region
  14.       count: 1
  15.  
  16.     # Task that will be used to Launch/Create an EC2 Instance
  17.     tasks:
  18.  
  19.       - name: Create a security group
  20.         local_action:
  21.           module: ec2_group
  22.           name: "{{ security_group }}"
  23.           description: Security Group for webserver Servers
  24.           region: "{{ region }}"
  25.           rules:
  26.             - proto: tcp
  27.               from_port: 22
  28.               to_port: 22
  29.               cidr_ip: 0.0.0.0/0
  30.             - proto: tcp
  31.               from_port: 80
  32.               to_port: 80
  33.               cidr_ip: 0.0.0.0/0
  34.             - proto: tcp
  35.               from_port: 443
  36.               to_port: 443
  37.               cidr_ip: 0.0.0.0/0
  38.           rules_egress:
  39.             - proto: all
  40.               cidr_ip: 0.0.0.0/0
  41.         register: basic_firewall
  42.  
  43.       - name: Launch the new EC2 Instance
  44.         local_action: ec2
  45.                       group={{ security_group }}
  46.                       instance_type={{ instance_type}}
  47.                       image={{ image }}
  48.                       wait=true
  49.                       region={{ region }}
  50.                       keypair={{ keypair }}
  51.                       count={{count}}
  52.         register: ec2
  53.  
  54.       - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
  55.         local_action: lineinfile
  56.                       dest="./hosts"
  57.                       regexp={{ item.public_ip }}
  58.                       insertafter="[webserver]" line={{ item.public_ip }}
  59.         with_items: ec2.instances
  60.  
  61.  
  62.       - name: Wait for SSH to come up
  63.         local_action: wait_for
  64.                       host={{ item.public_ip }}
  65.                       port=22
  66.                       state=started
  67.         with_items: ec2.instances
  68.  
  69.       - name: Add tag to Instance(s)
  70.         local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
  71.         with_items: ec2.instances
  72.         args:
  73.           tags:
  74.             Name: webserver
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top