LoginSignup
8
8

More than 5 years have passed since last update.

Ansible タスク(ロール)チートシート

Last updated at Posted at 2015-08-19

はじめに

オレオレベストプラクティスは思ったほど進化しなかったので後回し。
先にタスクを書いていて積み上がった知見を整理しておいたほうが後々楽かなと思ってやってみる。
タスク(ロール)毎に知見をメモっているので、タスク側のオレオレベストプラクティスだとも言える。

前提(という名の言い訳)

Ansible のバージョンは 1.9.2 を使用中。
CentOSで作業しているので、その他のことはよく知らんw
gistにしたほうが早いんじゃないか疑惑w
随時追加予定。

本題

yum module, ini_file module

roles/yum.repo/tasks/main.yml を抜粋


- name: epel
  yum: name=http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm state=present
  tags: [yum.repo]

- name: epel | disabled
  ini_file: dest=/etc/yum.repos.d/epel.repo
            section=epel
            option=enabled
            value=0
  tags: [yum.repo]

- name: epel-testing | disabled
  ini_file: dest=/etc/yum.repos.d/epel-testing.repo
            section=epel-testing
            option=enabled
            value=0
  tags: [yum.repo]

メモ

  • yum リポジトリを登録するロールは、抜粋のようにタスク内に値を全部書いてしまって外部変更不可にする(変更はgit等で別途管理)
  • 外部変更できなければこのタスクを見るだけで何が起きるのか把握できる(varsやdefaultも見なくていい)
  • rpmをどこから持ってきたか、バージョンも含めて明示的
  • たいていの場合、各機最初に一回やったらいじらないから問題ない
  • disabled にするのは単に趣味というか、通常の yum 使用時に無駄にリポジトリを参照しないようにするため

service module, with_items, ignore_errors, when

roles/service.stop/tasks/main.yml を抜粋


- name: service stopped. if set vars stop_services.
  service: name={{ item }} state=stopped enabled=no
  ignore_errors: yes
  with_items: stop_services
  when: stop_services is defined
  tags: [service.stop]

メモ

  • with_items に停止固定のサービス名(nfsとかiptablesとか)を入れている別タスク有
  • stop_services は vars のどこかで指定してもいいし、しなくてもいい(is defined)
  • 対象機によって指定のサービスが登録されていない場合もあるので、 ignore_errors は yes

lineinfile module, insertafter, notify

roles/postfix/tasks/main.yml を抜粋


- name: set up /etc/postfix/main.cf
  lineinfile:
    dest:   /etc/postfix/main.cf
    regexp: "{{ item.r }}"
    line:   "{{ item.l }}"
    state:  "{{ item.s }}"
    insertafter: "{{ item.i }}"
  with_items:
    - {r: '^relayhost = (.*)$' ,  l: 'relayhost = {{ relayhost }}', s: present, i: '^#relayhost ' }
    - {r: '^myhostname = (.*)$' , l: 'myhostname = {{ ansible_fqdn }}', s: present, i: '^#myhostname ' }
  notify: update_postfix
  when: relayhost is defined and relayhost != 'none'
  tags: [postfix]

メモ

  • with_items をハッシュ(Pythonでは辞書てかディクショナリ?)で与えて、複数タスクをひとまとめにするの便利
  • insertafter を指定すると、デフォルトでは行末追加なのを、指定位置に変更することができる
  • relayhost は group_vas の all に突っ込んでおくとサービス共通にできて楽
  • notify は乱用しないほうが吉。mail設定は最初に一回やったらもう弄らないと思うので追加した

shell module, creates, phpize && configure && make && make install

roles/php-swfed/tasks/main.yml を抜粋


- name: php-swfed | build
  shell: >
    {{ item }}
    chdir=/usr/local/src/swfed-0.62/src
    creates=/usr/lib64/php/modules/swfed.so
  with_items:
    - phpize
    - ./configure
    - make
    - make install
  tags: [php-swfed]

メモ

  • make まわり一式自動化とか夢のよう。。w
  • shell module に chdir を付けるとそのディレクトリで処理実行
  • 同じく creates を付けるとそのファイルが存在する場合に処理をスキップ(二回目以降は実行しないということ)
  • 逆に言えば creates のファイルを削除すればやり直しが可能
  • test があるものも make test を追加するだけ(自動化時にtestが必要かどうかは別問題)

template module, with_first_found

roles/hosts/tasks/main.yml を抜粋


- name: copy to /etc/hosts
  template: src={{ item }} dest=/etc/hosts group=root owner=root mode=0644
  with_first_found:
    - files:
        - hosts.{{ inventory_hostname }}.j2
        - hosts.{{ ansible_hostname }}.j2
        - hosts.{{ group_names[3] | default('x') }}.j2
        - hosts.{{ group_names[2] | default('x') }}.j2
        - hosts.{{ group_names[1] | default('x') }}.j2
        - hosts.{{ group_names[0] | default('x') }}.j2
        - hosts.j2
      paths:
        - ../templates/
  tags: ['hosts']

メモ

  • template module は j2 テンプレを処理
  • ../templates フォルダに置いたファイルから選択させる(最初にマッチしたファイル名のものが使われる)
  • group_names の階層順は調べていない
  • 使い方はいろいろ

template module 2回目

roles/zzfile/tasks/main.yml を抜粋


- name: upload files from host_vars
  template:
    src:   "{{ item.src }}"
    dest:  "{{ item.dest }}"
    owner: "{{ item.owner | default('root')}}"
    group: "{{ item.group | default('root') }}"
    mode:  "{{ item.mode  | default('0644')}}"
  with_items: host_zzfile_conf
  when: host_zzfile_conf is defined
  tags: [zzfile]

inventory/host_vars/x.yml


host_zzfile_conf:
  - src:    etc/td-agent/td-agent.conf.prod.slave
    dest:  /etc/td-agent/td-agent.conf

メモ

  • テンプレからファイルを生成して配置させる
  • roles/zzfile/template の配下に src のファイルを配置
  • 似てるけどちょっと違う、みたいなファイルを送りつけるのに便利
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8