0

I'm new to ansible and right now I'm trying to search a specific word from my output:

vars:
    vlans:
      - vlan 999 

tasks:

   - name: Check Vlans
     ios_facts:
      gather_subset:
      - '!all'
      gather_network_resources:
      - 'vlans'
     register: vlan_check
     
   - copy: content={{ vlan_check }} dest=/root/ansible/outputs/ios_test/{{ inventory_hostname }}.txt
   
   - name: Check output
     shell: 'grep -i "vlan_id"":" {{ vlans[0]}} /root/ansible/outputs/ios_test/{{ inventory_hostname }}.txt'
     register: output

   - name: Output msg
     debug: msg="'{{ vlans[0] }}'is on switch"
     when: output is changed

Every task is successful but I get this error when the task "Check output" is done:

fatal: [SWAnsible]: FAILED! => {"changed": true, "cmd": "grep -i "vlan_id"":" 999 /root/ansible/outputs/ios_test/SWAnsible.txt", "delta": "0:00:00.004479", "end": "2021-05-10 16:55:02.825909", "msg": "non-zero return code", "rc": 2, "start": "2021-05-10 16:55:02.821430", "stderr": "grep: 999: No such file or directory", "stderr_lines": ["grep: 999: No such file or directory"], "stdout": "", "stdout_lines": []}

I think the problem here is this piece of code here:

shell: 'grep -i "vlan_id"":" {{ vlans[0]}}

I could also just search for 999 but in my opinion this is not unique enough for this. I already tried different methods to exclude the semicolon but it changes the whole output from "vlan_id": 999 to \"vlan_id\"\":\" 999 and thats why the grep is not possbile.

How can I solve this? Thank you

Heiko
  • 1
  • 2
    You're breaking my heart. – Semicolon May 10 '21 at 16:58
  • You are completely miss using ansible and fully in an X/Y problem here IMHO. Can you please update your question with a debug view (teared down to a minimum but complete an reusable dataset if yours is too big) of the `vlan_check` variable? Then also explain clearly what you are looking for in there and the exact expected result. – Zeitounator May 10 '21 at 19:27

0 Answers0