Re: [ansible-project] Remove and create directory in a single task is failing

10 views
Skip to first unread message
Message has been deleted

Dick Visser

unread,
Dec 21, 2018, 7:17:18 AM12/21/18
to ansible...@googlegroups.com
This is the same question as you asked in an earlier thread - to which
you got two answers.
Without any feedback on how that worked out for you (did you actually
try them? did they work?), you are now trying almost the same solution
again, which is failing in a similar way.
Instead of shoehorning what you think should work and ignoring the
provided answers, can you please read them and try them out?

In any case, removing and recreating directories in a single task
sounds like a bad thing to do to begin with.

To give some hint, in order of elegance, the options are:

1. find and register all files/dirs, and then delete those
2. use a shell task to "rm -rf" the contents


Dick


On Fri, 21 Dec 2018 at 11:55, <anushake...@gmail.com> wrote:
>
> Hi Team,
>
> I am trying to remove and create the directory in single task but it is failing. Could someone please help.
>
> vars/main.yml
>
> SAMPLE:
> - { src: '/tmp/abc.war', dest: '/tmp/test/' }
> - { src: '/tmp/def.war', dest: 'tmp/test/' }
> - { src: '/tmp/xyz.war', dest: '/tmp/test/' }
>
> Playbook.yml
> ---
> - hosts: all
> tasks:
> - name: Ansible remove and create directory
> file:
> path: "{{ item.dest }}"
> state: absent
> with_items:
> - { "SAMPLE", state: absent }
> - { "SAMPLE", state: directory }
>
>
> ERROR:
>
> {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'path'\n\nThe error appears to have been in '/etc/ansible/test/tasks/main.yml': line 23, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: ansible create directory example\n ^ here\n"}
>
>
> --
> 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 post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/60202e37-9d08-4655-bd28-1aef955d4cd8%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Dec 21, 2018, 7:26:33 AM12/21/18
to ansible...@googlegroups.com
On Friday, 21 December 2018 11:55:19 CET anushake...@gmail.com wrote:
> Hi Team,
>
> I am trying to remove and create the directory in single task but it is
> failing. Could someone please help.
>
> vars/main.yml
>
> SAMPLE:
> - { src: '/tmp/abc.war', dest: '/tmp/test/' }
> - { src: '/tmp/def.war', dest: 'tmp/test/' }
> - { src: '/tmp/xyz.war', dest: '/tmp/test/' }
>
> Playbook.yml
> ---
> - hosts: all
> tasks:
> - name: Ansible remove and create directory
> file:
> path: "{{ item.dest }}"
> state: absent
> with_items:
> - { "SAMPLE", state: absent }
> - { "SAMPLE", state: directory }

You need to use with_nested:
https://docs.ansible.com/ansible/2.4/playbooks_loops.html#nested-loops


--
Kai Stian Olstad


Reply all
Reply to author
Forward
0 new messages