Skip to content

Instantly share code, notes, and snippets.

@lupupaulsv
Created February 23, 2020 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lupupaulsv/66000e9d919d2de8d1ae6be379302d02 to your computer and use it in GitHub Desktop.
Save lupupaulsv/66000e9d919d2de8d1ae6be379302d02 to your computer and use it in GitHub Desktop.
root@jenkins01:~/ansible_ami_builder# cat playbook.yaml
# 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
date: "{{ lookup('pipe', 'date +%Y%m%d-%H') }}"
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=files/key
with_items: "{{output.instances}}"
- hosts: newinstances
tasks:
- name: Wait for user data to finish
raw: test -f /var/lib/cloud/instance/boot-finished
retries: 20
register: cmd_res
changed_when: false
until: cmd_res | success
- hosts: localhost
tasks:
- name: create AMI from instance
ec2_ami:
region: "{{region}}"
instance_id: "{{output.instance_ids[0]}}"
name: "navigator-api-{{date}}"
wait: yes
fatal: [10.77.0.164]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 10.77.0.164 port 22: Connection timed out\r\n", "unreachable": true}
root@jenkins01:~/ansible_ami_builder# ssh ubuntu@10.77.0.164
Warning: Permanently added '10.77.0.164' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-1057-aws x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information disabled due to load higher than 1.0
* Multipass 1.0 is out! Get Ubuntu VMs on demand on your Linux, Windows or
Mac. Supports cloud-init for fast, local, cloud devops simulation.
https://multipass.run/
* Canonical Livepatch is available for installation.
- Reduce system reboots and improve kernel security. Activate at:
https://ubuntu.com/livepatch
66 packages can be updated.
43 updates are security updates.
Last login: Thu Jan 9 22:29:34 2020 from 10.77.0.253
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment