Ansible telnet module result checking

48 Views Asked by At

I've a playbook with telnet module

- name: run commands over Telnet
  hosts: myhosts
  gather_facts: False
  strategy: free
  tasks:
    - name: switch show
      ansible.netcommon.telnet:
        user: user
        password: pass
        login_prompt: 'sername'
        password_prompt: 'assword'
        prompts:
        - '[>#]'
        command:
        - disable clipaging
        - disable clipaging
        #- somethere
        #
      register: results

    - name: debug results
      debug:
        msg: "{{ results }}"

The debug of result is

{
    "msg": {
        "changed": true,
        "failed": false,
        "output": [
            " disable clipaging\r\nCommand: disable clipaging\r\n\r\nSuccess.\r\n\rn116-c8-acc7:5#",
            " disable clipaging\r\nCommand: disable clipaging\r\n\r\nSuccess.\r\n\rn116-c8-acc7:5#"
        ]
    }
}

How to check the output list in such a way, that if all items contain a "Success" substring, then the playbook is considered to be successfully completed, otherwise failed?

1

There are 1 best solutions below

0
S.Pot On

My current solution:

- name: Checking results
  set_fact:
    result:
      all_cnt: "{{results.output|length}}"
      #succeses: "{{results.output|select('regex', 'ucces|save')|list}}"
      succeses_cnt: "{{results.output|select('regex', 'ucces|save')|list|length}}"
      failures: "{{results.output|reject('regex', 'ucces|save')|list}}"
      failures_cnt: "{{results.output|reject('regex', 'ucces|save')|list|length}}"

- name: Print result
  debug:
    var: result

- name: If failed
  ansible.builtin.fail:
    msg: Switch configuration failure
  when: result.failures_cnt != "0"