ansible 比较文件中的两个字符串并检查是否等于定义的变量



团队,我有一个 kubeconfig 文件,我需要比较上下文名称和当前上下文是否相同,如果是,我想继续,否则失败。 下面是我想与 Ansible 集成和运行的 shell。

cat cluster-user.kubeconfig | yq .contexts[0].name```
"site.test.com"
cat cluster-user.kubeconfig | yq .["current-context"]```
"site.test.com"
- name: "GET API server current-context name with YQ..  "
shell: "cat cluster-user.kubeconfig | yq .["current-context"]"
register: string1
ignore_errors: true
- debug:
var: string1.stdout_lines
when: string1.stdout != ''

- name: "GET API server contexts name with YQ..  "
shell: "cat cluster-user.kubeconfig | yq .contexts[0].name"
register: string2
ignore_errors: true
- debug:
var: string2.stdout_lines
when: string2.stdout != ''

- name: "Validate if both string1 and string2 are same, if yes proceed..  "
failed_when: string2.stdout != string1.stdout

输出:

on the exact syntax problem.
The offending line appears to be:

- name: "Validate if both string1 and string2 are same, if yes proceed..  "
^ here```
anyhint what am i missing?

你能试试这个吗:

- fail:
msg: "Validate if both string1 and string2 are same, if yes proceed..."
when: "'{{ string2.stdout }}' != '{{ string1.stdout }}'"

failed_when:这用于在满足条件时使特定任务失败。Failed_When Ansible 文档

fail:这用于通过自定义消息使进度失败。失败的 Ansible 文档

相关内容

  • 没有找到相关文章

最新更新