ユーザ用ツール

サイト用ツール


サイドバー

Index

はじめてのおつかい






DokuWiki整形記法


PlayGround



serverapps:configmgmt:ansible

Ansible

  • 構成管理(configuration management)
  • アプリケーション構築(application deployment)
  • オーケストレーション(orchestration)

を行うためPython製ツール
Ansibleの最大の特徴はエージェントレス

  • pythonがインストールされていること
  • sshでLogin可能なこと

上記が満たされていればOK

インストール(管理する側)

CentOS 6.6

epelリポジトリを設定1)する。epelレポジトリは普段は使用しないよう(enabled=0)にしておく

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/epel.repo

インストール

# yum install --enablerepo=epel ansible

確認

# ansible --version                                                           
  ansible 1.7.2

以下のようなWarnが出る場合があるが、支障がないので放置2)

 [WARNING]: The version of gmp you have installed has a known issue regarding
timing vulnerabilities when used with pycrypto. If possible, you should update
it (ie. yum update gmp).

2015.10.13:追記
6.7でsshパスワードを入力しようとすると以下のエラー

FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program

以下のオプションでOKという話だったが、うちの環境ではだめ

-c paramiko

結局、素直にsshpassをインストール

yum install --enablerepo=epel sshpass

この状態でアクセスすると以下のエラー

FAILED => Using a SSH password instead of a key is not possible 
because Host Key checking is enabled and sshpass does not support this.  
Please add this host's fingerprint to your known_hosts file to manage this host.

ssh-keyscan でホストキーを known_hosts に登録

ssh-keyscan 192.168.xx.yyy >> ~/.ssh/known_hosts

FreeBSD 10.1

インストール

# pkg install ansible

確認

# ansible --version                                                           
  ansible 1.7.2

pip

pipをインストール

# curl -kL https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

Ansibleのインストール必要なパッケージをインストール(CentOS)

yum -y install gcc python-devel

ansibleのインストール

# pip install ansible

インストール(リモートホスト)

  • pythonがインストールされていること
  • sshでLogin可能なこと

上記が満たされていればOK

設定

/usr/local/etc/ansible/hosts3) にリモートホストをini形式で記述する4)

192.168.1.xxx
host.example.com
[serevrs]
host1
host2
......

ユーザ名、秘密鍵の指定も可能

192.168.1.xxx ansible_ssh_user=ansibleuser ansible_ssh_private_key_file=/home/ansibleuser/.ssh/ansibleuser.pem

linuxからFreeBSDをコントロールしようとすると、pyrhonが見つからないといわれるので以下のように

192.168.1.xxx ansible_python_interpreter=/usr/bin/python

テスト

ping モジュールでテスト
前述のhostsファイルを作成していないと

$ ansible myhost -m ping
ERROR: Unable to find an inventory file, specify one with -i ?

とエラーになる。
Ansibleはインベントリファイルに書かれたホストにしかアクセスしないので、適当に書いて以下のように指定することも可能

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

ホスト名(IP)の代わりに“all”を指定すると ansible/hostsすべてに実行される

$ ansible all -m ping
myhost | success >> {
    "changed": false, 
    "ping": "pong"
}
hostB | success >> {
......

-aを指定することで任意のコマンドを実行

$ ansible 192.168.1.xxx -a 'hostname'
192.168.1.xxx | success | rc=0 >>
host

–sudo オプションを指定することで、root 権限の必要なコマンドも実行可能5)

$ ansible host --sudo -a id                                                     
host | success | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) ......

参考

2)
updateがリポポジトリになかった
3)
Linuxでは/etc/ansible/hosts
4)
/usr/local/share/examples/ansible/にサンプルがある
5)
当然、sudo権限を持っていること
serverapps/configmgmt/ansible.txt · 最終更新: 2017/04/14 14:21 (外部編集)