2

I have a list of users that I want to delete (del_users), but I only want to delete those that haven't mountpoint on your /home directory. My currently code is:

- name: Verify if exist mount point on /home
  shell: "mount -l | grep bind | grep -ci {{ item }}"
  ignore_errors: yes
  check_mode: False
  loop: "{{ del_users }}"
  register: del_users_reg

- name: Delete user if doesn't exist mount point on /home
  user:
    name: "{{ item }}"
    state: absent
    remove: yes
  when: item.stdout_lines == 0
  loop: del_users_reg

When I execute this task appears the next error:

TASK [osbasic : Verify if exist mount point on /home] *******************************************************************************************************************************************************************************************
 [WARNING]: Consider using the mount module rather than running 'mount'.  If you need to use command because mount is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid
of this message.

failed: [LGAUSUX10] (item=user1) => {"changed": true, "cmd": "mount -l | grep bind | grep -ci usuario1", "delta": "0:00:00.025089", "end": "2019-06-11 13:33:40.978563", "item": "user1", "msg": "non-zero return code", "rc": 1, "start": "2019-06-11 13:33:40.953474", "stderr": "", "stderr_lines": [], "stdout": "0", "stdout_lines": ["0"]}

TASK [osbasic : Verify if exist mount point on /home] *******************************************************************************************************************************************************************************************
...ignoring

TASK [osbasic : Delete user if doesn't exist mount point on /home] **********************************************************************************************************************************************************************************

TASK [osbasic : Delete user if doesn't exist mount point on /home] **********************************************************************************************************************************************************************************
fatal: [LGAUSUX10]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: del_users_reg. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."}
        to retry, use: --limit @/etc/ansible/osbasic.retry

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
LGAUSUX10                  : ok=9    changed=1    unreachable=0    failed=1

How could put a condition for delete users only if the result of first task is 0?

2

Several problems in your playbook.

  1. loop is basically expecting a list, not a string containing a variable name. You need to expand your variable with a jinja2 expression => loop: "{{ some_list_variable }}"
  2. It will be hard to give you the correct syntax without seeing the exact data structure of your result var but... Typically, the register from a loop contains a results list of hashes, each containing info about the ran command for each loop iteration. This is your base loop for the second task => loop: "{{ del_users_reg.results }}"

If you still have trouble fixing your playbook, update your question with a debug of the registered var and I will update my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.