質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ansible

Ansibleは、Python で書かれたサーバーの設定を管理するための 構成管理ツールです。

Q&A

解決済

1回答

525閲覧

Ansible selectattrによる操作でエラーとなる

khaii21

総合スコア66

Ansible

Ansibleは、Python で書かれたサーバーの設定を管理するための 構成管理ツールです。

0グッド

0クリップ

投稿2023/04/24 05:11

編集2023/04/24 06:54

実現したいこと

①の処理で実行したコマンド結果(command1_result.stdout)で、"stdout_lines"キーに"hoge.repo"の文字列が含まれている場合、②でcommand2を実行したい

色々と試しているのですがエラーが解消しません。
何かお気づきことがありましたらご教授頂けないでしょうか。
宜しくお願い致します。

発生している問題・エラーメッセージ

fatal: [localhost]: FAILED! => {"msg": "The conditional check 'command1_result.stdout | selectattr(\"stdout_lines\", \"contains\", \"hoge.repo\") | list | length == 0' failed. The error was: error while evaluating conditional (command1_result.stdout | selectattr(\"stdout_lines\", \"contains\", \"hoge.repo\") | list | length == 0): 'str object' has no attribute 'stdout_lines'. 'str object' has no attribute 'stdout_lines'\n\nThe error appears to be in '/test.yml': line 22, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n var: command1_result\n- name: execution command2\n ^ here\n"}

該当のソースコード

test.yml抜粋

1# ① 2- name: activate package repo 3 ansible.builtin.command: command1 4 register: command1_result 5 6# ② 7- name: execution command2 8 ansible.builtin.command: command2 9 when: command1_result.stdout | selectattr("stdout_lines", "contains", "hoge.repo") | list | length == 0

command1_result.stdoutの抜粋

1 "stdout_lines": [ 2 "This is test repo", 3 "adding repo from: http://www.hoge.co.jp:8080/hoge.repo", 4 "repo saved to /etc/yum.repos.d/hoge.repo" 5 ]

試したこと

下記を試しましたが同様のエラーなります
selectattr("stdout_lines", "match", "hoge.repo")
selectattr("stdout_lines", "eq", "hoge.repo")

下記の行をコメントアウトした場合、エラーは解消されます。
when: command1_result.stdout | selectattr("stdout_lines", "contains", "hoge.repo") | list | length == 0

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

"hoge.repo"の文字列が含まれている場合、②でcommand2を実行したい

when のところを以下のようにしていかがでしょうか。

yaml

1 when: command1_result.stdout_lines | select("regex", "hoge.repo") | list | length >= 1

投稿2023/04/24 09:22

yabusakadeny

総合スコア157

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

khaii21

2023/04/24 10:46

ご確認ありがとうございます。 ご教授頂きました方法でエラーなく想定通りの処理が出来ました。 大変助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問