Ansible Playbook to setup Docker and launch EC2

Ansible Playbook to setup docker and launch ec2.


– hosts: localhost
remote_user: ec2-user
connection: local
become: true

vars_files:
– /etc/ansible/playbooks/vars/var.yml

tasks:
– name: Launchig EC2 instance
local_action: ec2
aws_access_key={{ aws_access_key }}
aws_secret_key={{ aws_secret_key }}
instance_type={{ instance_type }}
image={{ image }}
region={{ region }}
key_name={{ key_name }}
count={{ count }}
group={{ group }}
wait={{ wait }}
instance_tags={{ instance_tags }}
register: ec2

– name: Waiting
pause: minutes=2

– name: Creating Hosts group
add_host: hostname={{ item.public_ip }}
groupname=launched
with_items: ‘{{ ec2.instances }}’

– name: Waiting for SSH to come up
wait_for: host={{ item.public_dns_name }}
port=22
delay=60
timeout=320
state=started
with_items: ‘{{ ec2.instances }}’

– name: Install Docker in Ec2
hosts: launched
remote_user: ec2-user
become: true
gather_facts: true

vars:
service_name: docker
python_pip: python-pip

tasks:
– name: Installing Docker
yum: pkg={{ item }} state=latest
with_items:
– docker
– python-pip

– name: Checking Docker installed
stat: path=/etc/init.d/{{ service_name }}
register: service_status

– name: Starting Docker
service: name={{ service_name }} state=started
when: service_status.stat.exists
register: service_started

– name: Creating docker build directory
file: path=/mydir state=directory owner=root group=root mode=0755

– name: Copying local dockerfiles to ec2
copy:
src: /dockerfiles/
dest: /mydir/
owner: root
group: root
mode: 0755

– name: Switching to Build directory
command: chdir=/mydir/ ls

– name: Upgrading pip
command: pip install –upgrade pip

– name: Creating soft link
file: src=/usr/local/bin/pip dest=/usr/bin/pip state=link

– name: Installing docker-py
pip: name=docker-py state=latest

– name: Building raw docker
docker_image:
repository=docker.io/centos
name=rawdocker
dockerfile=dockerfile.java
path=/mydir
state=present
pull=false

– name: Building HTTPD docker
docker_image:
repository=rawdocker
name=httpddocker
dockerfile=dockerfile.web
path=/mydir
state=present
pull=false

– name: Running the launched docker container
docker_container:
name: container1
image: httpddocker
state: started
command: /usr/sbin/init
privileged: true
interactive: true
restart_policy: always
exposed_ports: 80
published_ports: 8081:80
hostname: mydocker.hadoop.com
tty: yes

—————————————————
This script is for Amazon Linux AMI (version 6)

in /etc/ansible/ansible.cfg

host_key_checking = False
gather_timeout = 30
timeout = 60

If you are launching with Jenkins, after installing docker and ansible plugins

Chmod 444 /etc/ansible/playbooks/mypair.pem

 

var.yml content

aws_access_key: ***********
aws_secret_key: *******************
instance_type: t2.micro
image: “ami-0b33d91d”
region: “us-east-1”
key_name: mypair
count: 1
group: “default”
wait: true
device: /dev/xvda
volume_size: 10
state: stopped
instance_tags: {“Name”:”Demo1″}
wget: wget

0Shares

2 Replies to “Ansible Playbook to setup Docker and launch EC2”

  1. Thank you for some other informative website.

    Where else may just I am getting that type of information written in such an ideal way?
    I’ve a challenge that I’m simply now operating on, and I’ve been at the look out for such info.

Leave a Reply

Your email address will not be published. Required fields are marked *