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"}*
> 
> PLAY RECAP
> *****************************************************************************************************************************
> R1                         : ok=1    changed=1    unreachable=0    failed=1   
>  skipped=0    rescued=0    ignored=0
> 
> root@NetworkAutomation-2:~#
> On Tuesday, 16 February, 2021 at 1:43:58 pm UTC+5:30 [email protected] wrote:
> 
>     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
> 
>     > ---
>     >
>     > - 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]"
>     >
>     > -------------------------------------------------
>     >
>     >
>     > --
>     > 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
>     > [email protected] 
> <mailto:[email protected]>.
>     > 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>
>     >
>     
> <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
> [email protected] 
> <mailto:[email protected]>.
> 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>.


-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/f108a792-e01c-d058-bcf9-ba2a4055d890%40linuxia.de.

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to