ansible - when condition not working with logical AND operator
Your second condition appears to be a string. I mean the whole condition. A string always is true.
"'Applying auth.0001_initial... OK' in django_migration_result.stdout"
In your last code block, the whole condition is in quotes. That would be a string on the yaml level and the reason why it then works.
This:
key: value
is the same as:
key: "value"
Writing your condition like this should do the trick:
when: django_migration_result|changed and ('Applying auth.0001_initial... OK' in django_migration_result.stdout)
Or even better:
when:
- django_migration_result | changed
- 'Applying auth.0001_initial... OK' in django_migration_result.stdout
import_playbook pass variable but it doesn't work in when condition
dynamic_hosts
is not a variable, it's an element in the list of groups
. You can debug that if you do a debug var=groups
You have to access it via groups.dynamic_hosts
.
- hosts: localhost
gather_facts: no
tasks:
- debug:
var: groups.dynamic_hosts
- name: PROMPT
block:
- name: ask host
pause:
prompt: "Please enter target IP"
register: target_host
- name: add to dynamic_hosts
add_host:
name: "{{ target_host.user_input }}"
groups: dynamic_hosts2
when: groups.dynamic_hosts is not defined
Ansible when variable == true not behaving as expected
You need to convert the variable to a boolean:
force_install|bool == true
I don't claim I understand the logic behind it. In python any non-empty string should be truthy. But when directly used in a condition it evaluates to false.
The bool filter then again interprets the strings 'yes'
, 'on'
, '1'
, 'true'
(case-insensitive) and 1
as true (see source). Any other string is false.
You might want to also set a default value in case force_install
is not defined, since it would result in an undefined variable error:
force_install|default(false)|bool == true
Ansible seems to be ignoring variable in when conditional when it is overridden with -e from the command line
The problem is caused by the fact that the type of the extra variables passed to Ansible from the command line is always string. For example, the playbook below
shell> cat pb.yml
- hosts: localhost
vars:
doomed: false
tasks:
- debug:
var: doomed|type_debug
- debug:
msg: We are doomed
when: doomed
works as expected. The type of the variable doomed is Boolean. Then, simply use the variable in the condition. No comparison is needed. The debug task will be skipped
shell> ansible-playbook pb.yml
PLAY [localhost] ******************************************************************************
TASK [debug] **********************************************************************************
ok: [localhost] =>
doomed|type_debug: bool
TASK [debug] **********************************************************************************
skipping: [localhost]
The thing will change when you pass the variable from the command line. Now, the type of the variable doomed is a string. A non-empty string will evaluate to True in the condition and the debug task will be executed
shell> ansible-playbook pb.yml -e doomed=false
PLAY [localhost] ******************************************************************************
TASK [debug] **********************************************************************************
ok: [localhost] =>
doomed|type_debug: str
TASK [debug] **********************************************************************************
ok: [localhost] =>
msg: We are doomed
There is a simple universal solution. Always cast such variables to bool. In addition to this, to simplify the code, default to false or true, depending on the use-case, instead of an explicit declaration. This way you write the code fast and the condition will always do what you want
shell> cat pb.yml
- hosts: localhost
tasks:
- debug:
msg: We are doomed
when: doomed|d(false)|bool
Related Topics
Why Can't I Sys_Write from a Pointer to Stack Memory, Using Int 0X80
Counting Number of Directories in a Specific Directory
Remote Linux Server to Remote Linux Server Dir Copy. How
How to Change All Occurrences of a Word in All Files in a Directory
What Is the *Nix Command to View a User's Default Login Shell
How to Query X11 Display Resolution
Grep Files Based on Time Stamp
Asp.Net Core Environment Variable Colon in Linux
How to Install Apxs Module on Apache 2.4.6
Manage Source Under Git and Svn Simultanously - Does It Make Sense
Print Field 'N' to End of Line
Use Find Command But Exclude Files in Two Directories
Linux Command Line Howto Accept Pairing for Bluetooth Device Without Pin
Http Debugging Proxy for Linux and MAC
Rm: Cannot Remove: Permission Denied