Creating soft links using ansible

The Nautilus DevOps team is practicing some of the Ansible modules and creating and testing different Ansible playbooks to accomplish tasks. Recently they started testing an Ansible file module to create soft links on all app servers. Below you can find more details about it.
Create a playbook.yml under /home/thor/ansible on jump host; an inventory file is already present under /home/thor/ansible directory on jump host itself.

  1. Create an empty file /opt/security/blog.txt on app server 1; its user owner and group owner should be tony. Create a symbolic link of source path /opt/security to destination /var/www/html.
  2. Create an empty file /opt/security/story.txt on app server 2; its user owner and group owner should be steve. Create a symbolic link of source path /opt/security to destination /var/www/html.
  3. Create an empty file /opt/security/media.txt on app server 3; its user owner and group owner should be banner. Create a symbolic link of source path /opt/security to destination /var/www/html.
    Note: Validation will try to run playbook using command ansible-playbook -i inventory playbook.yml so please make sure playbook works this way, without passing any extra arguments.
    Update inventory file as below

Playbook file as below

  • name: Configure soft limk
    hosts: all
    become: yes
    become_user: root
    tasks:
    • name: Create blank file and add contents in app01
      copy:
      dest: “/opt/sysops/blog.txt”
      when: app1 in group_names
    • name: Create blank file and add contents in app02
      copy:
      dest: “/opt/sysops/story.txt”
      when: app2 in group_names
    • name: Create blank file and add contents in app03
      copy:
      dest: “/opt/sysops/media.txt”
      when: app3 in group_names
    • name: Change file ownership, group and permissions
      file:
      src: /opt/sysops
      dest:/var/www/html
      owner: tony
      group: tony
      state: link
      when: app1 in group_names
    • name: Change file ownership, group and permissions
      file:
      src: /opt/sysops
      dest:/var/www/html
      owner: steve
      group: steve
      state: link
      when: app2 in group_names
    • name: Change file ownership, group and permissions
      file:
      src: /opt/sysops
      dest:/var/www/html
      owner: banner
      group: banner
      state: link
      when: app3 in group_names

Above is my playbook.yml and inventory file

Getting below error
thor@jump_host ~/ansible$ ansible-playbook -i inventory playbook.yml
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: No JSON object could be decoded

Syntax Error while loading YAML.
could not find expected ‘:’

The error appears to be in ‘/home/thor/ansible/playbook.yml’: line 23, column 10, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

     dest:/var/www/html
     owner: tony
     ^ here

@andrzej @Tej-Singh-Rana @Lakshmi Please help

@Dhanabalan

You can try this way too

  • hosts: stapp01
    gather_facts: false
    become: true
    tasks:
    • name: Create empty file
      file:
      path: /opt/dba/blog.txt
      state: touch
      owner: tony
      group: tony
    • name: Create Symbolic link
      file:
      src: /opt/dba
      dest: /var/www/html
      state: link

try the same for stapp02 and stapp03.

1 Like

For dest: you have to give one space before you specify the path. That’s the reason it’s showing that line in the error.

Correct :white_check_mark: