Ansible - 循环结果+ when语句

时间:2016-10-10 18:35:44

标签: ansible ansible-playbook

我的目标是遍历cisco交换机上的接口,检查access vlan,然后在设置为" 12"时更改该vlan。或者生活在12岁以外的其他地方。为此,我将结果保存到" 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

我想循环遍历" int_out"结果和使用"当"如下所示的statemant来改变访问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"
    ]
}

任何想法是什么?我怎样才能实现目标?

1 个答案:

答案 0 :(得分:0)

如果仔细查看int_out.results个元素,您会注意到stdout是一个字符串列表,而stdout_lines是一个列表列表。
我怀疑ios_config模块将列表返回为stdout(而不是command / shell模块中的多行字符串),Ansible的标准结果处理器会将{{1变量到新列表中。

所以你只需要按如下方式调整stdout语句:

when:

访问when: item.stdout[0] == " switchport access vlan 12" 列表的第一个元素。