如何使用“何时”条件过滤输出

时间:2020-04-07 11:37:08

标签: ssh ansible

因此,我不到一周就开始使用Ansible。我已经尝试并阅读了多篇有关如何过滤输出的文章。但是,似乎没有任何工作。我正在使用Ansible版本:ansible 2.9.6和python version = 2.7.12 请参阅下面的示例剧本

#This playbook checks if a Cisco Router supports SSH using Telnet 
- name: Checking If Cisco device Supports SSH
  hosts: all
  gather_facts: False
  tasks:
    - name: Telnet to Device
      telnet:
        login_prompt: ": "
        prompts:
          - '[>#]'
        command:
          - show ip ssh | include SSH       
      register: result
    - name: Second Task
      debug:
        msg: "{{inventory_hostname}} does not support SSH !"
        when: not(result.stdout.find('SSH Enabled') != -1)

预期结果:Ansible将搜索路由器的输出,并在result.stdout中搜索字符串“ SSH Enabled”的不存在,因为cisco IOS如果不支持该命令将返回“ Invalid input” 。该手册将在所有设备上运行,而我有另一本手册可在所有设备上启用SSH。执行此剧本时,请参见以下输出。

PLAYBOOK: CheckSSH.yml *************************************************************************************************************************************************************************************
1 plays in CheckSSH.yml

PLAY [Checking If Cisco device Supports SSH] ***************************************************************************************************************************************************************
META: ran handlers

TASK [Telnet to Device] ************************************************************************************************************************************************************************************
task path: /../CheckSSH.yml:8
changed: [XXXX_sw] => changed=true 
  output:
  - |-
    show ip ssh | include SSH
                      ^
    % Invalid input detected at '^' marker.

    XXXX_SW#

TASK [Second Task] *****************************************************************************************************************************************************************************************
task path: /.../CheckSSH.yml:17
fatal: [XXXX_sw]: FAILED! => 
  msg: 'Invalid options for debug: when'

PLAY RECAP *************************************************************************************************************************************************************************************************
XXXX_sw                   : ok=1    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

有人可以解释发生了什么吗?我只想自动知道我的设备是否支持SSH。 我打算在200个设备上运行此命令。此外,我打算使用输出过滤在不同设备上运行诊断程序。

更新:我也应用了建议。在此处查看新代码

---
#This playbook checks if a device supports SSH using Telnet 
#The device must support telnet first though
- name: Checking If Cisco device Supports SSH
  hosts: all
  gather_facts: False
  tasks:
    - name: Telnet to Device
      telnet:
        login_prompt: ": "
        prompts:
          - '[>#]'
        command:
#          - terminal length 0
          - show ip ssh | include SSH       
      register: result

    - name: Second Task      
      debug:
        msg: "{{inventory_hostname}} does not support SSH !"
      when: result.stdout is not search('SSH Enabled')

但是看到我得到的输出

TASK [Second Task] *****************************************************************************************************************************************************************************************
task path: /etc/ansible/CheckSSH.yml:18
fatal: [xxxx]: FAILED! => 
  msg: |-
    The conditional check 'result.stdout is not search('SSH Enabled')' failed. The error was: error while evaluating conditional (result.stdout is not search('SSH Enabled')): 'dict object' has no attribute 'stdout'

    The error appears to be in '/etc/ansible/CheckSSH.yml': line 18, column 7, but may
    be elsewhere in the file depending on the exact syntax problem.

    The offending line appears to be:


        - name: Second Task
          ^ here

2 个答案:

答案 0 :(得分:1)

尝试testing strings

    - debug:
        msg: "{{ inventory_hostname }} does not support SSH !"
      when: result.stdout is not search('SSH Enabled')

答案 1 :(得分:-1)

这也可以,对 when 语句略有不同

when: "'ssh enabled' not in (result.stdout | join(''))"