LoginSignup
57
64

More than 3 years have passed since last update.

備忘録 Ansibleのplaybook実行の超基本

Last updated at Posted at 2018-01-05

いまひとつ自分でよく分かっていなかったので整理してみた。

1. ディレクトリ構成

今回実行する上でのディレクトリ構成です。

/root/
   └ ansible
       ├─ hosts             ・・・inventoryファイル
       └─ tasks             ・・・playbook格納場所
           └─ test1.yml     ・・・playbook

2. inventoryファイルの作成

今回私が持っている環境は、以下の3台です。
  brighton001 ・・・ Ansibleサーバー
  brighton002 ・・・ テスト1号機
  brighton003 ・・・ テスト2号機

これをinventoryファイルに記述します。今回は、こんな感じ。
inventoryには、ansibleを実行したいホスト名(名前解決しているのであれば)かIPアドレスを記述します。
[] を利用することで、グループ分けができます。

[centos7]
brighton002
brighton003

[web]
brighton002

[db]
brighton003

3. playbookを作成

ユーザーを作成するplaybookを作成します。

test1.yml
---
- hosts: db                      ・・・ 今回inventoryのdbグループにだけ実施
  sudo: yes                      ・・・ 管理者権限で実行するには必要となる
  tasks:                         ・・・ playbookで実行する内容を記述
   - name: add a new user        ・・・ タスク名を記述(省略可)
     user: name=koba             ・・・ ユーザー追加のモジュール

4. playbookを実行

1. 文法チェック

作成したplaybookの文法が正しいかどうかを確認します。
# ansible-playbook -i hosts ./task/test1.yml --syntax-check
※ -i のあとにinventoryファイルを指定するが、ここでは、/root/ansibleディレクトリで実行しているためファイル名のみ指定。別ディレクトであれば、正しいパスを指定

実行後の確認。以下の内容は構文に問題が無い事を示す。

 # ansible-playbook -i hosts ./task/test1.yml --syntax-check
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo'
(default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

playbook: ./task/test1.yml

構文に誤りが有る場合の例
name: add a new userの中でエラー。

# ansible-playbook -i hosts ./task/test1.yml --syntax-check
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo'
(default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in '/root/ansible/task/test1.yml': line 5, column 6, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
   - name: add a new user
     ^ here


The error appears to have been in '/root/ansible/task/test1.yml': line 5, column 6, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
   - name: add a new user
     ^ here

exception type: <class 'ansible.errors.AnsibleParserError'>
exception: no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in '/root/ansible/task/test1.yml': line 5, column 6, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
   - name: add a new user
     ^ here

2. 実行されるタスクの確認

playbookで実行されるタスクを確認します。

# ansible-playbook -i hosts ./task/test1.yml --list-task

実行結果は以下の通りです。
hostsグループ dbに対して、 add a new userというタスクが実行されることがわかります。
 

# ansible-playbook -i hosts ./task/test1.yml --list-task
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo'
(default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

playbook: ./task/test1.yml

  play #1 (db): db      TAGS: []
    tasks:
      add a new user    TAGS: []

3. DryRunの確認

実際にplaybookが実行されたらどうなるかという結果のみを確認します。
ただし、この時点ではplaybook自身は実行されません。
※ユーザーは作成されない

# ansible-playbook -i hosts ./task/test1.yml --check

実行結果は以下の通りです。

# ansible-playbook -i hosts ./task/test1.yml --check
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo'
(default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

PLAY [db] **********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [brighton003]

TASK [add a new user] **********************************************************************************************
changed: [brighton003]

PLAY RECAP *********************************************************************************************************
brighton003                : ok=2    changed=1    unreachable=0    failed=0

4. 実行します

実際にplaybookを実行します。
# ansible-playbook -i hosts ./task/test1.yml

# ansible-playbook -i hosts ./task/test1.yml
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo'
(default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

PLAY [db] **********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [brighton003]

TASK [add a new user] **********************************************************************************************
changed: [brighton003]

PLAY RECAP *********************************************************************************************************
brighton003                : ok=2    changed=1    unreachable=0    failed=0

テスト機でログを確認していると、実行されたことがわかります。

# journalctl -f
Jan 05 13:18:52 brighton003 sudo[2105]:     root : TTY=pts/1 ; PWD=/root ; USER=root ; COMMAND=/bin/sh -c echo BECOM E-SUCCESS-qzcumjqjthhmauydpqfypweqzsumeeiz; /usr/bin/python /root/.ansible/tmp/ansible-tmp-1515125931.99-143434399830059/setup.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1515125931.99-143434399830059/" > /dev/null 2>&1
Jan 05 13:18:52 brighton003 python[2108]: ansible-setup Invoked with filter=* gather_subset=['all'] fact_path=/etc/a        nsible/facts.d gather_timeout=10
Jan 05 13:18:53 brighton003 sudo[2182]:     root : TTY=pts/1 ; PWD=/root ; USER=root ; COMMAND=/bin/sh -c echo BECOM        E-SUCCESS-ewcttorkprjeohkfeodjjqmiulokhkxv; /usr/bin/python /root/.ansible/tmp/ansible-tmp-1515125932.86-170288683869108/user.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1515125932.86-170288683869108/" > /dev/null 2>&1
Jan 05 13:18:53 brighton003 python[2185]: ansible-user Invoked with comment=None ssh_key_bits=0 update_password=always non_unique=False force=False ssh_key_type=rsa ssh_key_passphrase=NOT_LOGGING_PARAMETER uid=None home=None append=False skeleton=None ssh_key_comment=ansible-generated on brighton003 group=None system=False state=present local=None shell=None expires=None ssh_key_file=None groups=None move_home=False password=NOT_LOGGING_PARAMETER seuser=None name=koba createhome=True remove=False login_class=None generate_ssh_key=None
Jan 05 13:18:53 brighton003 useradd[2192]: new group: name=koba, GID=1000
Jan 05 13:18:53 brighton003 useradd[2192]: new user: name=koba, UID=1000, GID=1000, home=/home/koba, shell=/bin/bash

/etc/passwdでも確認します。

# grep koba /etc/passwd
koba:x:1000:1000::/home/koba:/bin/bash

5. 冪等性の確認

再度実行した場合、ユーザーはすでに作成されているため、何もおこなわれないことを確認します。

# ansible-playbook -i hosts ./task/test1.yml
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo'
(default). This feature will be removed in version 2.6. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

PLAY [db] **********************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [brighton003]

TASK [add a new user] **********************************************************************************************
ok: [brighton003]

PLAY RECAP *********************************************************************************************************
brighton003                : ok=2    changed=0    unreachable=0    failed=0

changed=0となり、変更が無い事がわかります。

すこしづつ分かり始めました。
まずは基本を理解することが重要ですね。

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