Task Failed: Using Ansible Conditionals (when)

My following task failed. Not sure what went wrong. Looks like everything is good :slight_smile:

Thank you

Please find the playbook.yaml text

[email protected]_host ~/ansible$ cat << EOF > "playbook.yml"
> ---
> - name: Copy file with owner and permissions
>   hosts: all
>   become: yes
>   tasks:
>   - name: "stapp01"
>     copy:
>         src: /usr/src/sysops/blog.txt
>         dest: /opt/sysops/blog.txt
>         owner: "{{ ansible_user }}"
>         group: "{{ ansible_user }}"
>         mode: '0655'
>     when:  inventory_hostname   in "stapp01"
>
>   - name: "stapp02"
>     copy:
>       src: /usr/src/sysops/story.txt
>       dest: /opt/sysops/story.txt
>       owner: "{{ ansible_user }}"
>       group: "{{ ansible_user }}"
>       mode: '0655'
>     when:   inventory_hostname  == "stapp02"
>
>   - name: "stapp03"
>     become: true
>     copy:
>       src: /usr/src/sysops/media.txt
>       dest: /opt/sysops/media.txt
>       owner: "{{ ansible_user }}"
>       group: "{{ ansible_user }}"
>       mode: '0655'
>     when: inventory_hostname == "stapp03"
> EOF

did you create dir beforehand ?

@mehulr
The mistake is the first task β€œstapp01”
when: inventory_hostname in " stapp01"

Replace in with == as you did in the other tasks

the task created file without explicit need to create directory , may be because of " become: yes " in the top.

Thank you @andrzej for reply

Thank you @Devops for reply,
Good point,
i do not have access to the window right now.
As far as I remember it did not give any error to me and remember ansible playbook messages were similar for stapp03,stapp02 and stapp01 …

My bad i only checked node stapp03 to confirm if the file was created (and it was crated) . DId not checked for node stapp02 and stapp01 :frowning:

Thank you