Answers: <p>Several problems in your playbook.</p>
  
  <ol>
  <li><code>loop</code> is basically expecting a list, not a string containing a variable name. You need to expand your variable with a jinja2 expression => <code>loop: "{{ some_list_variable }}"</code></li>
  <li>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 <code>results</code> list of hashes, each containing info about the ran command for each loop iteration. This is your base loop for the second task => <code>loop: "{{ del_users_reg.results }}"</code></li>
  </ol>
  
  <p>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.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-06-13T14:15:18.867Z
Answers: 2019-11-01T20:52:42.207Z
Answers: 2019-11-01T20:52:42.207Z
Answers: Zeitounator
Body: <p>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:</p>
  
  <pre><code>- 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
  </code></pre>
  
  <p>When I execute this task appears the next error:</p>
  
  <pre><code>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) =&gt; {"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! =&gt; {"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
  </code></pre>
  
  <p>How could put a condition for delete users only if the result of first task is 0?</p>
  
Tags: <ansible>
Title: Task to delete users without mount points in /home (using register and when)
UserDisplayName: David
ThreadText: # Task to delete users without mount points in /home (using register and when)
  
  (Asked by: username_0 on 2019-06-13)
  
  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?
  
  **Tags:** ansible
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-06-13. Score: 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.
  
  
  ---
  
  