Issue with 'when' conditional

48 views
Skip to first unread message

Dave Florek

unread,
Sep 22, 2017, 2:19:31 PM9/22/17
to Ansible Project
Hi,

I keep getting errors related to my 'when' conditionals in my following playbook:

---
- hosts: all

  become: yes

  vars_prompt:
   - name: "remove_user"
     prompt: "Type name of user to delete:"
     private: no
#     when: remove_user != "root"

  tasks:

   - name: Create new user account
     command: "userdel {{ remove_user }}"
        when: "{{remove_user}}" != "root"

Ansible prompts me that my not equals indicator needs to be in quotes but I know that's wrong. I'm trying to add a check to where the playbook won't execute if the root account is specified. Any ideas?

Brian Coca

unread,
Sep 22, 2017, 7:14:11 PM9/22/17
to Ansible Project
conditionals apply to tasks, not vars_prompt, which is not a task.

--
----------
Brian Coca

Dave Florek

unread,
Sep 25, 2017, 12:22:11 PM9/25/17
to Ansible Project
Hi Brian,

Thanks for the reply. After error testing, I commented out the conditional in vars_prompt and reinserted it right after the task command, however it's not liking the logic in my when-statement. Do you happen to know where I'm running into an error? I'm trying to add an exception to prevent the script from running if a user specifies the wrong account to delete such as 'root'.

I took out the quotes and brackets from the remove_user variable, but after running the playbook kicked back this message:

ERROR! Syntax Error while loading YAML.

The offending line appears to be:

     command: "userdel {{ user_to_remove }}"
       when: user_to_remove != "root"
       ^ here

Brian Coca

unread,
Sep 25, 2017, 12:34:52 PM9/25/17
to Ansible Project
when is a 'task' keyword, there you are making it an option for the
command module, unindent.


- command: "userdel {{ user_to_remove }}"
when: user_to_remove != "root"


also i recommend using the 'user' module instead.



--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages