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
On Thursday, 1 December 2016 17:58:11 UTC, [email protected] wrote:
>
> 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?
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/d27014f4-e791-402d-9f4c-73578e2f8aa8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.