# Getting a message if register failed inside a task

**URL:** <https://forum.ansible.com/t/getting-a-message-if-register-failed-inside-a-task/32402>\
**Category:** Ansible Project\
**Created:** [March 6, 2020, 6:09am UTC](https://forum.ansible.com/t/getting-a-message-if-register-failed-inside-a-task/32402 "2020-03-06T06:09:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [March 6, 2020, 6:09am UTC](https://forum.ansible.com/t/getting-a-message-if-register-failed-inside-a-task/32402/1 "2020-03-06T06:09:49Z")

</div>

HI,

I have a task that create a folder on HDFS storage.  
If the folder exists I wish to show a message

```auto
- hosts: gw
  become: true
  become_user: hdfs
  tasks:
   - name: create folder in HDFS
     shell: hadoop dfs -mkdir /tmp/dudu
     ignore_errors: yes
     register: result
     when: result is failed
     fail:
       msg: folder exists 

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 6, 2020, 6:54am UTC](https://forum.ansible.com/t/getting-a-message-if-register-failed-inside-a-task/32402/2 "2020-03-06T06:54:53Z")

</div>

It's not possible to use two modules (shell and fail) in one task. There are  
couple of scenarios how to handle this case.

1) Show a message and fail (fatal)

&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: create folder in HDFS  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: hadoop dfs -mkdir /tmp/dudu  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;- fail:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: folder exists  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: result is failed

2) Show a message and continue

&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: create folder in HDFS  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: hadoop dfs -mkdir /tmp/dudu  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: folder exists  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: result is failed

3) Execute the command if the folder does not exist else skip the command,  
&nbsp;&nbsp;&nbsp;show a message and continue. Optionally change "debug" to "fail".

&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: create folder in HDFS  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd: hadoop dfs -mkdir /tmp/dudu  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;creates: /tmp/dudu  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ result.stdout }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: not result.changed

Next options would be to use "block". See "Error Handling In Playbooks"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_error\_handling.html#error-handling-in-playbooks](https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html#error-handling-in-playbooks)

As a sidenote, quoting from "shell – Execute shell commands on targets"  
[https://docs.ansible.com/ansible/latest/modules/shell\_module.html#notes](https://docs.ansible.com/ansible/latest/modules/shell_module.html#notes)

&nbsp;&nbsp;"If you want to execute a command securely and predictably, it may be  
&nbsp;&nbsp;better to use the command module instead. Best practices when writing  
&nbsp;&nbsp;playbooks will follow the trend of using command unless the shell module is  
&nbsp;&nbsp;explicitly required..."

quoting from "command – Execute commands on targets"  
[https://docs.ansible.com/ansible/latest/modules/command\_module.html#notes](https://docs.ansible.com/ansible/latest/modules/command_module.html#notes)

&nbsp;&nbsp;"If you want to run a command through the shell (say you are using \<, \>, |,  
&nbsp;&nbsp;etc), you actually want the shell module instead. Parsing shell  
&nbsp;&nbsp;metacharacters can lead to unexpected commands being executed if quoting is  
&nbsp;&nbsp;not done correctly so it is more secure to use the command module when  
&nbsp;&nbsp;possible."

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [March 6, 2020, 11:47am UTC](https://forum.ansible.com/t/getting-a-message-if-register-failed-inside-a-task/32402/3 "2020-03-06T11:47:09Z")

</div>

Thank you for your answer. So basically I need to run 2 tasks **BUT**

If I have a list of folders that needs to be created and i will use with\_items - As I know, the first task will run on all folders and the MSG task will only verify the register of the last folder.

Basically what I’m askingת is there a way to run 2 tasks (one by one) on the same item and not running each task in parallel for all items
