When conditional does not work with ios_config module. Why is it so?

42 views
Skip to first unread message

Vikram S

unread,
Feb 16, 2021, 3:04:25 AM2/16/21
to Ansible Project

1st playbook uses ios_command module and gets executed as intended (to display the output when hostname of device contains router1).

However 2nd playbook uses ios_config module and fails when i use the 'when' conditional. Am i do something wrong or is it that 'when' conditional is not supposed to work with ios_config module?  (please ignore the formatting)

---

- name: show hostname

  hosts: R1


  tasks:

    - name: show hostname

      ios_command: 

        commands: "show run | i hostname"

            

      register: output

 

    - debug:

        var: output

      when: "'router1' in output.stdout[0]"

-------------------------------------------------

---

- name: show hostname

  hosts: R1


  tasks:

    - name: show hostname

      ios_config: 

        lines: 

          - hostname router1   

            

      register: output

 

    - debug:

        var: output

      when: "'router1' in output.stdout[0]"

-------------------------------------------------


Stefan Hornburg (Racke)

unread,
Feb 16, 2021, 3:13:58 AM2/16/21
to ansible...@googlegroups.com
On 2/16/21 9:04 AM, Vikram S wrote:
> 1st playbook uses ios_command module and gets executed as intended (to display the output when hostname of device
> contains router1).
>
> However 2nd playbook uses ios_config module and fails when i use the 'when' conditional. Am i do something wrong or is
> it that 'when' conditional is not supposed to work with ios_config module?  (please ignore the formatting)
>

First of all output.stdout is not a list but a string.

Can you provide sample output?

Regards
Racke
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/8bb51686-7f9a-4c88-8148-b94063103649n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/8bb51686-7f9a-4c88-8148-b94063103649n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Vikram S

unread,
Feb 16, 2021, 3:42:00 AM2/16/21
to Ansible Project
Hi,

Thanks for your assistance. Please find output for both the playbooks as you requrested:

OUTPUT FOR 1ST PLAYBOOK (WORKING ONE):
TASK [debug] ***************************************************************************************************************************
ok: [R1] => {
    "output": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "deprecations": [
            {
                "msg": "Distribution Ubuntu 18.04 on host R1 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information",
                "version": "2.12"
            }
        ],
        "failed": false,
        "stdout": [
            "hostname router28"
        ],
        "stdout_lines": [
            [
                "hostname router28"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

OUTPUT FOR 2ND PLAYBOOK (NON-WORKING ONE):

PLAY [show hostname] *******************************************************************************************************************

TASK [configure hostname] **************************************************************************************************************
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host R1 should use /usr/bin/python3, but is using /usr/bin/python for backward
compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host.
removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [R1]

TASK [debug] ***************************************************************************************************************************
fatal: [R1]: FAILED! => {"msg": "The conditional check ''router11' in output.stdout[0]' failed. The error was: error while evaluating conditional ('router11' in output.stdout[0]): Unable to look up a name or access an attribute in template string ({% if 'router11' in output.stdout[0] %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like '-': argument of type 'AnsibleUndefined' is not iterable\n\nThe error appears to be in '/root/sample8.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n"}

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

root@NetworkAutomation-2:~#

Stefan Hornburg (Racke)

unread,
Feb 16, 2021, 3:11:08 PM2/16/21
to ansible...@googlegroups.com
On 2/16/21 9:42 AM, Vikram S wrote:
> Hi,
>
> Thanks for your assistance. Please find output for both the playbooks as you requrested:

So if you have "hostname router28" in output.stdout, you can use the following when condition:

when: output.stdout is search('router28$')

This condition is true when the string "router28" is at the end of output.stdout.

Regards
Racke


>
> *OUTPUT FOR 1ST PLAYBOOK (WORKING ONE):*
> TASK [debug]
> ***************************************************************************************************************************
> ok: [R1] => {
>     "output": {
>         "ansible_facts": {
>             "discovered_interpreter_python": "/usr/bin/python"
>         },
>         "changed": false,
>         "deprecations": [
>             {
>                 "msg": "Distribution Ubuntu 18.04 on host R1 should use /usr/bin/python3, but is using /usr/bin/python
> for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered
> platform python for this host. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html
> for more information",
>                 "version": "2.12"
>             }
>         ],
>         *"failed": false,*
> *        "stdout": [*
> *            "hostname router28"*
> *        ],*
> *        "stdout_lines": [*
> *            [*
> *                "hostname router28"*
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> *OUTPUT FOR 2ND PLAYBOOK (NON-WORKING ONE):*
>
> PLAY [show hostname]
> *******************************************************************************************************************
>
> TASK [configure hostname]
> **************************************************************************************************************
> [DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host R1 should use /usr/bin/python3, but is using /usr/bin/python
> for backward
> compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python
> for this host.
>  See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This
> feature will be
> removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
> changed: [R1]
>
> *TASK [debug]
> ****************************************************************************************************************************
> *fatal: [R1]: FAILED! => {"msg": "The conditional check ''router11' in output.stdout[0]' failed. The error was: error
> while evaluating conditional ('router11' in output.stdout[0]): Unable to look up a name or access an attribute in
> template string ({% if 'router11' in output.stdout[0] %} True {% else %} False {% endif %}).\nMake sure your variable
> name does not contain invalid characters like '-': argument of type 'AnsibleUndefined' is not iterable\n\nThe error
> appears to be in '/root/sample8.yaml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact
> syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n"}*
> <https://groups.google.com/d/msgid/ansible-project/8bb51686-7f9a-4c88-8148-b94063103649n%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/8bb51686-7f9a-4c88-8148-b94063103649n%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/4bc08ea4-622a-4f27-b2cd-f92c588a5030n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/4bc08ea4-622a-4f27-b2cd-f92c588a5030n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature

Vikram S

unread,
Feb 20, 2021, 9:49:26 AM2/20/21
to Ansible Project
Hi Racke,

Thanks for the tip. But iam wondering if there is any restriction in ios_config module that my config line wouldn't work and your config is compatable with this module?

Regards,
Vikram

Reply all
Reply to author
Forward
0 new messages