When conditional does not work with ios_config module, please help

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

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\]"

Looks like you confused stdout_lines with stdout.

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

Regards
          Racke

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

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
        Racke

    > 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]"

    Looks like you confused stdout_lines with stdout.

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

    Regards
    Racke

    >
    > Thanks,
    > Vikram
    >
    > --
    > 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