Ansible

https://galaxy.ansible.com/

基礎

http://tdoc.info/blog/2013/04/20/ansible.html

始め方

http://yteraoka.github.io/ansible-tutorial/

導通確認

  1. echo -e "[server-grouping-name]ntarget_server_ip:port" > hosts

  2. ansible 127.0.0.1:2222 -m ping -i hosts -u root -k -s

  3. ansible -i hosts server-grouping-name -a 'uname -r'

構文確認

  1. ansible-playbook -i hosts simple-playbook.yml --syntax-check

  2. ansible-playbook -i hosts simple-playbook.yml --list-tasks

  3. ansible-playbook -i hosts simple-playbook.yml --check

http://yteraoka.github.io/ansible-tutorial/

実行

一回ログインしないと

fatal: : FAILED! => {"failed": true, "msg": "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 vagrant@xxx -p xxx

ansible-playbook -i hosts site.yml -k

ansible-playbook -i hosts site.yml -k --ask-become-pass ssh先でsudoする場合

-kを入れないとパスワード入力ができずエラーになる

途中から実行

ansible-playbook site.yml -l redmine --start-at="redmine : install plugins in github"

Playbookの記述

まずはメインのplaybookを作る

site.yml

そしてsite.ymlから読み込むグループごとのプレイブックを同じくトップレベルのディレクトリに作成します。

webservers.yml

dbservers.yml

http://knowledge.sakura.ad.jp/tech/3084/

ディレクトリ構成

mkdir -p files filter_plugins group_vars handlers host_vars library production roles staging tasks templates vars

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

モジュール

http://www.infiniteloop.co.jp/blog/2013/08/ansible/

apt,yum

file

copy

複数ファイルをコピーする

https://stackoverflow.com/questions/36696952/copy-multiple-files-with-ansible

template

スクリプト

get_url

ユースケース別タスクの書き方

再起動させたい

Tips

FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}

sshpassをインストールする必要がある

sudo apt-get install sshpass

コマンドオプションで -c paramiko でもうまく行くみたい

http://d.hatena.ne.jp/yk5656/20141016/1415403630

scriptとcommandとshellの違い

shell:シェルコマンドを実行。これを使っておけば無難

script:シェルスクリプトを実行するときに使う

command:環境変数を読めなくてよい、リダイレクトやパイプをしないときに使う

FAQ

初回にでるフィンガープリントを無視する Are you sure you want to continue connecting

ansible.cfg

fatal: : FAILED! => {"changed": false, "failed": true, "module_stderr": "sudo: パスワードが必要ですn", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}

become: yesがあるためローカルのコマンドもsudoで打とうとする

そのタスクだけbecome: noにすると大丈夫

Aborting, target uses selinux but python bindings (libselinux-python) aren't installed! というエラーがでる

libselinux-pythonがないことが原因

インストールすること

Last updated