コントロールされる側に以下のパッケージが必要。
python-simplejson
Pythonのパッケージ管理であるpipを使うと楽。EPEL から yum でもインストール可能。
yum install ansible --enablerepo=epel
ansible-playbook -i hosts simple-playbook.yml --syntax-check
- hosts: all
user: hoge
tasks:
- script: /home/hoge/test.sh
リモートでOSコマンドを実施する。
# Example from Ansible Playbooks. - command: /sbin/shutdown -t now # createsに指定されたファイルがない場合はcommandの処理を実行 - command: /usr/bin/make_database.sh arg1 arg2 creates=/path/to/database
# somedir以下に移動し、かつ
# createsに指定されたファイルがない場合はcommandの処理を実行
- command: /usr/bin/make_database.sh arg1 arg2
args:
chdir: somedir/
creates: /path/to/database
#繰り返し処理のサンプル。変数定義済みであればwith_items:{{users}}でもOK
- name: add several users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2
#コンパイルのタスクだが、実際にはシェルが実行できるのでなんでもOK
- name: Apacheコンパイル
shell:
make
chdir=/usr/local/src/httpd-2.4.12
creates=/usr/local/apache/2.4.12/bin/httpd
操作先にShellを送り込んで実行。操作先にpythonが必要ないのもメリット
Ansibleの実行サーバー先にあるshファイルをリモートに送り込んで実行できる。
変数は{{}}で囲まないので注意
- user: root
hosts: all
tasks:
- name: install mlocate
yum: name=mlocate state=installed
when: install == "y"