Crak
Crak

Reputation: 11

Create File on Target Server Using Ansible

I'm trying to create a file on the target server using Ansible.

I would like to get the path from the env variable on the target server.

The code is:

- name: Create a file 
  win_file: 
    path: ansible_env.CONFIG_HOME\conf\xyz.txt
    state: file

The error I'm receiving is:

"changed": false,

"msg": "path ansible_env.CONFIG_HOME\conf\xyz.txt will not be created"

Upvotes: 1

Views: 378

Answers (1)

costaparas
costaparas

Reputation: 5237

There are two issues here.

Firstly, since you are trying to use an environment variable, you need braces {{ }} to interpolate the value.

Secondly, have a close read of the docs for win_file where it talks about the way state behaves:

If file, the file will NOT be created if it does not exist

If touch, an empty file will be created if the path does not exist

Taking into consideration the above, the following should work for you:

- name: Create a file 
  win_file: 
    path: "{{ ansible_env.CONFIG_HOME }}\conf\xyz.txt"
    state: touch

Upvotes: 1

Related Questions