ansible-core2.19 の変更点

1K Views

June 17, 25

スライド概要

2025年7月に ansible-core 2.19.0 がリリース予定です。

本リリースでは、注意が必要な点が複数あります。Porting Guide (https://docs.ansible.com/ansible/devel/porting_guides/porting_guide_core_2.19.html )を中心に個人的に気になっているポイントの共有です。
今まで不通に動いていた Playbook がエラーになることもあります。


動画 https://www.youtube.com/watch?v=sZwi-nQ7GqE

APC勉強会「8a1」の資料です。
https://8a1-apc.connpass.com/event/358002/

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

ダウンロード

関連スライド

各ページのテキスト
1.

#apc8a1 ansible-core 2.19 の変更点 2025/06/17 「8a1」APC勉強会 #43 株式会社エーピーコミュニケーションズ 横地 晃 1

2.

#apc8a1 はじめに ◼ 2025年7月に ansible-core 2.19.0 がリリース予定です ◼ 本リリースでは、注意が必要な点が複数あります ◼ Porting Guide (*1)を中心に個人的に気になっているポイントを共有します ◼ 本資料は ansible-core 2.19.0b6 で動作確認しています。正式リリース時に挙動が変更されてい る可能性があります - name: Implicit boolean test string ansible.builtin.assert: that: 例えばこれが - kind エラーになる vars: kind: network *1: https://docs.ansible.com/ansible/devel/porting_guides/porting_guide_core_2.19.html 2

3.

#apc8a1 自己紹介 横地 晃 @akira6592 所属 (株)エーピーコミュニケーションズ 業務 ネットワーク自動化のご支援 てくなべ(ブログ) コミュニ Ansible ユーザー会、JANOG ティ https://tekunabe.hatenablog.jp/

4.

#apc8a1 ansible-core 2.19 の概要 4

5.

#apc8a1 ansible-core 2.19 の概要 ◼ Ansible のコア部分の次のリリース ◼ ◼ 通常は5月と11月リリースだが、現状 2025/07/21 の正式リリース予定(*1) テンプレートの処理に大幅な修正がある ◼ パフォーマンスやセキュリティ向上が見込める一方で、今までのPlaybookがエラーになることがあ る ◼ 暗黙的な変換が許容されなくなり、厳密な指定が必要なる傾向 ◼ 包括的な情報は Porting Guide (*2) や Changelog (*3) に掲載されている *1: https://docs.ansible.com/ansible/devel/roadmap/ROADMAP_2_19.html#release-phase *2: https://docs.ansible.com/ansible/devel/porting_guides/porting_guide_core_2.19.html *3: https://github.com/ansible/ansible/blob/stable-2.19/changelogs/CHANGELOG-v2.19.rst 5

6.

#apc8a1 影響度の高さの表れ ◼ 新しいドキュメントでは、どのページにも注意書きが表示される ◼ 「ansible-core 3.0.0」という表現をしていた方もいるほど(*1) *1: https://forum.ansible.com/t/core-2-19-templating-changes-preview-and-testing/40759/43 6

7.

#apc8a1 条件式の厳密化 Broken Conditionals への対応 7

8.

#apc8a1 条件式の暗黙的な bool 変換はエラー(例) ◼ when や assert モジュールの that オプションなどで、暗黙的な bool 変換に依存した 条件式はエラーになる ◼ 「1文字以上の文字列は True、空文字は False」「1要素以上のリストは True、空リストは False」「0 以外は True、0 は False」など - name: Implicit boolean test string ansible.builtin.assert: that: - kind vars: 文字列 network を評価し ようとしている エラー [ERROR]: Task failed: Conditional result was 'network' of type 'str', which evaluates to True. Conditionals must have a boolean result. kind: network * Porting Guide の「Example - implicit boolean conversion」に該当 8

9.

#apc8a1 条件式の暗黙的なbool変換はエラー(対策) 文字列や要素数の長さを指定した条件式に修正すればOK ◼ または、truthy / falsy テストプラグインを利用してもOK ◼ - name: Test boolean ansible.builtin.assert: that: - kind | length > 0 vars: 明示的に文字列の長さを 指定した条件式ならOK kind: network - name: Test boolean ansible.builtin.assert: that: - kind is truthy vars: truthy テストプラグインを 利用しているので OK kind: network 9

10.

#apc8a1 一部の条件式がクォーテーションで囲われているとエラー(例) ◼ 一部の条件式がクォーテーションで囲われていると、囲われている部分が常に True となり、条件の本来の役割を果たさない - name: Test unintentional truthy conditional ansible.builtin.assert: that: - kind is defined and 'kind | length > 0' vars: kind: network 囲われている * Porting Guide の「Example - unintentional truthy conditional」に該当 エラー [ERROR]: Task failed: Conditional result was 'kind | length > 0' of type 'str', which evaluates to True. Conditionals must have a boolean result. 10

11.

#apc8a1 一部の条件式がクォーテーションで囲われているとエラー(対策) ◼ 余計なクォーテーションを外せばOK - name: Test conditions ansible.builtin.assert: that: - kind is defined and kind | length > 0 vars: kind: network 囲わないなら OK - name: Test conditions ansible.builtin.assert: that: - kind is defined - kind | length > 0 vars: もちろんこれでも OK kind: network 11

12.

#apc8a1 いきなりエラーにされても困るんだけど・・・ ◼ ◼ ◼ ◼ ここまで紹介したエラーになる条件式は Porting Guide上「Broken Conditionals」と表現され ている Broken Conditionals を許容するかの設定「ALLOW_BROKEN_CONDITIONALS」(*1)がある デフォルトは無効(許容しないためエラー)。有効にすると非推奨を示す警告に留まる いったん有効で実行してみて、全容を把握してから修正するのもアリかも ▼ ansible.cfg で有効にする場合 [defaults] allow_broken_conditionals=True ▼ 環境変数 で有効にする場合 ANSIBLE_ALLOW_BROKEN_CONDITIONALS=True 警告の例(エラーではないので次のタスクに進む) [DEPRECATION WARNING]: Conditional result was 'network' of type 'str', which evaluates to True. Conditionals must have a boolean result. This feature will be removed from ansible-core version 2.23. *1: https://docs.ansible.com/ansible-core/devel/reference_appendices/config.html#allow-broken-conditionals 12

13.

#apc8a1 ネストされたテンプレートの禁止 Multi-pass templating 13

14.
[beta]
#apc8a1

ネストされたテンプレート式はエラー(例)
when や assert モジュールの that オプションなどには、
本来テンプレート書式 {{ }} の内側だけを指定する
◼ 部分的に {{ }} があるとエラーになる
◼

◼

ansible-core 2.18 では警告だった
- name: Test unnecessary template
ansible.builtin.assert:
that:
- 1 + {{ value }} == 2
vars:
不要な {{ }} がある
value: 1
のでエラー

* Porting Guide の「Example - unnecessary template in expression」に該当

エラー
[ERROR]: Task failed: Syntax error in
expression. Template delimiters are not
supported in expressions: expected token ':',
got '}'

14

15.

#apc8a1 ネストされたテンプレート式はエラー(微妙に違う例) ◼ 丸ごと "{{ }}" だとエラーではなく警告 ◼ Porting Guide にない例。おそらく以前から警告だっため - name: Test unnecessary template ansible.builtin.assert: that: - "{{ true }}" 警告 [DEPRECATION WARNING]: Conditionals should not be surrounded by templating delimiters such as {{ }} or {% %}. This feature will be removed from ansible-core version 2.23. 条件式が丸ごと "{{ }}" だと警告 15

16.

#apc8a1 ネストされたテンプレート式はエラー(対策) ◼ {{ }} を外せばOK - name: Test conditions ansible.builtin.assert: that: - 1 + value == 2 vars: 不要な {{ }} を外せばOK value: 1 16

17.

#apc8a1 テンプレートモードの変更 jinja2_native 固定に 17

18.
[beta]
#apc8a1

jinja2_native 固定になった
今まで「DEFAULT_JINJA2_NATIVE」(*1) という設定でテンプレートのモードを
「jinja2_native」にするかどうか選択できた(デフォルト無効)が、jinja2_native 固定に
なった(*1)。これにより、元の型が保持される
◼ 以下は、set_fact で数値をセットすると、数値のままの例
◼

- name: Set fact
ansible.builtin.set_fact:
myvar: "{{ 1 + 2 }}"
数値をセット

- name: Debug variable
ansible.builtin.debug:
msg: "{{ myvar }}"

TASK [Set fact] ************
ok: [localhost]
TASK [Debug variable] ******
ok: [localhost] => {
数値のまま
"msg": 3
}
今までのデフォルトでは以下のように文

* Porting Guide の「Native Jinja mode required」に該当
*1: https://docs.ansible.com/ansible/devel/reference_appendices/config.html#default-jinja2-native
*2: 設定項目 DEFAULT_JINJA2_NATIVE 自体はまだ残っているが意味がない

字列に変換されていた
ok: [localhost] => {
"msg": "3"

18

19.
[beta]
#apc8a1

jinja2_native 固定になった
◼

以下は、set_fact で文字列 "yes" をセットすると、文字列のままの例

- name: Set fact
ansible.builtin.set_fact:
myvar: "yes"

文字列をセット

- name: Debug variable
ansible.builtin.debug:
msg: "{{ myvar }}"

TASK [Set fact] ************
ok: [localhost]
TASK [Debug variable] ******
ok: [localhost] => {
文字列のまま
"msg": "yes"
}
今までのデフォルトでは以下のように
true に変換されていた
ok: [localhost] => {
"msg": true

* Porting Guide の「Native Jinja mode required」に該当

19

20.

#apc8a1 jinja2_native を有効にしていますか? ◼ 事前アンケートで、現状 jinja2_native にしているかお伺いしました # 確認例 (2.19.0b6 の結果例) $ ansible-config dump | grep NATIVE DEFAULT_JINJA2_NATIVE(default) = True *2025/06/17 18:10 時点 20

21.

#apc8a1 エラーハンドリングの向上 Error handling 21

22.

#apc8a1 文脈に応じた警告やエラー エラーが詳細に表示されるようになった ◼ -v なしの実行でも ◼ メッセージの出方はまだ安定していないので、メッセージに依存した自動化は避け るべき ◼ 例は次のページへ * Porting Guide の「Contextual warnings and errors」に該当 22

23.
[beta]
#apc8a1

文脈に応じた警告やエラー(変数未定義エラーの例)
$ ansible-playbook -i localhost, contextual_warnings_and_errors.yml

$ ansible-playbook -i localhost, contextual_warnings_and_errors.yml

-v なしの実行でも

PLAY [Test Play] ***************************************************************************

ansible-core 2.19.0b6 でのエラー

TASK [Debug Test] **************************************************************************
[ERROR]: Task failed: Finalization of task args for 'ansible.builtin.debug' failed: Error while resolving value for 'msg':
'undifined_variable' is undefined
Task failed.
Origin: /home/sakana/ansible/ac219/contextual_warnings_and_errors.yml:8:7
6
7
8

tasks:
- name: Debug Test
^ column 7

深掘り

<<< caused by >>>
Finalization of task args for 'ansible.builtin.debug' failed.
Origin: /home/sakana/ansible/ac219/contextual_warnings_and_errors.yml:9:7
7
8
9

PLAY [Test Play]
**********************************************************************************

ansible-core 2.18.6 でのエラー

TASK [Debug Test]
*******************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an
undefined variable.. 'undifined_variable' is undefined\n\nThe error appears to be
in '/home/yokochi/git/general/ansible/ac219/contextual_warnings_and_errors.yml':
line 8, column 7, but may\nbe elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n tasks:\n
- name: Debug
Test\n
^ here\n"}

Playbook の
hereエラーの原因として
は何を示してるんだろう?
中身が芋づる式に示される
が追いにくかった

PLAY RECAP
**********************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0
ignored=0

tasks:
- name: Debug Test
ansible.builtin.debug:
^ column 7

<<< caused by >>>

深掘り

Error while resolving value for 'msg': 'undifined_variable' is undefined
Origin: /home/sakana/ansible/ac219/contextual_warnings_and_errors.yml:10:14
8
9
10

- name: Debug Test
ansible.builtin.debug:
msg: "{{ undifined_variable }}"
^ column 14

・Playbook の抜粋が分かりやすい
・今までなかったエラー深掘りにより初動を早くできる
・最初にここを見ると良さそう

fatal: [localhost]: FAILED! => {"msg": "Task failed: Finalization of task args for 'ansible.builtin.debug' failed: Error while
resolving value for 'msg': 'undifined_variable' is undefined"}
PLAY RECAP *********************************************************************************
localhost
: ok=0
changed=0
unreachable=0
failed=1
skipped=0
rescued=0

ignored=0

23

24.

#apc8a1 テストプラグインが未定義のキーを参照するとエラーにできる ◼ true や false などのテストプラグインが未定義のキーを参照するとエラー - name: Test handling of undefined attribute ansible.builtin.debug: msg: Hello! when: - hoge.fuga is true 未定義のキーを参照しようと vars: しているのでエラー hoge: (以前は skipped) piyo: true - name: Test handling of undefined variable ansible.builtin.assert: that: - undifined_variable is true 未定義の変数を参照しようと しているのでエラー (以前は assert 結果 としてのfatal) * Porting Guide の「Example - missing attribute」に該当 エラー [ERROR]: Task failed: Error while evaluating conditional: object of type 'dict' has no attribute 'fuga' エラー [ERROR]: Task failed: Error while evaluating conditional: 'undifined_variable' is undefined 24

25.

#apc8a1 bool フィルターの厳密化 25

26.
[beta]
#apc8a1

bool フィルターが厳密になった
◼

今まで様々な値を変換してたが、以下に限定されるようになった(大文字小文字区
別なし)
◼

true: True, yes, on, "1", 1
◼ false: False, no, off, "0", 0
◼

その他は警告
- name: Test bool filter for others
ansible.builtin.debug:
msg: "{{ myvar | bool }}"
vars:
-1 に bool フィルターをか
けてるので警告
myvar: -1

警告
[DEPRECATION WARNING]: The `bool` filter
coerced invalid value -1 (int) to False. This
feature will be removed from ansible-core
version 2.23.
実行自体はされる
ok: [localhost] => {
"msg": false
}

* Porting Guide の Plugins の Noteworthy plugin changes 内の 「Coercing unrecognized input values in the bool filter(略)」に該当
* ゆるめの変換をする場合は truthy / falsy テストプラグインの結果を利用する

26

27.

#apc8a1 【おまけ】 生成AI でいい感じにしたいんだが? 27

28.

#apc8a1 結構修正してくれた プロンプト 修正結果 以下の URL はansible-core 2.19 の 注意点などを示した Porting Guide です。 https://docs.ansible.com/ansible/devel/porting_guides/porting _guide_core_2.19.html この Porting Guide をよく読み、ansible-core 2.19 でもエラーや警告が 表示されないような修正をに修正してください。 応答 Porting Guideに従い、ansible-core 2.19 でもエラーや警告が出ないよう 修正しました。 ・条件式は必ずブール値を返すようにし、テンプレート構文({{ ... }})は式内 で使わない形に変更しています。 ・これでエラーや警告なく動作します。 他にも確認や修正したい点があればお知らせください。 * GitHub Copilot Free、GPT-4.1、agant モード、私用PC 利用 警告が残るが 修正されなかった 工夫の余地ありか ✅修正 ✅修正 28

29.

#apc8a1 その他 29

30.

#apc8a1 その他の気になった変更点 ◼ よさそう ◼ ssh コネクションプラグイン ◼ 秘密鍵データ(private_key )や、パスフレーズ( private_key_passphrase)を指定できるようになった?(未検証) ◼ パスワード認証の仕組みを password_mechanism で選択可能になった。 デフォルトは内部実装( ssh_askpass)であり、sshpass は必須 ではなくなった ◼ ◼ デバッグ詳細度を verbosity で指定できるようになった?(未検証) インベントリ ◼ -i でディレクトリ単位で指定した場合、 .ini を除外しなくなった ◼ インベントリ変数に不正な変数名(例 interface-name)があるとパースが失敗するようになった。これにより、 Play 変数などの制約と一貫性があるよう になった ◼ その他 ◼ ◼ selectattr などの jinja2 組み込み機能が ansible.builtin.{name} で呼べるようになった 注意 ◼ paramiko コネクションプラグインが deprecated になった。2.21 で削除予定 ◼ become のタイムアウトはタスクのエラーではなく unreachable になった?(未検証) 詳細は Changelog など参照 https://github.com/ansible/ansible/blob/stable-2.19/changelogs/CHANGELOG-v2.19.rst 30

31.

#apc8a1 まとめ 31

32.

#apc8a1 まとめ ◼ ansible-core 2.19 パフォーマンスやセキュリティ向上が見込める一方、 破壊的な変更が含まれる ◼ 条件式や bool の判定を厳密に指定する必要がある ◼ テンプレートモードは jinja2_native 固定に変更される ◼ 詳細は Porting Guide (*1) や Changelog (*2) を参照 *1: https://docs.ansible.com/ansible/devel/porting_guides/porting_guide_core_2.19.html *2: https://github.com/ansible/ansible/blob/stable-2.19/changelogs/CHANGELOG-v2.19.rst 32

33.

#apc8a1 参考 ◼ Ansible Forum の関連トピック「Core-2.19 templating changes - preview and testing」 ◼ ◼ https://forum.ansible.com/t/core-2-19-templating-changes-preview-and-testing/40759 AnsibleFest 2025 におけるセッション「State of Ansible Core 2025」 ◼ https://cdck-file-uploads-us1.s3.dualstack.us-west-2.amazonaws.com/ansible/original/2X/0/0da27 c590dea9ac752f3dde143a11711a631a130.pdf 33