Anonymous
Anonymous

Reputation: 35

Is There A Way I Can Compare Ansible Assert Function with Items in YAML File Instead of a List?

I need to apply the missing logging server configuration on my network devices and also need to compare the running config with the ip's in group_vars yaml file using the Assert function.

My Current Assert function looks like this::

---

- name: Add Logging Server Configuration
  hosts: nxos

  tasks:

    - name: Pre-Change
      nxos_command:
         commands: show running-config | include "logging server"
      register: pre_output

    - name: Display Pre-Check
      debug:
        var: pre_output.stdout_lines

    - name: Pre-Configuration Verification Status
      assert:
        that:
          - "'10.1.1.2' not in pre_output.stdout[0] or '10.1.1.3' not in pre_output.stdout[0]"
        fail_msg: "Logging server configuration already exists on the device"
        success_msg: "Logging server configuration is missing, proceeding with adding config on the device"

group_vars/all.yml file:

logging_servers: ["10.1.1.2", "10.1.1.3"]

Is there a way if I can compare with items in all.yml directly instead of hard coding the ip's in assert function?

Upvotes: 0

Views: 165

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68144

Try this

    - assert:
        that: logging_servers|
              select('in', pre_output.stdout_lines.0)|list|
              length == no_of_servers|int
        fail_msg: "Logging server configuration is missing"
        success_msg: "Logging server configuration exists"
      vars:
        no_of_servers: "{{ logging_servers|length }}"

Upvotes: 1

Related Questions