実現したいこと
ファイルが存在しない場合、”stat”キーにある”exists”の結果がfalseなります。
falseだった場合にはNOを出力したいです。
ファイルの有無確認を行い、存在しない場合にはNOのメッセージを出力するplaybookを作成したのですが実行するとエラーとなります。
チェック対象の結果に対して意図通りに値が取得できません。
何かお気づきの方がいらっしゃましたらご教授頂けないでしょうか。
宜しくお願い致します。
発生している問題・エラーメッセージ
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'chk_file | selectattr(\"exists\", \"match\", \"false\") | list | length == 1' failed. The error was: error while evaluating conditional (chk_file | selectattr(\"exists\", \"match\", \"false\") | list | length == 1): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'exists'. 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'exists'\n\nThe error appears to be in '/hoge.yml': line ??, column ?, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# ファイルが無い場合にはNO\n- name: hoge_hoge\n ^ here\n"}
該当のソースコード
playbook.yml
1- name: Check /etc/hoge.txt 2 ansible.builtin.stat: 3 path: /etc/hoge.txt 4 register: chk_file 5- name: Check chk_file 6 ansible.builtin.debug: 7 var: chk_file 8 9# ファイルが無い場合にはNO 10- name: hoge_hoge 11 ansible.builtin.debug: 12 msg: "NO" 13 register: execution_add_repo 14 when: chk_file | selectattr("exists", "match", "false") | list | length == 1
chk_fileの結果
1ok: [localhost] => { 2 "chk_file": { 3 "ansible_facts": { 4 "discovered_interpreter_python": "/usr/bin/python" 5 }, 6 "changed": false, 7 "failed": false, 8 "stat": { 9 "exists": false 10 } 11 } 12}
試したこと
selectattrの代わりにselectを使いましたがSyntax Errorになります
when: chk_file | select("regex", "\"exists\"\: false") | list | length == 3
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/05/10 04:29
2023/05/10 07:42