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

时间:2017-10-31 00:16:50

标签: 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

- 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

这是debug stdout:

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  

2 个答案:

答案 0 :(得分:2)

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

  

[警告]:when语句不应包含jinja2模板分隔符,例如{{}}或{%%}。找到:not(&#39;版本{{version}}&#39;在result.stdout [0]中)

修复你的代码:

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

可能不需要围绕字符串连接使用括号。另外,给出你的版本字符串, 我根本不确定你是否需要连接,但问题是要求它。

答案 1 :(得分:-1)

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