Im posting my stack overflow question as it is much more clear:

Ask Question <https://stackoverflow.com/questions/ask>
up vote0down votefavorite 
<https://stackoverflow.com/questions/44838421/ansible-multiple-an-or-conditionals-in-when-clause/44839565?noredirect=1#>

I am having issues when trying to use multiple and/or conditionals in a 
when statement to decide whether a task needs to be ran or not. Basically I 
am making a playbook to do automated system patching with options for 
security patches, kernel only patches and to specify packages in a var file.

I run the playbook with the following commands and define the variables 
through extended variables option (-e)

ansible-playbook site.yml -i inventory --ask-vault -u (username)-e 
"security=true restart=true" -k -K 

By default the playbook will update every package on the system except 
kernel but I would like to skip that action if I specify any of a few 
variables. The code I have is the following:

- name: Update all packages yum: name: "*" state: latest exclude: "kernel*" 
when: security is not defined or kernel is not defined or 
specified_packages is not defined and ansible_os_family == "RedHat" 

Ive tried all of the following combinations:

when: (ansible_os_family == "RedHat") and (security is defined or kernel is 
defined or specified_packages is defined)

when: (ansible_os_family == "RedHat") and (security == true or kernel == 
true or specified_packages == true ) <- this case throws a not defined 
error because i don't define all variables every time i run the playbook

when: ansible_os_family == "RedHat" when: security is defined or kernel is 
defined or specified_packages is defined

*Note:* I am aware and have used an extra variable such as "skip" to skip 
this task and use the when clause when: ansible_os_family == "RedHat" and 
skip is not defined but would prefer not have my users need to use an extra 
variable just to skip this default action.

I also am not using tags as I am gathering a list of packages before and 
after the upgrade to compare and report in the end so I wont be able to run 
those as they are local action commands. This is why I'm using one role 
with multiple tasks turned on and off via extended variables. I am open to 
any suggestion that rewrites the playbook in a more efficient way as I am 
sort of a noob.
On Thursday, June 29, 2017 at 5:34:16 PM UTC-4, ajay jiwanand wrote:
>
> I am having issues with using multiple conditions in when to validate 
> whether or not to run a task.
>
> Basically I am using extrended variables within the ansible command to 
> state what sort of updates I want to run like this:
> ansible-playbook site.yml -i inventory --ask-vault -u (username)-e 
> "security=true restart=true" -k -K
>
> - name: Update all packages
>   yum:
>      name: "*"
>      state: latest
>      exclude: "kernel*"
>   when: security is not defined or kernel is not defined  or 
> specified_packages is not defined and ansible_os_family == "RedHat"  
>
> However I can not figure out a combination to get this conditional to run 
> properly. Everytime I run it the playbook continues to run the task when I 
> dont want it to. I already experimented with adding another variable to 
> specify if I want to skip this task only but I would prefer it to 
> automatically get skipped when I specify to run another type of update.
>
> I've tried the following:
>
>  (security is not defined or kernel is not defined  or specified_packages 
> is not defined) and (ansible_os_family == "RedHat")
>  (security is not defined or kernel is not defined  or specified_packages 
> is not defined) and ansible_os_family == "RedHat"
>
> or even:
>
> when: ansible_os_family == "RedHat"
> when: security is not defined or kernel is not defined  or 
> specified_packages is not defined
>

-- 
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/2feab920-5071-4f80-907e-e7a9fdb22c8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to