Using Ansible Conditionals

Hello @Ayman @Inderpreet @kodekloud1 @Tej-Singh-Rana
The terminal says that the when condition was not used but it was obviously. The files were copied correctly too and i have proof

@Inderpreet @mmumshad @Ayman
Please check this out already. Thank you!

Hello, @Chudo
Have you check it data of the file? and How you applied when condition in the playbook. Can you write syntax?

@Tej-Singh-Rana **As you can see in the screenshots, the when statement was implemented and the files were copied. Below is the syntax. Cheers **

  • hosts: all
    become: yes
    gather_facts: yes
    tasks:
    • name: Copy file with owner and permissions
      copy:
      src: /usr/src/sysops/blog.txt
      dest: /opt/sysops/blog.txt
      owner: tony
      group: tony
      mode: ‘0655’
      when: inventory_hostname == “stapp01”

    • name: Copy file with owner and permissions
      copy:
      src: /usr/src/sysops/story.txt
      dest: /opt/sysops/story.txt
      owner: steve
      group: steve
      mode: ‘0655’
      when: inventory_hostname == “stapp02”

    • name: Copy file with owner and permissions
      copy:
      src: /usr/src/sysops/media.txt
      dest: /opt/sysops/media.txt
      owner: banner
      group: banner
      mode: ‘0655’
      when: inventory_hostname == “stapp03”

Do you have task image?

I knew ansible_hostname not sure for inventory_hostname. When you will run playbook it will pick the ansible_hostname from the facts and will do correct treatment as per written in the playbook.

@Tej-Singh-Rana
I dont but you can clearly see that the files were copied. The task said we CAN use ansible_hostname but didnt say we must. The screenshots show it was correctly executed and I checked each host for the presence of the files.
What condition did you use for ansible_hostname? cos it gave me errors

Fine, your treatment is correct if it’s not necessary to use ansible_hostname.
You can see details of the inventory_hostname and ansible_hostname uses.

1 Like

@Inderpreet @mmumshad @kodekloud-support3 hello, please some help here

@Chudo, did you already have today’s task in your bucket ? Please let me know once you have new task assigned for today

@rahul456
no yet. Please why are you asking?

@Chudo, this is marked pending for you, please give it an another try.

1 Like

thanks @rahul456 All good

Hey guys @Ayman @Inderpreet @kodekloud1 @Tej-Singh-Rana

I’m experiencing the same problem. I ran and confirmed that it worked yet if failed.

Error Message:
App Server 2 after running Ansible playbook '/home/thor/ansible/playbook.yml'

Here are my screenshots:

Everything checked out, yet it failed. Could you please look into this?

Thank you.

@dcmexy
I think story.txt not story.text :frowning:

Thanks @Tej-Singh-Rana. it’s a shame it’s a simple typo error but the rest of the playbook is correct. Can I have another go at it?