m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

CentOS6.4のへpython-2.7(ansible用)をインストール

Ansibleのテストをしてみたかったけど

[vagrant@localhost ~]$ python -V
Python 2.6.6
[vagrant@localhost ~]$

まじか

pythonはいろいろなツールと関連しているから簡単にバージョンアップできないようなイメージがあるけどどうなんだろう…

epelリポジトリなどを取り込み
sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo yum update -y
sudo yum -y groupinstall "Development Tools"
必要なライブラリを取り込む
sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
pythonをインストール
cd /var/tmp
git clone https://github.com/tagomoris/xbuild.git
sudo xbuild/python-install 2.7.6 /opt/python-2.7
echo 'PATH=/opt/python-2.7/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
sudo /opt/python-2.7/bin/pip install ansible

※ansibleは入れなくてもいいかもしれないけど他のサーバにも設定しないといけないのか…

確認
[vagrant@localhost tmp]$ source ~/.bashrc
[vagrant@localhost tmp]$ python -V
Python 2.7.6
[vagrant@localhost tmp]$

クライアント設定

macでいろいろ設定して実行

hostsファイルを作成
192.168.33.101
192.168.33.102
192.168.33.103
192.168.33.104
192.168.33.105
pingモジュールを実行
$ ansible -i hosts 192.168.33.105 -m ping
192.168.33.105 | success >> {
    "changed": false,
    "ping": "pong"
}

$
任意のコマンドを実施
 ansible -i hosts 192.168.33.103 -a 'uname -a'
192.168.33.103 | success | rc=0 >>
Linux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

$
yumモジュールでインストール
$ ansible -i hosts 192.168.33.102 -m yum -s -a name=telnet
192.168.33.102 | success >> {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirror.fairway.ne.jp\n * epel: ftp.kddilabs.jp\n * extras: mirror.fairway.ne.jp\n * rpmforge: ftp.neowiz.com\n * updates: mirror.fairway.ne.jp\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package telnet.x86_64 1:0.17-47.el6_3.1 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package         Arch            Version                    Repository     Size\n================================================================================\nInstalling:\n telnet          x86_64          1:0.17-47.el6_3.1          base           58 k\n\nTransaction Summary\n================================================================================\nInstall       1 Package(s)\n\nTotal download size: 58 k\nInstalled size: 109 k\nDownloading Packages:\nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r  Installing : 1:telnet-0.17-47.el6_3.1.x86_64                              1/1 \n\r  Verifying  : 1:telnet-0.17-47.el6_3.1.x86_64                              1/1 \n\nInstalled:\n  telnet.x86_64 1:0.17-47.el6_3.1                                               \n\nComplete!\n"
    ]
}

$

Playbook

hostsをグループで分ける
[demo-servers]
192.168.33.101
192.168.33.102

[demo-servers2]
192.168.33.103
192.168.33.104
192.168.33.105
sample-playbook.yml
---
- hosts: demo-servers
  sudo: yes
  tasks:
    - name: httpdサーバをインストール
      yum: name=httpd state=installed

    - name: httpdサーバを起動
      service: name=httpd state=running enabled=yes
----
チェック
$ ansible-playbook -i hosts sample-playbook.yml --syntax-check

playbook: sample-playbook.yml


$
タスク一覧確認
$ ansible-playbook -i hosts sample-playbook.yml --list-tasks

playbook: sample-playbook.yml

  play #1 (demo-servers):
    httpdサーバをインストール
    httpdサーバを起動

$
check実行
$ ansible-playbook -i hosts sample-playbook.yml --check

PLAY [demo-servers] ***********************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.33.101]
ok: [192.168.33.102]

TASK: [httpdサーバをインストール] *******************************************************
changed: [192.168.33.101]
changed: [192.168.33.102]

TASK: [httpdサーバを起動] ***********************************************************
failed: [192.168.33.101] => {"failed": true}
msg: cannot find 'service' binary or init script for service,  possible typo in service name?, aborting
failed: [192.168.33.102] => {"failed": true}
msg: cannot find 'service' binary or init script for service,  possible typo in service name?, aborting

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/Users/matsumotoshigeharu/sample-playbook.retry

192.168.33.101             : ok=2    changed=1    unreachable=0    failed=1
192.168.33.102             : ok=2    changed=1    unreachable=0    failed=1

$

httpdは入れていないのでエラーになってしまうようです

実行
$ ansible-playbook -i hosts sample-playbook.yml

PLAY [demo-servers] ***********************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.33.101]
ok: [192.168.33.102]

TASK: [httpdサーバをインストール] *******************************************************
changed: [192.168.33.101]
changed: [192.168.33.102]

TASK: [httpdサーバを起動] ***********************************************************
changed: [192.168.33.101]
changed: [192.168.33.102]

PLAY RECAP ********************************************************************
192.168.33.101             : ok=3    changed=2    unreachable=0    failed=0
192.168.33.102             : ok=3    changed=2    unreachable=0    failed=0

$

参考


Ansible チュートリアル | Ansible Tutorial in Japanese

助かりましたm(__)m