- 追加された行はこの色です。
- 削除された行はこの色です。
-プロビジョニングツール
#contents
*インストール前に [#m389e3bc]
コントロールされる側に以下のパッケージが必要。
python-simplejson
*Install [#a1ef0832]
-Pythonのパッケージ管理であるpipを使うと楽。python-develとgccが必要。
yum install python27-devel
pip-2.7 install ansible
-EPEL から yum でもインストール可能。
yum install ansible --enablerepo=epel
*設定ファイル [#e36529ef]
ansible.cfgファイルの探す順番は以下の通り。
+カレントディレクトリ
+環境変数の ANSIBLE_CONFIG or ~/ansible.cfg
+/etc/ansible/ansible.cfg
カレントにおいておけばそこが一番に読み込まれる。以下のように記載しておけば-iでインベントリを指定しなくてよい。
[defaults]
hostfile = hosts
*インベントリ(操作対象ホスト) [#b06e7a3f]
# local
localhost ansible_connection=local
# SSHポート 10022でユーザーはremote_user
remote.example.com ansible_ssh_port=10022 ansible_ssh_user=remote_user ansible_ssh_private_key_file=~/.ssh/KEY_FILE ansible_ssh_host=192.168.1.50
クラウド向けにはDynamic Inventoryという概念がある。詳細は公式HPで収集せよ。
**パラメータ [#ff63884d]
|ansible_ssh_host|ホスト名に対しての実IP|
|ansible_ssh_port|SSHポート|
|ansible_ssh_user|SSHユーザ|
|ansible_ssh_pass|SSHパスワード。べた書きは非推奨なので--ask-passオプションを付けて実施するか公開鍵認証推奨|
|ansible_sudo|sudoを使うかどうか。デフォルトはfalse|
|ansible_sudo_pass|sudo のパスワード。危険なので--ask-sudo-passオプションを推奨)|
|ansible_sudo_exe (new in version 1.8)|sudo コマンドへのパス|
|ansible_connection|local, ssh or paramiko. |
|ansible_ssh_private_key_file|秘密鍵ファイルの場所|
|ansible_shell_type|シェル|
|ansible_python_interpreter|ログイン側のpython|
|ansible\_\*\_interpreter|ansibleのインタープリタの場所|
*ワンラインで簡易実行 [#t77c2971]
-インベントリを用意を用意して以下のように実行する。
|ansible web03 -a "ls" -i hosts|ls|
|ansible web03 -a "ls" -i hosts --ask-pass|ls|
|ansible web03 -m copy -a "src=/etc/hosts dest=/tmp/hosts" -i host|操作元ホストからリモートホストへファイルコピー|
*文法チェックと空実行(dry run) [#lcd9b61e]
-文法チェック
ansible-playbook -i hosts simple-playbook.yml --syntax-check
-タスク一覧の表示
ansible-playbook -i hosts simple-playbook.yml --list-tasks
-空実行(dry run)
ansible-playbook -i hosts simple-playbook.yml -C
*推奨ディレクトリ構成 [#b34b0116]
TOP
-vars
-roles
hosts
*Role [#k4b4d2c0]
-公式サイトによればRoleは非常に便利とのこと
*Playbook [#a57e2726]
- hosts: all
user: hoge
tasks:
- script: /home/hoge/test.sh
**インクルード(条件分岐あり) [#uc4e9c01]
---
- include: setup-web.yml
when: type == "WEB"
- include: setup-db.yml
when: type== "DB"
**yum まとめてインスコ [#ic6d2445]
- name: yum install
yum:
name={{ item }}
with_items:
- httpd
- php
**httpd,openjdkインストール(分割推奨) [#i7e16019]
- hosts: web03
user: ec2-user
tasks:
- name: Install httpd
yum: name=httpd state=latest
sudo: yes
- name: apacheが起動していることを確認
action: service name=httpd state=started
sudo: yes
- name: "設定の修正(1)"
lineinfile: >-
dest='/etc/httpd/conf/httpd.conf'
state=present
backrefs=yes
regexp='^#?\s*ServerTokens'
line='ServerTokens Prod'
sudo: yes
notify: apache restart
- name: Install openJDK
yum: name=java-1.8.0-openjdk-devel state=latest
sudo: yes
handlers:
- name: apache restart
service:
name=httpd
state=restarted
sudo: yes
-セキュリティ周りの設定tag名称をsecurityにしている。
ansible-playbook -i hosts tcp_wrapper.yml --tags "security"
- hosts: web03
user: ec2-user
tasks:
- name: deny all service
lineinfile: >-
dest='/etc/hosts.deny'
state=present
line='ALL: ALL'
sudo: yes
tags:
- security
- name: enable ssh for limited ip
lineinfile: >-
dest='/etc/hosts.allow'
state=present
line='sshd: 172.20.128.'
tags:
- security
sudo: yes
-こちらはユーザーの作成。パスワードはハッシュ化したものを指定する。
- hosts: web03
user: ec2-user
vars:
user_name: normaluser
user_pass: '$6$P0ky3YE5$JNFD0EuY52rXeVtNVypSMkB3chjHWu9z3o.8KMANeblkEe8p7ZOD0HSuPGT0pbzgHW2kP.xo4zE17tM6wy.1Y1'
tasks:
- name: Add a new user
user: name={{user_name}} password={{user_pass}} groups=wheel state=present
sudo: yes
- hosts: web03
user: ec2-user
tasks:
- name: touch
shell: touch /var/tmp/{{ item.path }}
with_items:
- { path: 'hoge' }
- { path: 'fuga' }
- name: result test
shell: echo "hoge"
register: result
failed_when: result.rc not in [0, 1]
- name: catしたものをcontents変数に入れる
shell: cat /etc/passwd
register: contents
- name: contents変数の標準出力にrootが含まれるときは実施
shell: ls
when: contents.stdout.find('root') != -1
- name: 日付変数を作成してそのファイルを作る
shell: date +'%Y%M%d%I%M%S'
register: datestring
- name: 日付のフォルダ作成
shell: touch /var/tmp/{{ datestring.stdout }}
- name: symlink作成
file: src=/var/tmp/{{ datestring.stdout }} dest=/var/tmp/latest state=link
-ディレクトリを列挙して、それを消す
- name: 古いディレクトリを特定する。
shell: ls -t1 | tail -n +5
args:
chdir: /var/tmp
register: old_dirs
- name: 古いディレクトリ削除
file: path={{ item }}
with_items: old_dirs.stdout_lines
-Jenkins.warがなければダウンロードさせる(ファイル存在チェックとダウンロード)
- name: jenkins exists check
stat: path=/var/cache/ansible/jenkins.war
register: jenkins
- name: Jenkinsをダウンロードする
get_url:
url: http://mirrors.jenkins-ci.org/war/latest/jenkins.war
dest: /var/cache/ansible/
when: jenkins.stat.exists == False
*Taskモジュール [#oe3c52a8]
-出来合いのモジュールがたくさんある
**一覧 [#cc8a4160]
|ec2|Amazon ec2|
|s3|Amazon s3|
|shell|シェルを実行だが、コマンドも実行できる|
**Command Module [#da755925]
リモートで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
**Script Module [#o42793d1]
操作先にShellを送り込んで実行。操作先にpythonが必要ないのもメリット
**Shell Module [#b6a7badd]
Ansibleの実行サーバー先にあるshファイルをリモートに送り込んで実行できる。
*Tips [#k0924b99]
**外部変数(extra_vars) [#ff48ddad]
http://d.hatena.ne.jp/akishin999/20130818/1376824640
**SSH接続時のキーチェック無効化 [#b73a72a0]
-新規のサーバーだとそこで処理が止まってしまうので、鍵のチェックを無効化する。
|実行時の環境変数で設定|export ANSIBLE_HOST_KEY_CHECKING=False|
**Playbookがまったく実行されない [#wd060084]
- name: OK
-name: NG
**謎のエラー [#vbc47341]
ローカルのパーミッションがNGだと内部モジュールでエラーがでてわかりづらい。コピー元はtemplateディレクトリに入れておくべき!
*条件 [#e7f8c9ad]
変数は{{}}で囲まないので注意
- user: root
hosts: all
tasks:
- name: install mlocate
yum: name=mlocate state=installed
when: install == "y"
**初接続時の注意 [#wed2c9f0]
公開鍵が保持されていないとエラー。以下の記事を参考に自動化できぬものか!
http://qiita.com/kawaz/items/20983ec286088a1ae5c7