Just had one more question. Is there a way to stop an Ansible playbook or set of lines from executing if a variable matches a specific text? I saw the Ansible conditionals documentation, however it doesn’t provide good examples on how to achieve this with a “when:” statement nor does it tell where would be best to insert these conditionals to prevent the specific lines from executing.
Just had one more question. Is there a way to stop an Ansible playbook or
set of lines from executing if a variable matches a specific text? I saw
the Ansible conditionals documentation
<http://docs.ansible.com/ansible/latest/playbooks_conditionals.html>,
however it doesn't provide good examples on how to achieve this with a
"when:" statement nor does it tell where would be best to insert these
conditionals to prevent the specific lines from executing.
Here's my script
---
- hosts: all
become: yes
vars_prompt:
- name: "new_user"
prompt: "What is the name of the new user?"
when: new_user != "root"
vars_prompt doesn't support when, only task does.
tasks:
- name: some function
command: .....
- name: another function
command: .....
Do I tack the "when:" conditional on at the end of the vars_prompt: or
after each "command:" function below that to stop the commands from being
run?
You would need it on each task.
But you can group task together with block: and set the when on the block. Then Ansible will add when on all the task
Or put all the task in a file, and use include: <file> and add the when to the include, here too Ansible will add when to each task.
I don’t see too many examples where a variable is invoked by when: so, I’m not sure if the syntax somehow is wrong. I tried specifying it as a list as well, but that had no effect:
I don't see too many examples where a variable is invoked by when: so, I'm
not sure if the syntax somehow is wrong. I tried specifying it as a list as
well, but that had no effect:
Most when is using variables, and for the Ansible docs about conditional you posted has several (actually all of them has at least one variable)
But you error messages indicate wrong indentation of when.
This would be easier if you showed your code and not just the error, but if you look at the link you posted you'll see when is indentation must be at the same level as the module name.