unable to create directory using ansible file module

10 views
Skip to first unread message

Shifa Shaikh

unread,
Mar 16, 2020, 3:20:21 AM3/16/20
to Ansible Project
Here is my variable file:

cat vars.yml
---

  layers
:
   
- name: APP
  layers
:
   
- name: WLS


Here is my playbook

cat varloop.yml
---

    hosts
: localhost
    vars_files
:
     
- vars.yml



    tasks
:
     
- name: Create sublinks based of bad.txt files
        include_tasks
: "{{ playbook_dir }}/inner.yml"
        loop
: "{{ layers }}"
        loop_control
:
          loop_var
: my_result

inner.yml
- name: Generate JSON data
  block
:
   
- file: path="{{ playbook_dir }}/dashboard/{{ my_result.name }} state=directory recurse=yes"


However, I'm getting the following error running the code:

TASK [file] *************************************************************************************************************************************************
fatal
: [localhost]: FAILED! => {"changed": false, "msg": "file (/tmp/dashboard/APP state=directory recurse=yes) is absent, cannot continue", "path": "/tmp/dashboard/APP state=directory recurse=yes"}

I was expecting the play to create the directory "/tmp/dashboard/APP", however it errors and fails.  Can you please suggest ? 

Dick Visser

unread,
Mar 16, 2020, 3:30:24 AM3/16/20
to ansible...@googlegroups.com
Hi

you've missed a few basic things. The vars file contains a duplicate
variable layers - that won't work.
Also, you appear to want to loop over the variable layers, but you
don't actually use it.
Have a look at this URL, it has some nice examples of how loops work:

https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#iterating-over-a-simple-list
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0fe8246b-1d71-44c4-baa0-6185815366a1%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Shifa Shaikh

unread,
Mar 16, 2020, 3:40:55 AM3/16/20
to Ansible Project
Dick Hi, The issue is not with the loop as I can see the correct value populated using debug. 

Aslo, the correct value shows up in the error output from the loop ... see below:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "file (/tmp/dashboard/APP state=directory recurse=yes) is absent, cannot continue", "path": "/tmp/dashboard/APP state=directory recurse=yes"}

/tmp/dashboard/APP is indeed the correct desired directory that should be created.

Please suggest.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Dick Visser

unread,
Mar 16, 2020, 3:57:14 AM3/16/20
to ansible...@googlegroups.com
Ah OK yes, i see, you're right.
The issue is your quoting, as indicated by the error message.
Instead of:

- file: path="{{ playbook_dir }}/dashboard/{{ my_result.name }}
state=directory recurse=yes"

Do this:

- file: path="{{ playbook_dir }}/dashboard/{{ my_result.name }}"
state=directory recurse=yes


> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/42e9abb0-78c1-4f0e-940a-a185f1a34ba6%40googlegroups.com.

Vivek Kothawale

unread,
Mar 16, 2020, 3:57:29 AM3/16/20
to ansible...@googlegroups.com
Hi shifa,

Can you please check whether dashboard dir is present on remote server?

Thanks,
Vivek

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/42e9abb0-78c1-4f0e-940a-a185f1a34ba6%40googlegroups.com.

Shifa Shaikh

unread,
Mar 16, 2020, 4:44:57 AM3/16/20
to Ansible Project
@Dick your suggestion helped !! it works. 
Reply all
Reply to author
Forward
0 new messages