'Cannot log Ansible output if log_path doesn’t exist

I have an ansible.cfg file that has

log_path=logdir/install.log

Where, the logdir is a new directory which doesn’t exist. I created this logdir in the Ansible playbook as below:

- name: installation starts
  hosts: localhost
  gather_facts: yes
  tasks:
    - name: log directory creation if doesn’t exist
      file:
        path: logdir
        state: directory
        mode: 0755
    - name: other tasks
      ………………………………………
      ………………………………………

Here, when this playbook is run, logdir is created but the ansible-playbook output isn’t recorded inside the install.log during the first run.

But at the second run, the logs are recorded in the logdir/install.log file.

What could be the reason and how can I get the first logs in the install.log file?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source