Skip to content

Instantly share code, notes, and snippets.

@msgre
Last active April 24, 2016 07:00
Show Gist options
  • Save msgre/4f83a4c0bd540e1d0494 to your computer and use it in GitHub Desktop.
Save msgre/4f83a4c0bd540e1d0494 to your computer and use it in GitHub Desktop.
Pomocné soubory k talku o Ansible, viz https://github.com/msgre/ansible-talk. Soubory nginx.conf.j2 a content.html.j2 ulož do adresáře templates.
<html>
<body>
<h1>Nazdar, ja jsem <span style="color:red">{{ansible_hostname}}</span></h1>
<pre>{{ansible_eth1|pprint}}</pre>
</body>
</html>
remote1.vbox ansible_ssh_host=172.16.1.21
remote2.vbox ansible_ssh_host=172.16.1.22
remote3.vbox ansible_ssh_host=172.16.1.23
[lachtani]
remote[1:3].vbox
server {
listen 80;
server_name {{ansible_hostname}}.vbox;
root {{static_dir}};
index index.html index.htm;
}
---
- hosts: lachtani
sudo: true
vars:
static_dir: /var/static/
tasks:
- name: Install Nginx
apt: pkg=nginx state=present
- name: Prepare directory for static content
file: path={{static_dir}} state=directory
- name: Remove default Nginx configuration
file: path=/etc/nginx/sites-enabled/default state=absent
- name: Configure Nginx
template: src=templates/nginx.conf.j2 dest=/etc/nginx/sites-enabled/demo
notify:
- Restart nginx
- name: Ensure Nginx is running
service: name=nginx state=started
- name: Generate static content for webserver
template: src=templates/content.html.j2 dest={{static_dir}}/index.html
tags:
- deploy
handlers:
- name: Restart nginx
service: name=nginx state=restarted
# -*- mode: ruby -*-
# vi: set ft=ruby :
# control masina, ze ktere budeme vsecko ovladat
$control_script = <<SCRIPT
if [ ! -f /home/vagrant/.ssh/id_rsa.pub ]; then
ssh-keygen -t rsa -N '' -f /home/vagrant/.ssh/id_rsa
chown vagrant:vagrant /home/vagrant/.ssh/id_rsa*
cp /home/vagrant/.ssh/id_rsa.pub /vagrant/
echo "172.16.1.21 remote1.vbox" >> /etc/hosts
echo "172.16.1.22 remote2.vbox" >> /etc/hosts
echo "172.16.1.23 remote3.vbox" >> /etc/hosts
fi
SCRIPT
Vagrant.configure("2") do |config|
config.vm.define :control do |control_config|
control_config.vm.box = "ubuntu1404"
control_config.vm.hostname = "control.vbox"
control_config.vm.network :private_network, ip: "172.16.1.20"
control_config.vm.provision "shell", inline: $control_script
control_config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "512"]
vb.name = "control.vbox"
end
end
end
# remote masiny, tj. ty ktere budou rizene z vnejsku
$remote_script = <<SCRIPT
if [ -f /vagrant/id_rsa.pub ]; then
cat /vagrant/id_rsa.pub >> /home/vagrant/.ssh/authorized_keys
fi
SCRIPT
Vagrant.configure("2") do |config|
config.vm.define :remote1 do |remote1_config|
remote1_config.vm.box = "ubuntu1404"
remote1_config.vm.hostname = "remote1.vbox"
remote1_config.vm.network :private_network, ip: "172.16.1.21"
remote1_config.vm.provision "shell", inline: $remote_script
remote1_config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "384"]
vb.name = "remote1.vbox"
end
end
end
Vagrant.configure("2") do |config|
config.vm.define :remote2 do |remote2_config|
remote2_config.vm.box = "ubuntu1404"
remote2_config.vm.hostname = "remote2.vbox"
remote2_config.vm.network :private_network, ip: "172.16.1.22"
remote2_config.vm.provision "shell", inline: $remote_script
remote2_config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "384"]
vb.name = "remote2.vbox"
end
end
end
Vagrant.configure("2") do |config|
config.vm.define :remote3 do |remote3_config|
remote3_config.vm.box = "ubuntu1404"
remote3_config.vm.hostname = "remote3.vbox"
remote3_config.vm.network :private_network, ip: "172.16.1.23"
remote3_config.vm.provision "shell", inline: $remote_script
remote3_config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "384"]
vb.name = "remote3.vbox"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment