When Condition use with Variables

16 views
Skip to first unread message

Pravin V

unread,
Mar 26, 2021, 8:25:22 AM3/26/21
to Ansible Project
Hello Everyone,

I've a task -
- name: Device Description
  uri:
      url: "{{ URL }}/api-db?username={{ USERNAME }};password={{ PASSWORD }};cmds=mget+*+{{ display_name }}+sys+SNMPv2-MIB.sysDescr"
      validate_certs: no
      method: GET
      return_content: yes
  register: get_descriptions

Debug Output 
- debug: msg="{{ get_descriptions.content }}"

ok: [localhost] => 
  msg: |-
   sw01 sys SNMPv2-MIB.sysDescr = Cisco IOS C2960S (C2960S-UNIVERSALK9-M) Version 12.2(55)SE7 RELEASE (fc1)

I'm able to configure the when to run only when - Cisco IOS C2960S found in the output
- debug: msg="{{ get_descriptions.content }}"
  when: "'Cisco IOS C2960S' in get_descriptions.content"

I've created a devices.yml
---
cisco10g:
- C2960S
- c2960x
- c3560x

- debug: msg="{{ cisco10g }}"

Output-
TASK [debug] ********************************************************************************************************************************************************
ok: [localhost] => 
  msg:
  - C2960S
  - c2960x
  - c3560x
  - ct5520

I want to check the entries of devices from the file and only run when it's found. Like below -
- debug: msg="{{ get_descriptions.content }}"
  when:  get_descriptions.content in cisco10g

When I'm writing the code this way. It is always skipping.

please advise

Thanks in advance.

Pravin V

unread,
Mar 29, 2021, 3:37:16 AM3/29/21
to Ansible Project
Hello Everyone,
Please advise.
Reply all
Reply to author
Forward
0 new messages