String 如何匹配playbook输出中的字符串?

String 如何匹配playbook输出中的字符串?,string,ansible,stdout,String,Ansible,Stdout,我正在尝试根据我的路由器版本创建一个报告,以检查哪个版本正确,哪个版本不正确。但不知何故,即使我输入了版本的精确匹配,它仍然返回R1,R2的IOS不匹配。 你能看一下让我知道我做错了什么吗 代码如下: - hosts: localhost tasks: - file: path=/etc/ansible/version_report.txt state=absent - file: path=/etc/ansible/version_report.txt state=touch -

我正在尝试根据我的路由器版本创建一个报告,以检查哪个版本正确,哪个版本不正确。但不知何故,即使我输入了版本的精确匹配,它仍然返回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"
}
ok:[R2]=>{
“result.stdout[0]:“Cisco IOS XE软件,版本03.13.03.S-扩展支持发布\nCisco IOS软件,CSR1000V软件(X86\u 64\u LINUX\u IOSD-UNIVERSALK9-M),版本15.4(3)S3,发布软件(fc1)\n技术支持:http://www.cisco.com/techsupport\nCopyright(c)1986-2015由Cisco Systems,Inc.\n于2015年5月28日星期四13:02由mcpre\n\n\nCisco IOS-XE software编译,版权(c)2005-2015由Cisco Systems,Inc.\n保留所有权利。Cisco IOS-XE软件的某些组件根据GNU通用公共许可证(“GPL”)许可版本2.0。根据GPL版本2.0许可的软件代码是免费软件,绝对没有保修。您可以根据GPL版本2.0的条款重新分发和/或修改此类\nGPL代码。有关详细信息,请参阅文档或\“许可通知”\IOS-XE软件附带的文件,\n或IOS-XE软件附带的传单上提供的适用URL。\n\n\nROM:IOS-XE ROMMON\n\nR2正常运行时间为32周、2天、14小时、47分钟\n此控制处理器的正常运行时间为32周、2天、14小时、51分钟\n系统通过重新加载返回到ROM \n系统映像文件为\“bootflash:packages.conf\"\nLast重新加载原因:\n\n\n\n此产品包含加密功能,并受美国各州和当地国家/地区有关进口、出口、转让和使用的法律管辖。交付Cisco加密产品并不意味着\n第三方有权进口、出口、分发或使用加密。\n搬运工、出口商、分销商和用户负责遵守美国和当地国家的法律。使用本产品,您可以遵守适用的法律法规。如果您无法遵守美国和当地的法律,请立即退回本产品。\n\n有关Cisco加密产品的美国法律摘要,请访问:\nhttp://www.cisco.com/wwl/export/crypto/tool/stqrg.html\n\n如果您需要进一步帮助,请发送电子邮件至与我们联系\nexport@cisco.com.\n\n许可级别:ax\n许可类型:默认。找不到有效的许可证。\n下一个重新加载许可级别:ax\n\ncisco CSR1000V(VXE)处理器(VXE版本)具有794186K/6147K字节的内存。\n处理器板ID 9CGYDZIMALX\n5千兆以太网接口\n32768K字节的非易失性配置内存。\n25062800K字节的物理内存。\n7774207K字节的引导闪存虚拟硬盘:。\n\n配置寄存器为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  
任务[lineinfle]*******************************************************************************************************************************************************
任务路径:/etc/ansible/playbooks/check version without curlybrackets.yml:15
正在跳过:[R1]=>{
“更改”:错误,
“跳过原因”:“条件结果为假”,
“跳过”:true
}
使用模块文件/usr/lib/python2.7/dist-packages/ansible/modules/files/lineinflee.py
为用户建立本地连接:root
EXEC/bin/sh-c'echo~&睡眠0'
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'
将/tmp/tmp3uMTqV放到/home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466/lineinfle.py
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/lineinfle.py&&sleep 0'
EXEC/bin/sh-c'/usr/bin/python/home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466/lineinfle.py;rm-rf”/home/nanis/.ansible/tmp/ansible-tmp-1509412236.83-100172408761466/“>/dev/null 2>&1&&sleep 0”
确定:[R2]=>{
“备份”:“,
“更改”:错误,
“差异”:[
{
“在“:”之后,
“在_标题之后”:“version_report.txt(content)”,
“在“:”之前,
“在标题之前”:“version\u report.txt(content)”
}, 
{
“在_头之后”:“version_report.txt(文件属性)”,
“在_头之前”:“version_report.txt(文件属性)”
}
], 
“调用”:{
“模块参数”:{
“属性”:空,
“backrefs”:false,
“备份”:false,
“内容”:空,
“创建”:错误,
“分隔符”:空,
“dest”:“version_report.txt”,
“目录模式”:空,
“跟随”:错误,
“强制”:无效,
“组”:空,
“insertafter”:空,
“insertbefore”:空,
“行”:“R2:IOS版本不匹配”,
“模式”:空,
“所有者”:空,
“路径”:“version_report.txt”,
“regexp”:“R2”,
“远程_src”:空,
“selevel”:空,
“serole”:空,
“setype”:空,
“seuser”:空,
“src”:空,
“国家”:“目前”,
“不安全写入”:null,
“验证”:空
}
}, 
“msg”:”
}
META:ran处理程序
META:ran处理程序
重演*****************************************************************************************************************************************
when: "not (('Version ' + version) in result.stdout[0])"