Search code examples
ansibleansible-2.xansible-inventoryansible-facts

can't use the ansible extra vars values in playbook task


When running the ansible playbook, i am providing the extra vars especially for modules variable where i am passing multiple values.

ansible-playbook abc.yml  --extra-vars "status=enable modules='redis apache zookeeper'"

I want to run the below task when the modules in extra vars has "redis"

  - name: fetch ports
    shell: ll | grep -i "ports"
    when:  modules == "redis"

However, the above task is getting skipped even when the module in extra vars has "redis". Any help would be appreciated.


Solution

  • the modules variable you supplied is a string, you have 2 options:

    1. try to search for "redis" in the supplied string, not the ideal option because it would validate to true if you had supplied string like redisv2 apache zookeeper.
    2. split the string to a list using the space as a delimiter, and then validate to true if the "redis" is in one of the list elements.

    for the 2nd option, here is how to do it:

      - name: fetch ports
        shell: ll | grep -i "ports"
        when: '"redis" in modules.split(" ")'
    

    hope it helps.