1
我的目標是循環訪問cisco交換機上的接口,檢查訪問vlan,然後在設置爲「12」時更改該vlan,或者在其他時候更改爲12。這樣做,我保存結果「int_out」:Ansible - 循環播放結果+語句
- 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
我想遍歷所有的「int_out」的結果,並使用「時,」就像下面衆所周知聲明改變接入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 }}"
但比較不起作用:
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']]})
如果我檢查「item.stdout_lines」的輸出,看啥子的錯誤:
- debug: msg= "{{ item.stdout_lines }}"
with_items: "{{ int_out.results }}"
我得到的不僅stdout_lines但多了很多:
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": ""
}
但是當我運行的代碼一樣這樣的:
- debug: msg="{{int_out.results[1].stdout}}"
我得到這樣的迴應:
TASK [debug] *******************************************************************
ok: [192.168.0.33] => {
"msg": [
" switchport access vlan 12"
]
}
任何想法什麼是wron'g,我怎樣才能實現我的目標?
太好了。就是這個! –
謝謝你的幫助。 –