Ansible playbook: print a message

Viewed 12850

Probably this has been asked before but can't find anything helpful yet.

I have this task:

 - name: Create folder if not exists
   win_file:
     path: '{{ folder }}'
     state: directory
   when: my_dir.stat.exists == false << this stat has been previously created

 - Debug: msg"folder already exists"

If I execute this the output in Ans. tower looks like this (the folder already exists):

TASK [playbook : Create folder if not exists] ***
17:51:00
23
skipping: [host]


TASK [playbook : debug]    ***************************************
18:16:07
26
ok: [host] => {
27
"msg": "Folder already exists"

Ik want this msg to be printed in the Create folder task and not in a separate task.

Help would be appreciated

1 Answers

I would suggest a different strategy:

- win_file:
  ...
  register: create

- debug:
    msg: Folder already exists
  when: create.changed == false

This might not work in your exact use case, but from the given example it would make sense. file/win_file already check themselves if a file/directory already exist.

Regarding your actual question: I think it is not possible to put two tasks (win_file and debug) into one. If your intention is to avoid writing the condition twice, you could use blocks: https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html

Related