LoginSignup
2
1

More than 3 years have passed since last update.

IT未経験がいまさらだけどAnsibleの実行結果を制御してみた

Last updated at Posted at 2020-05-15

はじめに

この記事はAnsibleを勉強し始めた人を対象としたものです。
私自身は、IT未経験で今の会社に入社し、2年目に突入しました。
Ansibleは多少触ったことがある程度ですので、自由自在に使いこなせるよう、日々勉強中です。

目的

Ansibleでは『ディレクティブ』という概念があります。
1つのタスクに対して記述する『ディレクティブ』の組み合わせによって、実行結果は異なります。
今回は、いくつかのパターンを試して、ディレクティブの組み合わせによる実行結果の違いを確認していきます。

ディレクティブ一覧

Playbook Keywords
※公式サイトでは、『ディレクティブ』ではなく『Playbook Keywords』となっております。

準備

実行結果の確認をするだけなので、httpdプロセスを起動状態にするという簡単なPlaybookを用意します。

---
- hosts: localhost
 become: True
 gather_facts: False
 tasks:
 - name: Specify the state of Apache
 service:
 name: httpd
 state: started
 enabled: yes
...

実行サーバはEC2(Amazon Linux)を使用しました。Ansibleのバージョンは2.8.1です。

実践

まず、ディレクティブを組み合わせる前に、単体のディレクティブの効果を確認していきましょう。
まずは、changed_whenディレクティブです。

---
- hosts: localhost
 become: True
 gather_facts: False
 tasks:
 - name: Specify the state of Apache
 service:
 name: httpd
 state: started
 enabled: yes
 changed_when: True
...

上記のようにディレクティブの真偽値をTrueにすることで、タスクの実行結果をchangedに変えることができます。

PLAY [localhost] *******************************************************************************************************************************************************
TASK [Specify the state of Apache] *************************************************************************************************************************************
changed: [localhost]
PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

一方、failed_whenディレクティブも同様に、真偽値をTrueにすることでタスクの実行結果をfailedに変えることができます。

---
- hosts: localhost
 become: True
 gather_facts: False
 tasks:
 - name: Specify the state of Apache
 service:
 name: httpd
 state: started
 enabled: yes
 failed_when: True
...
PLAY [localhost] *******************************************************************************************************************************************************
TASK [Specify the state of Apache] *************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "enabled": true, "failed_when_result": true, "name": "httpd", "state": "started"}
PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

それでは、1つのタスクに対して“changed_when: True”, “failed_when: True”の2つを記述すると、どのような結果になるでしょう。

---
- hosts: localhost
 become: True
 gather_facts: False
 tasks:
 - name: Specify the state of Apache
 service:
 name: httpd
 state: started
 enabled: yes
 changed_when: True
 failed_when: True
...
PLAY [localhost] *******************************************************************************************************************************************************
TASK [Specify the state of Apache] *************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "enabled": true, "failed_when_result": true, "name": "httpd", "state": "started"}
PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

結果がfailedとなりました。
この結果はディレクティブの記述する順番を入れ替えても変わりません。
“changed_when: True”と“failed_when: True”では、“failed_when: True”が優先されることが分かります。
更に、ignore_errorsディレクティブを記述するとどうなるでしょう。

---
- hosts: localhost
 become: True
 gather_facts: False
 tasks:
 - name: Specify the state of Apache
 service:
 name: httpd
 state: started
 enabled: yes
 changed_when: True
 failed_when: True
 ignore_errors: True
...
PLAY [localhost] *******************************************************************************************************************************************************
TASK [Specify the state of Apache] *************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "enabled": true, "failed_when_result": true, "name": "httpd", "state": "started"}
...ignoring
PLAY RECAP *************************************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1

今度は“changed_when: True”と “failed_when: True”の2つだけを記述したときと異なり、“changed_when: True”が有効になりました。
先程有効であった”failed_when: True”は、“ignore_errors: True”によって無効になります。
この場合も、ディレクティブの記述する順序が実行結果に影響することはありません。

あとがき

ディレクティブの組み合わせによって実行結果を制御できることが分かりました。
仕組みを理解することで、役立つことがあるかもしれません。
Ansibleには他にもディレクティブが存在しますので、様々な組み合わせを試してみるとよいと思います。
今後もAnsibleに関する記事は書いていこうと思います。

2
1
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
2
1