インストール前に

コントロールされる側に以下のパッケージが必要。

python-simplejson

Install

yum install python27-devel
pip-2.7 install ansible
yum install ansible --enablerepo=epel

インベントリ(操作対象ホスト)

# 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

パラメータ

ansible_ssh_hostホスト名に対しての実IP
ansible_ssh_portSSHポート
ansible_ssh_userSSHユーザ
ansible_ssh_passSSHパスワード。べた書きは非推奨なので--ask-passオプションを付けて実施するか公開鍵認証推奨
ansible_sudosudoを使うかどうか。デフォルトはfalse
ansible_sudo_passsudo のパスワード。危険なので--ask-sudo-passオプションを推奨)
ansible_sudo_exe (new in version 1.8)sudo コマンドへのパス
ansible_connectionlocal, ssh or paramiko.
ansible_ssh_private_key_file秘密鍵ファイルの場所
ansible_shell_typeシェル
ansible_python_interpreterログイン側のpython
ansible\_\*\_interpreteransibleのインタープリタの場所

文法チェックと空実行(dry run)

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

Playbook

- hosts: all
  user: hoge
  tasks:
    - script: /home/hoge/test.sh

httpd,openjdkインストール(分割推奨)

- 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
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

Taskモジュール

Command Module

リモートで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

操作先にShellを送り込んで実行。操作先にpythonが必要ないのもメリット

Shell Module

Ansibleの実行サーバー先にあるshファイルをリモートに送り込んで実行できる。

Tips

SSH接続時のキーチェック無効化

実行時の環境変数で設定export ANSIBLE_HOST_KEY_CHECKING=False

条件

変数は{{}}で囲まないので注意

- user: root
  hosts: all
  tasks:
    - name: install mlocate
      yum: name=mlocate state=installed
      when: install == "y"

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS