Compare two variables

1,901 views
Skip to first unread message

marcin_...@hotmail.com

unread,
Dec 1, 2016, 12:58:11 PM12/1/16
to Ansible Project
Hi All,

I am trying to compare two variables however it looks like it does not work. I am new to ansible so please be understanding. I know that this script is not optimal but I am still learning.

My code looks like this

---
- name: change MTU
  hosts: router
  connection: local

  vars:
    mtu: []
    mtu_1: '["1500"]'
    contents: "{{ lookup('file', '/home/marcin/var.json') }}"

  tasks:
    - name: show ip interface et0/0
      ios_command:
        commands: 
          - sh ip interface et0/0
        provider: "{{ cli }}"
      register: showrun
    - copy: content="{{ showrun.stdout_lines | to_nice_json }}" dest="/home/marcin/show.json"

    - name: grep
      shell: grep -Eo "([A-Z])\w+\s\is\s[0-9]+" /home/marcin/show.json
      register: my_grep
    - debug: var=my_grep
    - copy: content="{{ my_grep.stdout_lines }}" dest="/home/marcin/var.json"

    - debug: var=contents

    - name: check MTU
      set_fact:
        mtu: "{{ mtu + [item.split()[2]] }}"
      with_items: 
        - "{{ contents }}"
    - debug: msg="{{ mtu }}"

    - name: change MTU if smaller then 1500
      ios_config:
        lines:
          - ip mtu 1500
        parents: interface ethernet0/0
        provider: "{{ cli }}"

    - debug: msg="MTU match"
      when: 'mtu ==  mtu_1'

    - debug: msg="MTU mismatch"
      when: 'mtu !=  mtu_1'

    - debug: var=mtu
    - debug: var=mtu_1


this is what I get when I run ansible even I see that both var's are the same the script change the MTU under interface.


TASK [change MTU if smaller then 1500] *****************************************
changed: [10.10.10.3]

TASK [debug] *******************************************************************
skipping: [10.10.10.3]

TASK [debug] *******************************************************************
ok: [10.10.10.3] => {
    "msg": "MTU mismatch"
}

TASK [debug] *******************************************************************
ok: [10.10.10.3] => {
    "mtu": [
        "1500"
    ]
}

TASK [debug] *******************************************************************
ok: [10.10.10.3] => {
    "mtu_1": [
        "1500"
    ] 
}

PLAY RECAP *********************************************************************
10.10.10.3                 : ok=13   changed=2    unreachable=0    failed=0   


Any advice what I am doing wrong?


marcin_...@hotmail.com

unread,
Dec 1, 2016, 3:23:30 PM12/1/16
to Ansible Project
Problem solved

This is working scenario

---
- name: change MTU
  hosts: router
  connection: local

  vars:
    mtu_1: 'MTU is 1500'
    contents: "{{ lookup('file', '/home/marcin/var.json') }}"

  tasks:
    - name: show ip interface et0/0
      ios_command:
        commands: 
          - sh ip interface et0/0
        provider: "{{ cli }}"
      register: showrun
    - copy: content="{{ showrun.stdout_lines | to_nice_json }}" dest="/home/marcin/show.json"

    - name: grep
      shell: grep -Eo "([A-Z])\w+\s\is\s[0-9]+" /home/marcin/show.json
      register: my_grep
    - copy: content="{{ my_grep.stdout }}" dest="/home/marcin/var.json"

    - debug: var=contents


    - name: change MTU if smaller then 1500
      ios_config:
        lines:
          - ip mtu 1500
        parents: interface ethernet0/0
        provider: "{{ cli }}"
      when: 
       - contents !=  mtu_1

    - debug: msg="MTU mismatch"
      when: 
       - contents !=  mtu_1
       
    - debug: msg="MTU match"
      when: 
       - contents ==  mtu_1
Reply all
Reply to author
Forward
0 new messages