On Thu, 5 Mar 2020 22:09:49 -0800 (PST)
דודו דודו <[email protected]> wrote:

> I have a task that create a folder on HDFS storage. 
> If the folder exists I wish to show a message
> 
> - 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 

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)

   tasks:
    - name: create folder in HDFS
      command: hadoop dfs -mkdir /tmp/dudu
      ignore_errors: yes
      register: result
    - fail:
        msg: folder exists 
      when: result is failed

2) Show a message and continue

   tasks:
    - name: create folder in HDFS
      command: hadoop dfs -mkdir /tmp/dudu
      ignore_errors: yes
      register: result
    - debug:
        msg: folder exists 
      when: result is failed

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

   tasks:
    - name: create folder in HDFS
      command:
        cmd: hadoop dfs -mkdir /tmp/dudu
        creates: /tmp/dudu
      ignore_errors: yes
      register: result
    - debug:
        msg: "{{ result.stdout }}"
      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

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

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

quoting from "command – Execute commands on targets"
https://docs.ansible.com/ansible/latest/modules/command_module.html#notes

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

HTH,

        -vlado

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200306075453.2ee45c34%40gmail.com.

Attachment: pgp20UUgWCW0l.pgp
Description: OpenPGP digital signature

Reply via email to