# When conditional does not work with ios\_config module, please help

**URL:** <https://forum.ansible.com/t/when-conditional-does-not-work-with-ios-config-module-please-help/34958>\
**Category:** Ansible Project\
**Created:** [April 11, 2021, 1:25pm UTC](https://forum.ansible.com/t/when-conditional-does-not-work-with-ios-config-module-please-help/34958 "2021-04-11T13:25:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikram\_S](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vikram\_S](https://forum.ansible.com/u/Vikram_S)\
**Post date:** [April 11, 2021, 1:25pm UTC](https://forum.ansible.com/t/when-conditional-does-not-work-with-ios-config-module-please-help/34958/1 "2021-04-11T13:25:33Z")

</div>

Hi,

I have written the below script to check if packages.conf has been updated (by checking it’s date) in flash and if so, to set it as boot statement for switch. But ansible does not seem to consider the when conditional at all. It sets the boot statement regardless of whether the condition matches or not.  
But below ‘when’ syntax works for ios\_command module. Should i modify the syntax or am i writing it wrong?

- name: GATHER PACKAGES.CONF FILE INFO IN FLASH  
ios\_command:  
commands: “show flash: | i packages.conf”

register: show\_flash\_packages

- name: ADD PACKAGES.CONF AS BOOT STATEMENT WHEN THIS FILE IS UPDATED  
ios\_config:  
lines:
- boot system flash:packages.conf  
when:
- “‘Apr 09 2021’ in show\_flash\_packages.stdout[0] or  
‘Apr 10 2021’ in show\_flash\_packages.stdout[0]”

Thanks,  
Vikram

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 11, 2021, 1:32pm UTC](https://forum.ansible.com/t/when-conditional-does-not-work-with-ios-config-module-please-help/34958/2 "2021-04-11T13:32:54Z")

</div>

> Hi,
> 
> I have written the below script to check if packages.conf has been updated (by checking it's date) in flash and if so,  
> to set it as boot statement for switch. But ansible does not seem to consider the when conditional at all. It sets the  
> boot statement regardless of whether the condition matches or not.  
> But below 'when' syntax works for ios\_command module. Should i modify the syntax or am i writing it wrong?
> 
> - name: GATHER PACKAGES.CONF FILE INFO IN FLASH  
> ios\_command:  
> commands: "show flash: | i packages.conf"
> 
> ```
> register: show\_flash\_packages
> 
> \- name: ADD PACKAGES\.CONF AS BOOT STATEMENT WHEN THIS FILE IS UPDATED
> ios\_config:
> lines:
> \- boot system flash:packages\.conf
> when:  
> \- &quot;&#39;Apr 09 2021&#39; in show\_flash\_packages\.stdout\[0\] or
> &#39;Apr 10 2021&#39; in show\_flash\_packages\.stdout\[0\]&quot;
> 
> ```

Looks like you confused stdout\_lines with stdout.

stdout is a string and so stdout[0] is the first character of stdout.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Vikram\_S](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vikram\_S](https://forum.ansible.com/u/Vikram_S)\
**Post date:** [April 11, 2021, 1:40pm UTC](https://forum.ansible.com/t/when-conditional-does-not-work-with-ios-config-module-please-help/34958/3 "2021-04-11T13:40:09Z")

</div>

Hi Racke,

I have been told the same thing earlier as you said now and i have also tested. When i give “‘Apr 09 2021’ in show\_flash\_packages.stdout[0]”, it seems to check the entire string and not just the first character.  
I give the exact when syntax with ios\_command module and other modules and it works and it checks the entire string. Porblem with ios\_config module is that it doesn’t even seem to check the 1st character, but it simply seems to ignore the when conditional itself.

Are you saying that if i were to give it as below, it should work? I will double check again anyways…

- name: ADD PACKAGES.CONF AS BOOT STATEMENT WHEN THIS FILE IS UPDATED  
ios\_config:  
lines:
- boot system flash:packages.conf  
when:
- “‘Apr 09 2021’ in show\_flash\_packages.stdout or  
‘Apr 10 2021’ in show\_flash\_packages.stdout”

Thanks,  
Vikram

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 11, 2021, 1:55pm UTC](https://forum.ansible.com/t/when-conditional-does-not-work-with-ios-config-module-please-help/34958/4 "2021-04-11T13:55:38Z")

</div>

> Hi Racke,
> 
> I have been told the same thing earlier as you said now and i have also

tested. When i give "'Apr 09 2021' in

> show\_flash\_packages.stdout[0]", it seems to check the entire string and

not just the first character.

> I give the exact when syntax with ios\_command module and other modules and it works and it checks the entire string.  
> Porblem with ios\_config module is that it doesn't even seem to check the 1st character, but it simply seems to ignore  
> the when conditional itself.
> 
> Are you saying that if i were to give it as below, it should work? I will double check again anyways.....
> 
> - name: ADD PACKAGES.CONF AS BOOT STATEMENT WHEN THIS FILE IS UPDATED  
> ios\_config:  
> lines:  
> - boot system flash:packages.conf  
> when:  
> - "'Apr 09 2021' in show\_flash\_packages.stdout or  
> 'Apr 10 2021' in show\_flash\_packages.stdout"
> 
> Thanks,  
> Vikram

Can't be sure that this works as you didn't show the actual contents of show\_flash\_packages.stdout and especially where  
it comes from.

So please include sufficient information in your next posts.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

> &nbsp;&nbsp;&nbsp;&nbsp;\> Hi,  
> &nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\> I have written the below script to check if packages.conf has been updated (by checking it's date) in flash and if  
> &nbsp;&nbsp;&nbsp;&nbsp;so,  
> &nbsp;&nbsp;&nbsp;&nbsp;\> to set it as boot statement for switch. But ansible does not seem

to consider the when conditional at all. It sets

> &nbsp;&nbsp;&nbsp;&nbsp;the  
> &nbsp;&nbsp;&nbsp;&nbsp;\> boot statement regardless of whether the condition matches or not.  
> &nbsp;&nbsp;&nbsp;&nbsp;\> But below 'when' syntax works for ios\_command module. Should i modify the syntax or am i writing it wrong?  
> &nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\> - name: GATHER PACKAGES.CONF FILE INFO IN FLASH  
> &nbsp;&nbsp;&nbsp;&nbsp;\> ios\_command:  
> &nbsp;&nbsp;&nbsp;&nbsp;\> commands: "show flash: | i packages.conf"  
> &nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\> register: show\_flash\_packages  
> &nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\> - name: ADD PACKAGES.CONF AS BOOT STATEMENT WHEN THIS FILE IS UPDATED  
> &nbsp;&nbsp;&nbsp;&nbsp;\> ios\_config:  
> &nbsp;&nbsp;&nbsp;&nbsp;\> lines:  
> &nbsp;&nbsp;&nbsp;&nbsp;\> - boot system flash:packages.conf  
> &nbsp;&nbsp;&nbsp;&nbsp;\> when:  
> &nbsp;&nbsp;&nbsp;&nbsp;\> - "'Apr 09 2021' in show\_flash\_packages.stdout[0] or  
> &nbsp;&nbsp;&nbsp;&nbsp;\> 'Apr 10 2021' in show\_flash\_packages.stdout[0]"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Looks like you confused stdout\_lines with stdout.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;stdout is a string and so stdout[0] is the first character of stdout.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Regards  
> &nbsp;&nbsp;&nbsp;&nbsp;Racke
> 
> &nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\> Thanks,  
> &nbsp;&nbsp;&nbsp;&nbsp;\> Vikram  
> &nbsp;&nbsp;&nbsp;&nbsp;\>  
> &nbsp;&nbsp;&nbsp;&nbsp;\> --  
> &nbsp;&nbsp;&nbsp;&nbsp;\> You received this message because you are subscribed to the Google Groups "Ansible Project" group.  
> &nbsp;&nbsp;&nbsp;&nbsp;\> To unsubscribe from this group and stop receiving emails from it,

send an email to
