LoginSignup
4
6

More than 5 years have passed since last update.

ansible2.4での動的タスク導入方法

Last updated at Posted at 2018-01-16

ansible2.4以降スクリプト(playbook, role, task)を再利用する方法がinclude一択から
import*とinclude*に分かれました。

import*とinclude*の違い

ドキュメントを訳すると
- import*文(import_tasks, import_role, import_playbook)はスクリプトが実行される前にパースされます。
- include*文はスクリプトの実行時でパースされ、したがって動的に変数を渡せます。

動的タスクincludeする時の問題

今まではinclude文しか使えません。ansible2.4対応で動的にタスクをincludeするとエラーが発生しました。例えばimportantという名のroleがあり、playbook.ymlの中このroleをincludeしています。このroleの中動的にタスクをincludeしてあります。

before:

roles/important/tasks/main.yml
...
- include: other_tasks.yml
    var1: password
...

after_ver1:

include*文をそのまま代入します

roles/important/tasks/main.yml
...
- include_tasks: other_tasks.yml
    var1: password
...

変更したあと

ansible-playbook playbook.yml

を実行すると

fatal: [myhost.com]: FAILED! => {"reason": "Unable to retrieve file contents\nCould not find or access '/working/dir/other_tasks.yml'"}

エラーが出ます。理由はincludeする時ansibleがplaybookのあるdirでother_tasks.ymlを探しています。でもこのymlはrole/importance/task/にあるので、見つからずエラーで終了しました。直すとすると

after_ver2

roles/important/tasks/main.yml
...
- include_role:
    name: importance
    tasks_from: other_tasks.yml 
  vars:
    var1: password
...

解説:まず動的にroleをincludeし、実行するタスクが入ってるymlをtasks_fromで指定します。無事role/importance/task/のdirでother_tasks.ymlを見つけました!

4
6
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
4
6