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

ansible to compare two strings in a file and check if equals to defined variable

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

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 Documentation

fail:这用于通过自定义消息使进度失败。 Fail Ansible Documentation