简体   繁体   中英

Ansible playbook: print a message

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM