Ansible Level 3 --- ansible lineinfile module


Performed the task correctly but getting validation error.

playbook…


  • name: install and configure httpd
    hosts: all
    become: yes
    tasks:
    • name: Install httpd
      yum:
      name: httpd
      state: present
    • name: start httpd
      service:
      name: httpd
      state: started
    • name: remove pre-existing file
      file:
      path: /var/www/html/index.html
      state: absent
    • name: create file
      file:
      path: /var/www/html/index.html
      state: touch
      owner: apache
      group: apache
      mode: ‘0655’
    • name: add content
      lineinfile:
      path: /var/www/html/index.html
      line: This is a Nautilus sample file, created using Ansible!
      create: true
    • name: add few more content
      lineinfile:
      path: /var/www/html/index.html
      line: Welcome to xFusionCorp Industries!
      create: true

Still getting error that

@Tej-Singh-Rana can you please check on this.

As I remember, in this task you must add line on the top of the file.
And lab itself add extra line in file, just for make in harder.
So try to use this:

---
- name: Service httpd
  hosts: all
  become: yes
  tasks:
    - name: Install the latest version of httpd
      ansible.builtin.yum:
        name: httpd
        state: present
    - name: Start httpd
      ansible.builtin.service:
        name: httpd
        state: started
        enabled: yes
    - name: Create the index.html file
      ansible.builtin.copy:
        dest: /var/www/html/index.html
        content: |
            This is a Nautilus sample file, created using Ansible!
    - name: Add a line to a file
      ansible.builtin.lineinfile:
        path: /var/www/html/index.html
        line: Welcome to xFusionCorp Industries!
        insertbefore: BOF
        mode: '0644'        
        owner: 'apache'
        group: 'apache'

tried but still failed

@bhard @Tej-Singh-Rana can you please check…

Hi @Dhirendra-Vats,

Thanks for reporting. I am looking into it.

Regards,

The playbook shared by Bhard is working for me.

I checked your playbook, and the playbook was putting the “Welcome to …” message at the bottom in the index.html page. As per the task description, this should be at the top.


I modified my playbook still it is failing

Solved, I was making mistake in permission… Thanks @bhard @Tej-Singh-Rana

I’m glad to hear that you solved it yourself. :+1: