複数行レジスタに文字列がない場合は、可能なタスクを実行します。

複数行レジスタに文字列がない場合は、可能なタスクを実行します。

設定が欠落している場合にのみ、レジストリ出力から文字列を取得してネットワークデバイスの設定をプッシュしたいと思います。まず、このタスクを実行して、ターゲットデバイスで実行されている設定を記録します。

  # Collect information about the available configuration
- name: Execute show command
  cisco.ios.ios_command:
    commands: 
    - show runn | in repository ## to find if the repository is already configured
  register: output

出力レジスタを使って次のタスクを条件付きで実行したいと思います。

- name: Push repository configuration
  cisco.ios.ios_command:
    commands:
    - conf t
    - repository MAIN
    - url ftp://{{ repository_main }}
    - user {{ repository_main_user}} password plain {{ repository_main_password }}
    - exit
    - repository SECONDARY
    - url ftp://{{ repository_sec }}/
    - user {{ repository_sec_user}} password plain {{ repository_sec_password }}
    - end
  when: 'not "MAIN" in {{ output.stdout }} and not "SECONDARY" in {{ output.stdout }}'

Output.stdoutは次のようになります。

TASK [print output] ************************************************************
Saturday 19 November 2022  06:45:44 +0000 (0:00:06.370)       0:00:06.409 ***** 
ok: [node1] => 
  msg:
  - |-
    repository MAIN
    --
    repository SECONDARY

ネットワークデバイスでansibleユーザーのセッションを確認するときにリポジトリを再設定するのがわかりますが、これは私が望むものではありません。これをどのように制御できますか?実際、stdoutは複数行出力の要素ですか?

ベストアンサー1

修理条件

      when:
        - "'MAIN' not in output.stdout"
        - "'SECONDARY' not in output.stdout"

試験を受ける

- hosts: localhost

  tasks:

    - debug:
        msg: OK
      when:
        - "'MAIN' not in output.stdout"
        - "'SECONDARY' not in output.stdout"
      vars:
        output:
          stdout: XY

    - debug:
        msg: OK
      when:
        - "'MAIN' not in output.stdout"
        - "'SECONDARY' not in output.stdout"
      vars:
        output:
          stdout: XY MAIN

    - debug:
        msg: OK
      when:
        - "'MAIN' not in output.stdout"
        - "'SECONDARY' not in output.stdout"
      vars:
        output:
          stdout: XY SECONDARY

与えられた

PLAY [localhost] *****************************************************************************

TASK [debug] *********************************************************************************
ok: [localhost] => 
  msg: OK

TASK [debug] *********************************************************************************
skipping: [localhost]

TASK [debug] *********************************************************************************
skipping: [localhost]

PLAY RECAP ***********************************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

おすすめ記事