【问题标题】:How to match a string from playbook output?如何匹配剧本输出中的字符串?
【发布时间】:2017-10-31 00:16:49
【问题描述】:

我正在尝试根据我的路由器版本创建一个报告,以检查哪个版本正确,哪个版本不正确。但不知何故,即使我将版本完全匹配,它仍然返回 R1 并且 R2 有IOS不匹配。 你能看看,让我知道我做错了什么吗?

代码如下:

- hosts: localhost
  tasks:
  - file: path=/etc/ansible/version_report.txt state=absent
  - file: path=/etc/ansible/version_report.txt state=touch

- hosts: all
  connection: local
  gather_facts: no
  tasks:
  - ios_command:
      commands: show version
      provider: "{{cli}}"
    register: result
  - debug: var=result
  - lineinfile:
      dest:  /etc/ansible/version_report.txt
      regexp: "{{inventory_hostname}}"
      line:   "{{inventory_hostname}}: IOS version mismatch"
    when: "not ('Version {{version}}' in result.stdout[0])"

这是剧本的结果:

TASK [lineinfile] *******************************************************************************************************************************************************
 [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: not ('Version {{version}}' in result.stdout[0])

skipping: [R1]
 [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: not ('Version {{version}}' in result.stdout[0])

skipping: [R2]

PLAY RECAP **************************************************************************************************************************************************************
R1                         : ok=2    changed=0    unreachable=0    failed=0   
R2                         : ok=2    changed=0    unreachable=0    failed=0   
localhost                  : ok=3    changed=2    unreachable=0    failed=0 

这是我的变量:

location: Building2
syslog_host: 10.96.7.8
version: "15.4(3)S3"
cli:
 transport: cli
root@virtual-machine:/etc/ansible# cat group_vars/ios.yml  
---
syslog_host: 10.97.6.5
location: Building6 
version: "03.13.03.S - Extended Support Release"
cli:
 transport: cli

这是我的一个路由器的输出:

  R1#sh ver
Cisco IOS XE Software, Version 03.13.03.S - Extended Support Release
Cisco IOS Software, CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 15.4(3)S3, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2015 by Cisco Systems, Inc.
Compiled Thu 28-May-15 13:02 by mcpre

如果我检查了我的 report.txt,这就是我得到的。

virtual-machine:/etc/ansible# cat playbooks/version_report.txt 
R1: IOS version mismatch
R2: IOS version mismatch

这里是调试标准输出:

ok: [R2] => {
    "result.stdout[0]": "Cisco IOS XE Software, Version 03.13.03.S - Extended Support Release\nCisco IOS Software, CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 15.4(3)S3, RELEASE SOFTWARE (fc1)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2015 by Cisco Systems, Inc.\nCompiled Thu 28-May-15 13:02 by mcpre\n\n\nCisco IOS-XE software, Copyright (c) 2005-2015 by cisco Systems, Inc.\nAll rights reserved.  Certain components of Cisco IOS-XE software are\nlicensed under the GNU General Public License (\"GPL\") Version 2.0.  The\nsoftware code licensed under GPL Version 2.0 is free software that comes\nwith ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such\nGPL code under the terms of GPL Version 2.0.  For more details, see the\ndocumentation or \"License Notice\" file accompanying the IOS-XE software,\nor the applicable URL provided on the flyer accompanying the IOS-XE\nsoftware.\n\n\nROM: IOS-XE ROMMON\n\nR2 uptime is 32 weeks, 2 days, 14 hours, 47 minutes\nUptime for this control processor is 32 weeks, 2 days, 14 hours, 51 minutes\nSystem returned to ROM by reload\nSystem image file is \"bootflash:packages.conf\"\nLast reload reason: <NULL>\n\n\n\nThis product contains cryptographic features and is subject to United\nStates and local country laws governing import, export, transfer and\nuse. Delivery of Cisco cryptographic products does not imply\nthird-party authority to import, export, distribute or use encryption.\nImporters, exporters, distributors and users are responsible for\ncompliance with U.S. and local country laws. By using this product you\nagree to comply with applicable laws and regulations. If you are unable\nto comply with U.S. and local laws, return this product immediately.\n\nA summary of U.S. laws governing Cisco cryptographic products may be found at:\nhttp://www.cisco.com/wwl/export/crypto/tool/stqrg.html\n\nIf you require further assistance please contact us by sending email to\nexport@cisco.com.\n\nLicense Level: ax\nLicense Type: Default. No valid license found.\nNext reload license Level: ax\n\ncisco CSR1000V (VXE) processor (revision VXE) with 794186K/6147K bytes of memory.\nProcessor board ID 9CGYDZIMALX\n5 Gigabit Ethernet interfaces\n32768K bytes of non-volatile configuration memory.\n2506280K bytes of physical memory.\n7774207K bytes of virtual hard disk at bootflash:.\n\nConfiguration register is 0x2102"
}

这是 linefile 任务的详细输出:

TASK [lineinfile] *******************************************************************************************************************************************************
task path: /etc/ansible/playbooks/check-version-withoutcurlybrackets.yml:15
skipping: [R1] => {
    "changed": false, 
    "skip_reason": "Conditional result was False", 
    "skipped": true
}
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/files/lineinfile.py
<192.168.1.2> ESTABLISH LOCAL CONNECTION FOR USER: root
<192.168.1.2> EXEC /bin/sh -c 'echo ~ && sleep 0'
<192.168.1.2> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466 `" && echo ansible-tmp-1509412236.83-100172408761466="` echo /home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466 `" ) && sleep 0'
<192.168.1.2> PUT /tmp/tmp3uMTqV TO /home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466/lineinfile.py
<192.168.1.2> EXEC /bin/sh -c 'chmod u+x /home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466/ /home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466/lineinfile.py && sleep 0'
<192.168.1.2> EXEC /bin/sh -c '/usr/bin/python /home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466/lineinfile.py; rm -rf "/home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466/" > /dev/null 2>&1 && sleep 0'
ok: [R2] => {
    "backup": "", 
    "changed": false, 
    "diff": [
        {
            "after": "", 
            "after_header": "version_report.txt (content)", 
            "before": "", 
            "before_header": "version_report.txt (content)"
        }, 
        {
            "after_header": "version_report.txt (file attributes)", 
            "before_header": "version_report.txt (file attributes)"
        }
    ], 
    "invocation": {
        "module_args": {
            "attributes": null, 
            "backrefs": false, 
            "backup": false, 
            "content": null, 
            "create": false, 
            "delimiter": null, 
            "dest": "version_report.txt", 
            "directory_mode": null, 
            "follow": false, 
            "force": null, 
            "group": null, 
            "insertafter": null, 
            "insertbefore": null, 
            "line": "R2: IOS version mismatch", 
            "mode": null, 
            "owner": null, 
            "path": "version_report.txt", 
            "regexp": "R2", 
            "remote_src": null, 
            "selevel": null, 
            "serole": null, 
            "setype": null, 
            "seuser": null, 
            "src": null, 
            "state": "present", 
            "unsafe_writes": null, 
            "validate": null
        }
    }, 
    "msg": ""
}
META: ran handlers
META: ran handlers

PLAY RECAP **************************************************************************************************************************************************************
R1                         : ok=2    changed=0    unreachable=0    failed=0   
R2                         : ok=3    changed=0    unreachable=0    failed=0   
localhost                  : ok=3    changed=2    unreachable=0    failed=0  

【问题讨论】:

  • 根据人们的标记是什么,因为它需要更多的研究?有人可以详细说明吗?有没有

标签: string ansible stdout


【解决方案1】:

您应该从收到的警告中得到提示:

[警告]:when 语句不应包含 jinja2 模板分隔符,例如 {{ }} 或 {% %}。找到:not ('Version {{version}}' in result.stdout[0])

并修复您的代码:

when: "not (('Version ' + version) in result.stdout[0])"

可能不需要围绕字符串连接的括号。此外,鉴于您的版本字符串, 我不确定您是否需要串联,但问题要求它。

【讨论】:

  • 感谢 Techraf ,所以我如何将版本变量传递给: (('Version ' + version)。你的意思是我应该将其输入为 (('Version ' + 03.13.03.S) ?
  • 没有。我的意思是我发布的内容。
  • 我把它改成了那个,但它仍然没有给出正确的输出。它仍然声称路由器在以下情况下存在 IOS 不匹配:“not (('Version ' + version) in result.stdout[0])” virtual-machine:/etc/ansible# more playbooks/version_report.txt R1: IOS version mismatch R2:IOS 版本不匹配,是的,你是对的,我不再收到分隔符错误?
  • 我添加了debug -vvv输出,你能看看吗?
【解决方案2】:

这似乎是一个目录问题。我创建的文件(version-report.txt)在 etc/ansible 下,但是 playbook 在本地目录下创建了一个结果 verstion-report.txt 文件。

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 2018-09-29
    • 1970-01-01
    • 2022-11-30
    • 2023-01-03
    • 2019-03-05
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多