Created
February 23, 2020 15:16
-
-
Save paul-lupu/5e6247e972a9c8b4778cecc2af7d59ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AWS Playbook | |
--- | |
- hosts: localhost | |
connection: local | |
gather_facts: False | |
vars: | |
key_name: troc-ec2-key | |
region: us-east-1 | |
image: ami-08d35f180f6c2f2d4 | |
sec_group: troc-jenkins-sg | |
profile: troc-jenkins-role | |
vpc_subnet: subnet-da78dc82 | |
size: t2.micro | |
tasks: | |
- name: Create ec2 instance | |
register: output | |
ec2: | |
instance_profile_name: "{{profile}}" | |
key_name: "{{key_name}}" | |
vpc_subnet_id: "{{vpc_subnet}}" | |
instance_type: "{{size}}" | |
group: "{{sec_group}}" | |
region: "{{region}}" | |
user_data: "{{ lookup('file', 'files/provision-query-api.sh') }}" | |
assign_public_ip: true | |
image: "{{image}}" | |
wait: true | |
instance_tags: | |
Environment: Testing | |
count: 1 | |
Name: navigator-api | |
- name: add host to inventory | |
add_host: name={{ item.private_ip }} groups=newinstances ansible_user=ubuntu ansible_ssh_private_key_file=/root/.ssh/id_rsa | |
with_items: "{{output.instances}}" | |
- hosts: newinstances | |
gather_facts: false | |
tasks: | |
- name: wait for remote connection | |
wait_for_connection: | |
timout: 60 | |
connect_timeout: 10 | |
- name: Wait for user data to finish | |
raw: test -f /var/lib/cloud/instance/boot-finished | |
retries: 50 | |
register: cmd_res | |
changed_when: false | |
until: cmd_res | success | |
- hosts: localhost | |
connection: local | |
gather_facts: false | |
vars: | |
date: "{{ lookup('pipe', 'date +%Y%m%d-%H') }}" | |
tasks: | |
- name: create AMI from instance | |
ec2_ami: | |
region: us-east-1 | |
instance_id: "{{output.instance_ids[0]}}" | |
name: "navigator-api-{{date}}" | |
wait: yes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@jenkins01:~/ansible_ami_builder/files# cat provision-query-api.sh | |
#!/bin/bash | |
echo > /root/output.txt | |
exec &>> /root/output.txt | |
apt update; | |
apt install -y awscli git; | |
aws s3 cp s3://navigator-imagebuilder/bitbucket_private_rsa /root/.ssh/id_rsa; | |
chmod 400 /root/.ssh/id_rsa; | |
echo "StrictHostKeyChecking no" > /root/.ssh/config; | |
cd /home/troc/query_api && git checkout master && git pull --rebase || true; | |
cd /home/troc/query_api && git checkout master && git pull --rebase || true; | |
/home/troc/query_api/.venv/bin/python -m pip install -r requirements/requirements.txt; | |
export DEBIAN_FRONTEND=noninteractive | |
export DEBIAN_PRIORITY=critical | |
apt-get -qy -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold" upgrade | |
apt-get -qy autoclean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@jenkins01:~/ansible_ami_builder/files# cat wait.sh | |
#!/bin/bash | |
until [ -f /var/lib/cloud/instance/boot-finished ]; do | |
echo "Waiting for user data script to finish" | |
sleep 1; | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment