Возможность сравнить две строки в файле и проверить, равно ли оно определенной переменной - PullRequest
0 голосов
/ 01 октября 2019

Команда, у меня есть файл kubeconfig, и мне нужно сравнить, если имя контекста и текущий контекст совпадают, если да, я хочу продолжить, если не получится. ниже моя оболочка, которую я хочу интегрировать и запустить с 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?


1 Ответ

1 голос
/ 01 октября 2019

Можете ли вы попробовать это:

- 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

...