Last active
January 18, 2020 01:09
-
-
Save csiens/f2cc6b2efc7f0b1cfce034e1ea228e07 to your computer and use it in GitHub Desktop.
Packer docker builder with ansible provisioner demo
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
--- | |
- hosts: all | |
sudo: true | |
tasks: | |
- name: install nginx for ubuntu | |
apt: name=nginx state=latest | |
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' | |
- name: install nginx for centos | |
dnf: name=nginx state=latest | |
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' | |
- name: create sites-available directory ubuntu | |
file: | |
path: /etc/nginx/sites-available | |
state: directory | |
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' | |
- name: create sites-enabled directory ubuntu | |
file: | |
path: /etc/nginx/sites-enabled | |
state: directory | |
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' | |
- name: create the nginx config file ubuntu | |
copy: | |
dest: /etc/nginx/sites-available/default.cfg | |
content: | | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
root /html; | |
server_name _; | |
location / { | |
try_files $uri $uri/ =404; | |
} | |
} | |
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' | |
- name: create symlink ubuntu | |
file: | |
src: /etc/nginx/sites-available/default.cfg | |
dest: /etc/nginx/sites-enabled/default | |
state: link | |
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' | |
- name: create the nginx site config file centos | |
copy: | |
dest: /etc/nginx/conf.d/default.conf | |
content: | | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
root /html; | |
server_name _; | |
location / { | |
try_files $uri $uri/ =404; | |
} | |
} | |
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' | |
- name: create the nginx config file centos | |
copy: | |
dest: /etc/nginx/nginx.conf | |
content: | | |
user nginx; | |
worker_processes auto; | |
error_log /var/log/nginx/error.log; | |
pid /run/nginx.pid; | |
include /usr/share/nginx/modules/*.conf; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' | |
'$status $body_bytes_sent "$http_referer" ' | |
'"$http_user_agent" "$http_x_forwarded_for"'; | |
access_log /var/log/nginx/access.log main; | |
sendfile on; | |
tcp_nopush on; | |
tcp_nodelay on; | |
keepalive_timeout 65; | |
types_hash_max_size 2048; | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
include /etc/nginx/conf.d/*.conf; | |
} | |
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' | |
- name: restart nginx ubuntu | |
service: | |
name: nginx | |
state: restarted | |
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' | |
- name: restart nginx centos | |
systemd: | |
name: nginx | |
state: restarted | |
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' | |
- name: enable and start nginx on ubuntu | |
service: | |
name: nginx | |
enabled: yes | |
state: started | |
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' | |
- name: enable and start nginx on centos | |
systemd: | |
name: nginx | |
enabled: yes | |
state: started | |
when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' | |
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
{ | |
"variables": { | |
"ansible_host": "default", | |
"ansible_connection": "docker" | |
}, | |
"builders": [{ | |
"type": "docker", | |
"name": "ubuntu", | |
"image": "ubuntu:latest", | |
"commit": "true", | |
"run_command": [ | |
"-d", | |
"-i", | |
"-t", | |
"--name", | |
"{{user `ansible_host`}}", | |
"{{.Image}}", | |
"/bin/bash" | |
], | |
"changes": [ | |
"CMD [\"nginx\", \"-g\", \"daemon off;\"]" | |
] | |
},{ | |
"type": "docker", | |
"name": "centos", | |
"image": "centos:latest", | |
"commit": "true", | |
"run_command": [ | |
"-d", | |
"-i", | |
"-t", | |
"--name", | |
"{{user `ansible_host`}}", | |
"{{.Image}}", | |
"/bin/bash" | |
], | |
"changes": [ | |
"CMD [\"nginx\", \"-g\", \"daemon off;\"]" | |
] | |
}], | |
"provisioners": [ | |
{ | |
"type": "shell", | |
"only": ["ubuntu"], | |
"inline": [ | |
"apt-get update", | |
"apt-get install python -yq" | |
] | |
}, | |
{ | |
"type": "shell", | |
"only": ["centos"], | |
"inline": [ | |
"yum update -y", | |
"yum install python36 -y", | |
"ln -s /usr/bin/python3 /usr/bin/python", | |
"curl https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py > /usr/bin/systemctl", | |
"chmod +x /usr/bin/systemctl" | |
] | |
}, | |
{ | |
"type": "ansible", | |
"user": "root", | |
"playbook_file": "./playbook.yml", | |
"extra_arguments": [ | |
"--extra-vars", | |
"ansible_host={{user `ansible_host`}} ansible_connection={{user `ansible_connection`}}" | |
] | |
} | |
], | |
"post-processors": [ | |
[ | |
{ | |
"type": "docker-tag", | |
"repository": "csiens/nginx-ubuntu", | |
"tag": "latest", | |
"only": ["ubuntu"] | |
}, | |
{ | |
"type": "docker-tag", | |
"repository": "csiens/nginx-centos", | |
"tag": "latest", | |
"only": ["centos"] | |
}, | |
{ | |
"type": "docker-push", | |
"login": "true", | |
"login_username": "csiens", | |
"login_password": "" | |
} | |
] | |
] | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment