In my Ansible playbook many times i need to create file there
- name: Copy file
template:
src: code.conf.j2
dest: "{{project_root}}/conf/code.conf"
now many times conf
dir is not there. Then I have to create more task to create that dir first.
Is there any easy way to auto create dir if don't exist with some option
If you are running Ansible >= 2.0 there is also the dirname filter you can use to extract the directory part of a path. That way you can just use one variable to hold the entire path to make sure both tasks never get out of sync.
So for example if you have playbook with
dest_path
defined in a variable like this you can reuse the same variable:AFAIK, the only way this could be done is by using the
state=directory
option. Whiletemplate
module supports most ofcopy
options, which in turn supports mostfile
options, you can not use something likestate=directory
with it. Moreover, it would be quite confusing (would it mean that{{project_root}}/conf/code.conf
is a directory ? or would it mean that{{project_root}}/conf/
should be created first.So I don't think this is possible right now without adding a previous
file
task.copy module creates the directory if it's not there. In this case it created the resolved.conf.d directory
you can create the folder using the following depending on your ansible version.
Latest version 2<
Older version:
Refer - http://docs.ansible.com/ansible/latest/file_module.html
According to the latest document when state is set to be directory, you don't need to use parameter recurse to create parent directories, file module will take care of it.
this is fare enough to create the parent directories data and test with foo
please refer the parameter description - "state" http://docs.ansible.com/ansible/latest/modules/file_module.html
To ensure success with a full path use recurse=yes