Limitations of the file module

Hi,

right now the file module does not create files, if they do not exist. But in contrast to that it creates directories, if they do not exist. I personally think this is inconsistent.

And there are good reasons to create files, if they do not exist, e.g. for syslogd on FreeBSD, if you want to use /var/log/console.log for console.info, you have to create that file and it needs certain file permissions (mode ‘600’). Right now you have to use the copy module to create the file without content, with correct permissions and force=no, so you won’t delete content in subsequent playbook runs. If you want to ensure correct file permission in subsequent playbook runs, you have to additionally use the file module. In comparison Puppet handles this with just one declaration.

The file module could handle the whole situation alone, if it would create files. Maybe a parameter ‘create’ could be added, so you could decide whether you want this behavior or not.

Regards,
Nils

p.s. beside this little issue I really like Ansible a lot :slight_smile:

Could you open a github issue for this? Thanks!

It should be easy to make “file: path=/tofile state=present” the equivalent of “touch /tofile”, I’ll put it on my queue unless someone beats me to it.

You can already get this behavior by using the copy module with “force=no” and either a blank file as source or content=no.

But I would support there being a state=touch

I would not support having state=present make the file present as I think it’s useful to have it error out when the file is not there in many cases and would be a bit hesitant to break compatibility.

I added an issue on github (https://github.com/ansible/ansible/issues/4097) and really like to have state=(touch|present) or equivalent.

I agree with mdehaan and prefer state=touch, it makes it more clear about what it does. In the event that the file exists, we could also have it replicate what touch does via the python os library using os.utime().

I updated the github issue to add state=touch.

The above was just merged in, thanks to René Moser (@resmo) for submitting it!