Skip to content

Instantly share code, notes, and snippets.

@toripiyo
Last active August 29, 2015 14:15
Show Gist options
  • Save toripiyo/9927fe4fe3cd957bc78d to your computer and use it in GitHub Desktop.
Save toripiyo/9927fe4fe3cd957bc78d to your computer and use it in GitHub Desktop.
vagrant and ansible tutorial

fabric tutorial

what is fabric

Fabric = Shell Script + Python + Useful Methods

install fabric

pip install fabric
fabric -V

try small sample

check server info

  • shell command can be directly used.
mkdir fabric; cd fabric
vi fabfile.py
------------------------------------
from fabric.api import *

def serverinfo():
	run('hostname')
	run('cat /etc/issue')
	run('uname -m')
------------------------------------

check mount information of multiple servers

  • password prompt appears only one time.
  • very quick compared to manual operation.
vi fabfile.py
------------------------------------
from fabric.api import *
import sys

lines = sys.stdin.read().splitlines()
env.hosts = filter(bool, lines)
print "target hosts: %r" % env.hosts

def mount():
	run('cat /etc/issue')
	run('mount | grep ^n | grep -v ^none')
------------------------------------

vi servers.txt
------------------------------------
192.168.33.12
------------------------------------

cat servers.txt | fab -I mount > mome.txt

fab command option

option description
-I force pasword prompt up-front.
-H comma-separated list of host to operate on
-u username to use when connecting to remote hosts

reference

overview and tutorial

今日からすぐに使えるデプロイ・システム管理ツール Fabric 入門

Fabric Python Developers Festa 2013.03 #pyfes

farbicを使って複数のサーバを操作する

ansible workshop

set vagrant for ansible

set vagrant box

centos-7.0

vagrant box add centos7 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-7.0_chef-provisionerless.box

centos-6.5

vagrant box add chef/centos-7.0

set Vagrantfile

centos-7.0

[16:57:05 - 15-03-01] /Users/toripiyo/vagrant/ansible7 % cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.define :node1 do |node|
    node.vm.box = "chef/centos-7.0"
    node.vm.network :forwarded_port, guest: 22, host: 2001, id: "ssh"
    node.vm.network :private_network, ip: "192.168.33.11"
  end

  config.vm.define :node2 do |node|
    node.vm.box = "chef/centos-7.0"
    node.vm.network :forwarded_port, guest: 22, host: 2002, id: "ssh"
    node.vm.network :forwarded_port, guest: 80, host: 8000, id: "http"
    node.vm.network :private_network, ip: "192.168.33.12"
  end

end

centos-6.5

[19:18:05 - 15-03-01] /Users/toripiyo/vagrant/ansible % cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.define :node1 do |node|
    node.vm.box = "chef/centos-6.5"
    node.vm.network :forwarded_port, guest: 22, host: 2001, id: "ssh"
    node.vm.network :private_network, ip: "192.168.33.11"
  end

  config.vm.define :node2 do |node|
    node.vm.box = "chef/centos-6.5"
    node.vm.network :forwarded_port, guest: 22, host: 2002, id: "ssh"
    node.vm.network :forwarded_port, guest: 80, host: 8000, id: "http"
    node.vm.network :private_network, ip: "192.168.33.12"
  end

end

boot machines

vagrant up

*vagrant version 1.7.2 is recommended

create secret key

[21:08:32 - 15-02-21] /Users/toripiyo/vagrant/ansible % vagrant ssh-config node1 > ssh_config
[21:08:49 - 15-02-21] /Users/toripiyo/vagrant/ansible % vagrant ssh-config node2 >> ssh_config
[21:12:23 - 15-02-21] /Users/toripiyo/vagrant/ansible % scp -F ssh_config ~/.vagrant.d/insecure_private_key node1:.ssh/id_rsa
insecure_private_key

##set up ansible

install python and ansible

vagrant ssh node1
sudo yum install bzip2-devel sqlite-devel git patch gcc zlib zlib-devel readline readline-devel openssl openssl-devel
mkdir ansible
cd ansible
git clone https://github.com/yyuu/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
exec $SHELL
pyenv install 2.7.9
pyenv local 2.7.9
pip install ansible
ansible --version

ansile test

ping

echo 192.168.33.12 > hosts
ansible --ask-pass -i hosts 192.168.33.12 -m ping -c paramiko
SSH password:
192.168.33.12 | success >> {
    "changed": false,
    "ping": "pong"
}

reference

ansible setup playbook

a-simple-insecure-way-to-use-ansible

install zabbix

mysql configuration

$ mysql -u root -p
mysql> CREATE USER zabbix;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE DATABASE zabbix;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON zabbix.* TO zabbix@localhost IDENTIFIED BY ‘<パスワード>’;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

reference

http://knowledge.sakura.ad.jp/tech/585/2/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment