LoginSignup
38
38

More than 5 years have passed since last update.

Ansibleでよく使ってるタスクとかちょっとしたTips

Last updated at Posted at 2015-01-31

Ansibleで少し調べないとわかんない事とか、普段よく使うのとかをなんとなく書いてみる。

Ansibleは入門程度の知識でも、かなり効果あると思うのでおすすめです。大規模の場合でもロールや冪等性、Gathering Factsの内容活用するとか少し気にするだけじゃないでしょうか。

タスク

Ansibleのタスクはたくさんあり、サーバ構築に関わる大半の作業が網羅されているが、ミドルウェアのインストールまではインフラ担当が行う事が多いので、自分が実際良く使うのは数種類。

テンプレートからファイルを生成する

設定ファイルの生成とか。srcに指定したファイルに変数埋め込むと、タスク実行に展開してくれる。backupをyesにすると、バックアップを取ってくれる。

# Example from Ansible Playbooks
- template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode=0644

[http://docs.ansible.com/template_module.html:title]

ディレクトリやシンボリックンクを作成する

stateをdirectoryやlinkにすることで、作成するものを変えられる。

- file: src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link

[http://docs.ansible.com/file_module.html:title]

シェルやコマンドを実行する

シェルやコマンド実行は、commandやscriptでもできるが挙動やできることが微妙に違う。私はshellを使うことが多い。

# You can also use the 'args' form to provide the options. This command
# will change the working directory to somedir/ and will only run when
# somedir/somelog.txt doesn't exist.
- shell: somescript.sh >> somelog.txt
  args:
    chdir: somedir/

[http://docs.ansible.com/shell_module.html:title]

圧縮ファイルを解凍する

これはタイトルのまま。.tar.gzの解凍とか。

# Example from Ansible Playbooks
- unarchive: src=foo.tgz dest=/var/lib/foo

[http://docs.ansible.com/unarchive_module.html:title]

実行オプション

Ansibleではplaybook実行時、オプションを指定することで実行内容が変わる。

タスクを部分的に実行する

タスクにタグを付け、オプションでタグを指定する。タスクのまとまり毎に同じタグを付ける感じ。

タスク

tasks:

    - yum: name={{ item }} state=installed
      with_items:
         - httpd
         - memcached
      tags:
         - packages

    - template: src=templates/src.j2 dest=/etc/foo.conf
      tags:
         - configuration

タグを指定して実行

ansible-playbook example.yml --tags "configuration,packages"

[http://docs.ansible.com/playbooks_tags.html:title]

パスワードを聞く

sshのパスワード聞かない設定できない時とか。playbook実行直後にパスワードを入力する。

ansible-playbook example.yml --ask-pass

[http://docs.ansible.com/intro_getting_started.html#remote-connection-information:title]

チェック

playbookを実行せずに、実行したとしたらどんな変更が行われるかチェックする。いわゆるドライラン。playbookの文法チェックとしても使える。

ansible-playbook example.yml --check

[http://docs.ansible.com/playbooks_checkmode.html:title]

変数

Ansibleでは変数を使うことで、playbookのコーディング量を減らせる。Tips的に思いつくのを書く。

itemをハッシュ風に使う

値をセットでループさせたいときに便利

- name: add several users
  user: name={{ item.name }} state=present groups={{ item.groups }}
  with_items:
    - { name: 'testuser1', groups: 'wheel' }
    - { name: 'testuser2', groups: 'root' }

[http://docs.ansible.com/playbooks_loops.html#standard-loops:title]

対象サーバのホスト名を変数にセットする

inventory_hostnameを使うことで、playbookのhostを変数として使える。

{{ inventory_hostname }}

[http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts:title]

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