Reputation: 91
My goal is to loop over interfces on cisco switch, check access vlan, and then change that vlan when is set to "12" or live as it is when is other then 12. To do that I save result to "int_out":
- name: check vlan on some interfaces and save to int_out
ios_command:
commands: show run int Fa0/{{ item }} | incl switchport access vlan
provider: "{{ cli }}"
with_sequence: start=1 end=3 stride=1
register: int_out
I would like to loop over the "int_out" result and use "when" statemant like below to change access vlan:
- name: change vlan if needed
ios_config:
lines: switchport access vlan 1234
parents: interface Fa0/{{item}}
when: item.stdout_lines == " switchport access vlan 12"
with_items: "{{ int_out.results }}"
but the comparison doesnt work:
TASK [change vlan when needed] *************************************************
skipping: [192.168.0.33] => (item={u'changed': False, '_ansible_no_log': False, u'stdout': [u' switchport access vlan 12'], '_ansible_item_result': True, 'item': u'1', 'invocation': {'module_name': u'ios_command', u'module_args': {u'authorize': False, u'username': u'admin', u'commands': [u'show run int Fa0/1 | incl switchport access vlan'], u'auth_pass': None, u'retries': 10, u'waitfor': None, u'interval': 1, u'host': u'192.168.0.33', u'ssh_keyfile': None, u'timeout': 10, u'provider': u"{'username': 'admin', 'host': '192.168.0.33', 'password': '********'}", u'password': u'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', u'port': 22}}, u'stdout_lines': [[u' switchport access vlan 12']]})
skipping: [192.168.0.33] => (item={u'changed': False, '_ansible_no_log': False, u'stdout': [u' switchport access vlan 12'], '_ansible_item_result': True, 'item': u'2', 'invocation': {'module_name': u'ios_command', u'module_args': {u'authorize': False, u'username': u'admin', u'commands': [u'show run int Fa0/2 | incl switchport access vlan'], u'auth_pass': None, u'retries': 10, u'waitfor': None, u'interval': 1, u'host': u'192.168.0.33', u'ssh_keyfile': None, u'timeout': 10, u'provider': u"{'username': 'admin', 'host': '192.168.0.33', 'password': '********'}", u'password': u'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', u'port': 22}}, u'stdout_lines': [[u' switchport access vlan 12']]})
skipping: [192.168.0.33] => (item={u'changed': False, '_ansible_no_log': False, u'stdout': [u' switchport access vlan 12'], '_ansible_item_result': True, 'item': u'3', 'invocation': {'module_name': u'ios_command', u'module_args': {u'authorize': False, u'username': u'admin', u'commands': [u'show run int Fa0/3 | incl switchport access vlan'], u'auth_pass': None, u'retries': 10, u'waitfor': None, u'interval': 1, u'host': u'192.168.0.33', u'ssh_keyfile': None, u'timeout': 10, u'provider': u"{'username': 'admin', 'host': '192.168.0.33', 'password': '********'}", u'password': u'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', u'port': 22}}, u'stdout_lines': [[u' switchport access vlan 12']]})
If I check the output of "item.stdout_lines" to see waht's wrong:
- debug: msg= "{{ item.stdout_lines }}"
with_items: "{{ int_out.results }}"
I get not only stdout_lines but a lot more:
TASK [debug] *******************************************************************
ok: [192.168.0.33] => (item={u'changed': False, '_ansible_no_log': False, u'stdout': [u' switchport access vlan 12'], '_ansible_item_result': True, 'item': u'1', 'invocation': {'module_name': u'ios_command', u'module_args': {u'authorize': False, u'username': u'admin', u'commands': [u'show run int Fa0/1 | incl switchport access vlan'], u'auth_pass': None, u'retries': 10, u'waitfor': None, u'interval': 1, u'host': u'192.168.0.33', u'ssh_keyfile': None, u'timeout': 10, u'provider': u"{'username': 'admin', 'host': '192.168.0.33', 'password': '********'}", u'password': u'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', u'port': 22}}, u'stdout_lines': [[u' switchport access vlan 12']]}) => {
"item": {
"changed": false,
"invocation": {
"module_args": {
"auth_pass": null,
"authorize": false,
"commands": [
"show run int Fa0/1 | incl switchport access vlan"
],
"host": "192.168.0.33",
"interval": 1,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": 22,
"provider": "{'username': 'admin', 'host': '192.168.0.33', 'password': '********'}",
"retries": 10,
"ssh_keyfile": null,
"timeout": 10,
"username": "admin",
"waitfor": null
},
"module_name": "ios_command"
},
"item": "1",
"stdout": [
" switchport access vlan 12"
],
"stdout_lines": [
[
" switchport access vlan 12"
]
]
},
"msg": ""
}
ok: [192.168.0.33] => (item={u'changed': False, '_ansible_no_log': False, u'stdout': [u' switchport access vlan 12'], '_ansible_item_result': True, 'item': u'2', 'invocation': {'module_name': u'ios_command', u'module_args': {u'authorize': False, u'username': u'admin', u'commands': [u'show run int Fa0/2 | incl switchport access vlan'], u'auth_pass': None, u'retries': 10, u'waitfor': None, u'interval': 1, u'host': u'192.168.0.33', u'ssh_keyfile': None, u'timeout': 10, u'provider': u"{'username': 'admin', 'host': '192.168.0.33', 'password': '********'}", u'password': u'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', u'port': 22}}, u'stdout_lines': [[u' switchport access vlan 12']]}) => {
"item": {
"changed": false,
"invocation": {
"module_args": {
"auth_pass": null,
"authorize": false,
"commands": [
"show run int Fa0/2 | incl switchport access vlan"
],
"host": "192.168.0.33",
"interval": 1,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": 22,
"provider": "{'username': 'admin', 'host': '192.168.0.33', 'password': '********'}",
"retries": 10,
"ssh_keyfile": null,
"timeout": 10,
"username": "admin",
"waitfor": null
},
"module_name": "ios_command"
},
"item": "2",
"stdout": [
" switchport access vlan 12"
],
"stdout_lines": [
[
" switchport access vlan 12"
]
]
},
"msg": ""
}
ok: [192.168.0.33] => (item={u'changed': False, '_ansible_no_log': False, u'stdout': [u' switchport access vlan 12'], '_ansible_item_result': True, 'item': u'3', 'invocation': {'module_name': u'ios_command', u'module_args': {u'authorize': False, u'username': u'admin', u'commands': [u'show run int Fa0/3 | incl switchport access vlan'], u'auth_pass': None, u'retries': 10, u'waitfor': None, u'interval': 1, u'host': u'192.168.0.33', u'ssh_keyfile': None, u'timeout': 10, u'provider': u"{'username': 'admin', 'host': '192.168.0.33', 'password': '********'}", u'password': u'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER', u'port': 22}}, u'stdout_lines': [[u' switchport access vlan 12']]}) => {
"item": {
"changed": false,
"invocation": {
"module_args": {
"auth_pass": null,
"authorize": false,
"commands": [
"show run int Fa0/3 | incl switchport access vlan"
],
"host": "192.168.0.33",
"interval": 1,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": 22,
"provider": "{'username': 'admin', 'host': '192.168.0.33', 'password': '********'}",
"retries": 10,
"ssh_keyfile": null,
"timeout": 10,
"username": "admin",
"waitfor": null
},
"module_name": "ios_command"
},
"item": "3",
"stdout": [
" switchport access vlan 12"
],
"stdout_lines": [
[
" switchport access vlan 12"
]
]
},
"msg": ""
}
But when I run code like this:
- debug: msg="{{int_out.results[1].stdout}}"
I get such response:
TASK [debug] *******************************************************************
ok: [192.168.0.33] => {
"msg": [
" switchport access vlan 12"
]
}
Any idea whats wron'g and how can I achieve my goal?
Upvotes: 1
Views: 1059
Reputation: 68239
If you take close look at int_out.results
elements, you'll notice that stdout
is a list of strings, and stdout_lines
is a list of lists.
I suspect ios_config
module returns a list as stdout
(instead of multiline string in command
/shell
modules) and Ansible's standard result processor splits any stdout
variable into a new list.
So you just need to tweak your when:
statement as follows:
when: item.stdout[0] == " switchport access vlan 12"
to access first element of stdout
list.
Upvotes: 1