mikeschultz
mikeschultz

Reputation: 33

How to have each line in .stdout_lines/array match a variable

I'm trying to write an Ansible playbook. Need help writing one of the tasks. How do I correctly write the "when:" in the task named CONF T?

One of my registers will include an array:

{
    "current_ntp_servers.stdout_lines": [
        "ntp server 10.190.248.248",
        "ntp server 10.190.248.8",
        "ntp server 10.190.248.16",
        "ntp server 10.190.248.1",
        "ntp server 10.190.248.9",
        "ntp server 10.190.248.17",
        "ntp server 10.190.248.2",
        "ntp server 10.190.248.10",
        "ntp server 10.190.248.34",
        "ntp server 10.190.248.98",
        "ntp server 10.190.248.18",
        "ntp server 10.180.248.3"
    ]
}

I also have a pre-defined variable:

vars:
    ntp_server: "ntp server 10.190.248.248"

I want a task to run when any of the items in the array DO NOT match the predefined variable. However, the variable CAN match any line in the array and the task will still need to run if other lines do not match.

Even if ntp_server exists in the array, I still need the task to run if other array items do not match. So I have to match it from using the array items and checking each one for a match to ntp_server. And if a match is not made, run the task. But the task only needs to run once even if there are multiple non-matches.

The task named "CONF T" and the associated "when:" does not work for me. The task named "remove ntp servers" works correctly.

---

- hosts: switches
  gather_facts: false
  connection: network_cli
  vars:
    ntp_server: "ntp server 10.190.248.248"


  tasks:
    - name: "sh run | s ntp server"
      cli_command:
        command: sh run | s ntp server
      register: current_ntp_servers

    - name: THESE ARE THE CURRENT NTP SERVERS CONFIGURED ON THIS DEVICE BEFORE ANY CHANGES
      debug:
        var: current_ntp_servers.stdout_lines

    - name: REMOVE NTP SERVERS CONF T
      block:
        - name: CONF T
          when: "(current_ntp_servers.stdout_lines not in ntp_server)"
          cli_command:
            command: conf t
          register: conf_t
        - debug:
            var: conf_t
        - name: remove ntp servers
          with_items: "{{ current_ntp_servers.stdout_lines }}"
          when: "(item != ntp_server)"
          cli_command:
            command: "no {{item}}"
          register: remove_ntp_output.stdout_lines

Ansible is new to me. I'm a network engineer with 10 years exp but with no coding experience.

Upvotes: 3

Views: 118

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Q: "I want a task to run when any of the items in the array DO NOT match the predefined variable."

The condition below does the job

- debug:
    msg: This task will run
  when: current_ntp_servers.stdout_lines|                           
        reject('match', ntp_server)|
        list|
        length > 0

Test match can be changed to equalto or search depending on the use-case.

Upvotes: 1

Related Questions