This post will show you how to install Ansible and use Ansible to create a instance EC2 in AWS.
There are several way to install Ansible. In this post, I choose the way that uses PIP to install Ansible.
Install PIP, Python Boto:
1 |
apt-get install python-pip python-boto python-dev libffi-dev |
We need python-boto to make Ansible is able to work AWS
Install Ansible:
1 |
pip install ansible |
Now, we need to add AWS Access Key to make Boto can access to your AWS account:
1 2 |
export AWS_ACCESS_KEY_ID="your_aws_access_key" export AWS_SECRET_ACCESS_KEY="your_aws_secret_key" |
In the next step, you need create a folder for your project:
1 2 |
mkdir ansible-ec2 cd ansible-ec2 |
Create hosts file with the content below:
1 2 3 4 |
[local] localhost [minions] |
And create a provision file name ec2_launch.yml with the content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
--- - name: Provision an EC2 Instance hosts: local connection: local gather_facts: False tags: provisioning vars: instance_type: t2.micro security_group: minions image: ami-2dad0149 region: ap-southeast-1 keypair: minions count: 1 tasks: - name: Create a security group local_action: module: ec2_group name: "{{ security_group }}" description: Security Group for webserver Servers region: "{{ region }}" rules: - proto: tcp type: ssh from_port: 22 to_port: 22 cidr_ip: 0.0.0.0/0 - proto: tcp from_port: 80 to_port: 80 cidr_ip: 0.0.0.0/0 - name: Launch the new EC2 Instance local_action: ec2 group={{ security_group }} instance_type={{ instance_type}} image={{ image }} wait=true region={{ region }} keypair={{ keypair }} count={{count}} register: ec2 - name: Add the newly created EC2 instance(s) to the local host group local_action: lineinfile dest="./hosts" regexp={{ item.public_ip }} insertafter="[minions]" line={{ item.public_ip }} with_items: ec2.instances - name: Wait for SSH to come up local_action: wait_for host={{ item.public_ip }} port=22 state=started with_items: ec2.instances - name: Add tag to Instance(s) local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present with_items: ec2.instances args: tags: Name: minions |
Please custom the source code above for your case with the guide:
1 2 3 4 5 6 7 |
vars: instance_type: t2.micro # instance type security_group: minions # AWS security group, will be created image: ami-2dad0149 # ami file for your instance region: ap-southeast-1 keypair: minions # your key pair count: 1 |
Finally, just run a command to create EC2 instance:
1 |
ansible-playbook -i hosts ec2_launch.yml |
If you what to run a series of commands in all the instances you created. You should create a run_test.yml file with content:
1 2 3 4 5 6 7 |
--- - name: Run Test Script hosts: minions tasks: - shell: echo 'test script' #should change to test script args: chdir: /home/ubuntu |
And run the test by Ansible Playbook:
1 |
ansible-playbook -i hosts -u ubuntu --sudo --sudo-user root run_test.yml -vvvv |