LoginSignup
5
7

Ansibleのモジュールで使用する引数についてのメモ(随時更新)

Last updated at Posted at 2016-06-13

環境

  • ansible 2.0.2.0
  • Amazon Linux AMI release 2016.03

ansibleコマンド

各環境(prd,stg,dev)のインベントリに定義されているホスト・グループに対してモジュールを指定して実行する

$ ansible -i <inventoryfile> <hostpattern> -m <module>

モジュール

  • 対象ホストで実行するライブラリ群
  • ファイル操作・転送・PKG操作等
  • モジュールの引数はシーケンスではなく、文字列として扱われる
  • 論理値は使い分ける
    • モジュールの引数はyes,no
    • その他はTrue,False
  • 行の折り返しは「>」
    • 改行を空白に置換

yum

  • state
    • present(default)
      • インストール状態
    • absent
      • 非インストール状態
    • latest
      • 最新版インストール状態

Nginxインストール

example.yml
---
#インデントはスペース2つ
#!/usr/bin/env ansible-playbook

- name: test-play
   hosts: target-al
   become: True
   tasks:
     - name: epel-install
       yum: >
         name=epel-release
         state=present

     - name: nginx-install
       yum: >
         name=nginx
         state=present

service

  • state
    • started
      • 起動状態
    • stopped
      • 停止状態
    • restarted
      • 再起動実施
    • reloaded
      • リロード処理実施

起動状態にし、システム起動時に開始

example.yml
---
---
#インデントはスペース2つ
#!/usr/bin/env ansible-playbook

- name: test-play
  hosts: target-al
  become: True
  tasks:
    - name: epel-install
      yum: >
        name=epel-release
        state=present

    - name: nginx-install
      yum: >
        name=nginx
        state=present

    - name: nginx-status
      service: >
        name=nginx
        state=started
        enabled=yes

copy

  • src
    • 差し替え元
  • dest
    • 差し替え先
  • backup
    • origin作成

indexファイル差し替え(オリジンはバックアップとして保存)

example.yml
---
#インデントはスペース2つ
#!/usr/bin/env ansible-playbook

- name: test-play
  hosts: target-al
  become: True
  tasks:
    - name: epel-install
      yum: >
        name=epel-release
        state=present

    - name: nginx-install
      yum: >
        name=nginx
        state=present

    - name: nginx-status
      service: >
        name=nginx
        state=started
        enabled=yes

    - name: change index.html
      copy: >
        src=/etc/ansible/index.html
        dest=/usr/share/nginx/html/index.html
        backup=yes

タイムスタンプ付きでバックアップされる

-rw-r--r-- 1 root root 3696 Feb 19 22:52 404.html
-rw-r--r-- 1 root root 3738 Feb 19 22:52 50x.html
-rw-r--r-- 1 root root  192 Jun 13 05:01 index.html
-rw-r--r-- 1 root root 3770 Feb 19 22:52 index.html.2016-06-13@05:01:48~
-rw-r--r-- 1 root root  368 Feb 19 22:52 nginx-logo.png
lrwxrwxrwx 1 root root   32 Jun 13 04:32 poweredby.png -> /usr/share/pixmaps/poweredby.png

setup

自動的に各ホストの情報を収集し、変数に代入される。その変数をFactsと呼び、これらは明示的に呼び出すことができる。

ファクトは "ansible_facts"というマッピングに定義されていることが分かる。

$ ansible -i hosts <target> -m setup
(snip)
10.0.0.90 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.0.0.90"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::41e:73ff:fecb:a23f"
        ], 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "05/12/2016", 
        "ansible_bios_version": "4.2.amazon", 
        "ansible_cmdline": {
            "console": "ttyS0", 
            "root": "LABEL=/"
        }, 
        "ansible_date_time": {
            "date": "2016-07-05", 
            "day": "05", 
            "epoch": "1467702189", 
            "hour": "16", 
            "iso8601": "2016-07-05T07:03:09Z", 
            "iso8601_basic": "20160705T160309733090", 
            "iso8601_basic_short": "20160705T160309", 
            "iso8601_micro": "2016-07-05T07:03:09.733143Z", 
            "minute": "03", 
            "month": "07", 
            "second": "09", 
            "time": "16:03:09", 
            "tz": "JST", 
            "tz_offset": "+0900", 
            "weekday": "Tuesday", 
            "weekday_number": "2", 
            "weeknumber": "27", 
            "year": "2016"
        }, 
        "ansible_default_ipv4": {
            "address": "10.0.0.90", 
            "alias": "eth0", 
            "broadcast": "10.0.0.255", 
            "gateway": "10.0.0.1", 
            "interface": "eth0", 
            "macaddress": "06:1e:73:cb:a2:3f", 
            "mtu": 9001, 
            "netmask": "255.255.255.0", 
            "network": "10.0.0.0", 
            "type": "ether"
        },
(snip)

ansible_default_ipv4を取り出す

 $ ansible -i hosts <target> -m setup -a 'filter=ansible_default_ipv4'
10.0.0.90 | SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "10.0.0.90", 
            "alias": "eth0", 
            "broadcast": "10.0.0.255", 
            "gateway": "10.0.0.1", 
            "interface": "eth0", 
            "macaddress": "06:1e:73:cb:a2:3f", 
            "mtu": 9001, 
            "netmask": "255.255.255.0", 
            "network": "10.0.0.0", 
            "type": "ether"
        }
    }, 
    "changed": false
}
5
7
1

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