conditional query

22 views
Skip to first unread message

jb79...@gmail.com

unread,
Mar 3, 2017, 10:33:57 AM3/3/17
to Ansible Project
Hello,

I am trying to enforce a conditional match to check if a VLAN is configured on a switch before applying changes

I can see that the variable is being recorded, but the match against it always results in the skipping of the task 'interface e01' even though the vlan number is present.


PLAY [arista] ******************************************************************


TASK [check VLAN] **************************************************************

ok: [arista]


TASK [debug] *******************************************************************

ok: [arista] => {

    "msg": "test: [u'VLAN  Name                             Status    Ports\\n----- -------------------------------- --------- -------------------------------\\n1     default                          active    Et2, Et3\\n100   VLAN0100                         active    Et1\\n200   VLAN0200                         active   \\n']"

}


TASK [interface e01] ***********************************************************

skipping: [arista]



playbook file:


---


- hosts: arista

  gather_facts: no

  connection: local

  strategy: free

  tasks:

    - name: check VLAN

      ignore_errors: yes

      eos_command:

        host: arista

        username: admin

        password: ccc

        commands:

          - show vlan

      register: test

    - debug:

        msg: "test: {{ test.stdout }}"

    - name: interface e01

      ignore_errors: yes

      eos_config:

        host: arista

        username: admin

        password: ccc

        authorize: yes 

        lines: 

          - switchport access vlan 200

          - no shutdown

        parents: 

          - interface Ethernet1

      when: '"200" in test'

Kai Stian Olstad

unread,
Mar 3, 2017, 10:49:50 AM3/3/17
to ansible...@googlegroups.com
On 03. mars 2017 10:32, jb79...@gmail.com wrote:
> - debug:
> msg: "test: {{ test.stdout }}"
>
> - name: interface e01
> ignore_errors: yes
> eos_config:
> host: arista
> username: admin
> password: ccc
> authorize: yes
> lines:
> - switchport access vlan 200
> - no shutdown
> parents:
> - interface Ethernet1
> when: '"200" in test'

You need to use "test.stdout" as you did in the debug task.

--
Kai Stian Olstad

jb79...@gmail.com

unread,
Mar 4, 2017, 5:20:09 AM3/4/17
to Ansible Project
The task is still being skipped even though the VLAN 200 is present in the variable test.

Can I confirm the syntax is correct? I have written it like:

        lines: 

          - switchport access vlan 200

          - description nan

          - no shutdown

        parents: 

          - interface e01

      when: "'200' in test.stdout"


thanks


Kai Stian Olstad

unread,
Mar 4, 2017, 5:55:19 AM3/4/17
to ansible...@googlegroups.com
On 04. mars 2017 11:20, jb79...@gmail.com wrote:
> The task is still being skipped even though the VLAN 200 is present in the
> variable test.

In the debug task use var: instead of msg: when looking at variables,
it's then easier to see their structure.
- debug:
var: test.stdout

>
> Can I confirm the syntax is correct? I have written it like:
>
> lines:
> - switchport access vlan 200
> - description nan
> - no shutdown
> parents:
> - interface e01
> when: "'200' in test.stdout"

After looking more closely at the output is seams that test.stdout is a
list.
So get the first entry you would need to use test.stdout.0

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages