Action disabled: source

ansible

Ubuntu 13.04

$ which pip
/usr/local/bin/pip

// パッケージから入れる
// sudo apt-get install python-pip

$ sudo pip install ansible

例えば service モジュールの説明を読む:

$ ansible-doc service

シェルから

ssh username@server1.example.com

ができる状態に ~/.ssh/config などを整備して:

$ mkdir ansible
$ cd ansible
$ echo server1.example.com ansible_ssh_user=username >> hosts
$ ansible -i hosts server1.example.com -m ping
server1.example.com | success >> {
    "changed": false, 
    "ping": "pong"
}

つぎにアドレス aaa.bbb.ccc.ddd の server2 に

$ ssh -p nnnnn username@aaa.bbb.ccc.ddd

ができることを確認して:

$ echo server2 ansible_ssh_port=nnnnn ansible_ssh_host=aaa.bbb.ccc.ddd ansible_ssh_user=username >> hosts
$ ansible -i hosts server2 -m ping
server2 | success >> {
    "changed": false, 
    "ping": "pong"
}

これらはまとめて実行できる:

$ ansible -i hosts all -m ping
server1 | success >> {
    "changed": false, 
    "ping": "pong"
}

server2 | success >> {
    "changed": false, 
    "ping": "pong"
}

任意のコマンドは -a で実行できる:

$ ansible -i hosts all -a 'uname -r'

$ ansible -i hosts all -a 'free -m'

$ ansible -i hosts all -a 'df -h'

モジュール shell (-m shell) を使うと pipe でコマンドを使える:

ansible -i hosts server3 --sudo -m shell -a 'grep XXXX /var/log/httpd/access_log-YYYY | wc'

verbose mode:

$ ansible -i hosts servers -m ping -vvvv

Hosts

可読性のために改行を入れている:

example.jp
 ansible_ssh_host=***.***.***.***
 ansible_ssh_user=****
 ansible_ssh_private_key_file=/Users/***/***.pem
example.com
 ansible_ssh_user=****
 ansible_sudo_pass=******
example.org
 ansible_ssh_port=*****
 ansible_ssh_host=***.***.***.***
 ansible_ssh_user=******
 ansible_sudo_pass=********

Playbook

Playbook を使ってみる。

リモートの Ubuntu サーバー(server2)に apache2 を入れる:

hosts:

[test-servers]
server2 ansible_ssh_port=nnnnn ansible_ssh_host=aaa.bbb.ccc.ddd ansible_ssh_user=username

playbook.yml:

---
- hosts: test-servers
  sudo: yes
  tasks:
    - name: be sure httpd is installed
      apt: name=apache2 state=present
$ ansible-playbook -i hosts playbook.yml --verbose --ask-sudo-pass

聞かれるパスワードは username ユーザーのパスワード。このユーザーでは sudo できるようにあらかじめ設定してあるものとする。

実行すると apache2 がインストールされて動き出す:

...

PLAY RECAP ******************************************************************** 
server2              : ok=2    changed=1    unreachable=0    failed=0   

もう一度 ansible-playbook を実行すると何も起こらない:

PLAY [test-servers] *********************************************************** 

GATHERING FACTS *************************************************************** 
ok: [server2]

TASK: [be sure httpd is installed] ******************************************** 
ok: [server2] => {"changed": false}

PLAY RECAP ******************************************************************** 
server2              : ok=2    changed=0    unreachable=0    failed=0   

サーバーを複数扱うときは、このやりかただと sudo するときのパスワードは共通でなくてはならない。

かわりに

http://docs.ansible.com/intro_inventory.html

に書かれている ansible_sudo_pass をインベントリの各サーバーに書けばよい。

Playbook を使わずに apache2 を止める:

$ ansible -i hosts server2 -m service -a "name=apache2 state=stopped"
server2 | success >> {
    "changed": true, 
    "name": "apache2", 
    "state": "stopped"
}

MacPorts

since 2014-04-20

sudo pip install ansible

すると下記に入ってしまい、パスが通らない現象。。

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/ansible

2014-08-09 ansible 1.7 にバージョンアップしてみた:

$ which ansible
/opt/local/bin/ansible

$ ls -l /opt/local/bin/ansible
lrwxr-xr-x  1 root  admin  71  6 22 13:57 /opt/local/bin/ansible -> /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/ansible

$ sudo pip-2.7 install --upgrade ansible

$ ansible --version
ansible 1.7
ansible.txt · 最終更新: 2014/08/09 14:58 by Takuya Nishimoto
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0