Ansible level 3 task 1 issue

Hi !

I’m having the following error in kk engineer: - it seems like you haven’t created the symbolic link to ‘/var/www/html’ properly on stapp01

But I mostly think I made it. This is my yml file:

---
- name: file creation
  hosts: all
  become: yes
  tasks:
    - name: firstFile
      ansible.builtin.file:
        path: /opt/devops/blog.txt
        state: touch
        owner: tony
        group: tony
      when: inventory_hostname == 'stapp01'

    - name: makeDir1
      ansible.builtin.file:
        path: /var/www/html
        state: directory
        owner: tony
        group: tony
        mode: "0777"
      when: inventory_hostname == 'stapp01'
    
    - name: firstSymLink
      ansible.builtin.file:
        src: /opt/devops/blog.txt
        dest: /var/www/html/blog.txt
        state: link
        owner: tony
        group: tony
      when: inventory_hostname == 'stapp01'

    - name: secondFile
      ansible.builtin.file:
        path: /opt/devops/story.txt
        state: touch
        owner: steve
        group: steve
      when: inventory_hostname == 'stapp02'


    - name: makeDir2
      ansible.builtin.file:
        path: /var/www/html
        state: directory
        owner: steve
        group: steve
        mode: "0777"
      when: inventory_hostname == 'stapp02'

    - name: secondSymLink
      ansible.builtin.file:
        src: /opt/devops/story.txt
        dest: /var/www/html/story.txt
        state: link
        owner: steve
        group: steve
      when: inventory_hostname == 'stapp02'

    - name: thirdFile
      ansible.builtin.file:
        path: /opt/devops/media.txt
        state: touch
        owner: banner
        group: banner
      when: inventory_hostname == 'stapp03'


    - name: makeDir3
      ansible.builtin.file:
        path: /var/www/html
        state: directory
        owner: banner
        group: banner
        mode: "0777"
      when: inventory_hostname == 'stapp03'

    - name: thirdSymLink
      ansible.builtin.file:
        src: /opt/devops/media.txt
        dest: /var/www/html/media.txt
        state: link
        owner: banner
        group: banner
      when: inventory_hostname == 'stapp03'

I’m able to create the folders and make the symbolic links. But by some unknown reason the lab fails with the previous reason. Can you explain what is the issue ? I think the lab doesn’t explain it well enough to be honest.

The 0777 permissions are because of being trying with different modes before, but I think the permissions of the file are not the issue.

Thanks in advance!

I’ve just checked it with the answer below, and I was able to pass the task.

---
- name: Create files and symbolic links on app servers
  hosts: all
  become: true

  vars:
    app_config:
      stapp01:
        filename: blog.txt
        owner: tony
      stapp02:
        filename: story.txt
        owner: steve
      stapp03:
        filename: media.txt
        owner: banner

  tasks:
    - name: Create the sysops directory
      ansible.builtin.file:
        path: /opt/sysops
        state: directory
        owner: "{{ app_config[inventory_hostname].owner }}"
        group: "{{ app_config[inventory_hostname].owner }}"
        mode: "0755"

    - name: Create the required empty file
      ansible.builtin.file:
        path: "/opt/sysops/{{ app_config[inventory_hostname].filename }}"
        state: touch
        owner: "{{ app_config[inventory_hostname].owner }}"
        group: "{{ app_config[inventory_hostname].owner }}"
        mode: "0644"

    - name: Remove the existing HTML directory
      ansible.builtin.file:
        path: /var/www/html
        state: absent

    - name: Create symbolic link to the sysops directory
      ansible.builtin.file:
        src: /opt/sysops
        dest: /var/www/html
        state: link

1 Like

thanks @raymond.baoly . May I ask why it is needed to delete the /var/www/html folder first to solve this task ?

@carlos140

No, you don’t need to do that.