脳汁portal

アメリカ在住(だった)新米エンジニアがその日学んだIT知識を書き綴るブログ

Ansible playbook tips

モジュール関係

get_url

wgetのようにURLからファイルをダウンロードする

- hosts: all
  tasks:
    - name: download
      get_url: 
        url:  # downloadしたいURL
        dest: # 配置先(/usr/local/src/somepackageとか) 
        validate_certs: no # 認証チェックでひっかかる&信頼できる場合
        timeout: 30       # v1.8から
command
- hosts: all
  sudo: yes
  tasks:
    - name: change permission
      command: chown -R hoge:fuga /usr/local/src
lineinfile

configファイルなどファイルを編集(置換)します

- hosts: all
  tasks:
    - name: make backup of config file
      command: cp /etc/config/hoge.conf /etc/config/hoge.conf.org
      # ちなみにcp /etc/config/hoge.conf{,.ofg}のような書き方は出来ませんでした

    - name: edit config file
      lineinfile: >-
        dest='/etc/config/hoge.conf'      # 編集したいファイルの指定
        state=present
        backrefs=yes                      # 正規表現にバックスラッシュを利用できるようにする
        regexp='{{ ^enabled= }}'          # 置換したい行を正規表現でしてい(^は行頭) 
        line='{{ enabled=true }}'         # 置き換えたい文字列を指定

複数置換したいポイントがある場合はwith_itemsを使用します

- hosts: all
  tasks:
    - name: make backup of config file
      command: cp /etc/config/hoge.conf /etc/config/hoge.conf.org

    - name: edit config file
      lineinfile: >-
        dest='/etc/config/hoge.conf' 
        state=present
        backrefs=yes                 
        regexp='{{ item.regexp }}'    
        line='{{ item.line }}'
      with_items:
        - regexp: '^enabled='
          line: 'enabled=true'
        - regexp: 'path=/temp'
          line: 'path=/usr/local/src'

変数関係

利用方法

ansibleの変数は

{{ 変数名 }}

で利用する

作業中の自分のサーバのIPを取得する
{{ ansible_eth1["ipv4"]["address"] }}
自分で変数を設定して利用する

group_vars/all.ymlファイルを作成し、変数宣言をする
group_vars/all.yml

version: 1.0.0
user: vagrant
storage0: 192.168.33.21
storage1: 192.168.33.22

他の変数と同じく{{}}で囲んで使う

その他

グループ内のサーバに番号をつけたい

以下のようにipアドレスのあとに指定することで変数として格納できます
hostsファイル

[storage]
192.168.33.21 order=0
192.168.33.22 order=1
192.168.33.23 order=2

こんな風に微妙に設定パラメータを変えたりするときに使える

- hosts: storage
  tasks:
    - name: edit config file
      lineinfile: >-
        dest='/etc/storage.conf'
        state=present
        backrefs=yes
        regexp='target: storage'
        line='target: storage_{{ order }}'
           # 192.168.33.21ではstorage_0 になる
           # 192.168.33.21ではstorage_1 になる
           # 192.168.33.21ではstorage_2 になる
sleep/wait処理
- hosts: all
  tasks:
    - name: wait 
      pause:
        seconds: 15
notify/handler

notifyを指定することで次のタスクを指定できます

- hosts: all
  tasks:
    - name: first processing
      command: # 最初の処理
      notify: next processing

  handlers:
    - name: next processing
      command: # 次の処理
debug

ansibleから各サーバへ送った命令の実行結果を取得して標準出力に出します

- hosts: all
  tasks:
    - name: check status
      command: ls -l
      register: ls_result

    - debug: var=ls_result.stdout_lines # ここで出力します
proxy設定

Proxy環境下でAnsibleを利用するときは各サーバでProxyの設定がされていないと動かない場合がある
対象のサーバ側で直接設定してもいいのだが、playbook側でも利用するProxyを指定することができる

- hosts: all
  vars:
    hogehoge_proxy:  # 名前はなんでもよい
      http_proxy: http://hogehoge-proxy.co.jp:8080
      https_proxy: https://hogehoge-proxy.co.jp:8080

  tasks:
    - name: download package via proxy
      command: wget ************
      environment: hogehoge_proxy  # taskごとに作成したProxyの設定を指定する