Ansible lineinfile module task failing

We already have an inventory file under /home/thor/ansible directory on jump host.
Write a playbook playbook.yml under /home/thor/ansible directory on jump host itself.
Using the playbook perform below given tasks:

Install httpd web server on all app servers, and make sure its service is up and running.

Create a file /var/www/html/index.html with content:

This is a Nautilus sample file, created using Ansible!

Using lineinfile Ansible module add some more content in /var/www/html/index.html file.
Below is the content:
Welcome to xFusionCorp Industries!

Also make sure this new line is added at the top of the file.

The /var/www/html/index.html file’s user and group owner should be apache on all app servers.

The /var/www/html/index.html file’s permissions should be 0755 on all app servers.

Note: Validation will try to run the playbook using command
ansible-playbook -i inventory playbook.yml so please make sure the playbook
works this way without passing any extra arguments.

thor@jump_host ~/ansible$ vim playbook.yml

  • name: install and start httpd on all app servers
    hosts: all
    remote_user: root
    become: true

    tasks:

    • name: Install the latest version of Apache
      ansible.builtin.package:
      name:
      - httpd
      state: latest

    • name: Start service httpd, if not started
      ansible.builtin.service:
      name: httpd
      state: started

    • name: Create index.html file and add line to file
      ansible.builtin.lineinfile:
      path: /var/www/html/index.html
      line: “This is a Nautilus sample file, created using Ansible!”
      create: yes

    • name: Update permissions of index.html
      ansible.builtin.file:
      path: /var/www/html/index.html
      owner: apache
      group: apache
      mode: 0755

    • name: Append line of text to index.html and add new line before beginning of file
      ansible.builtin.lineinfile:
      path: /var/www/html/index.html
      insertbefore: BOF
      line: ‘Welcome to xFusionCorp Industries!’

thor@jump_host ~/ansible$

Below is the index.html content found on the target servers after running the playbook:
[root@stapp01 ~]# cat /var/www/html/index.html
Welcome to xFusionCorp Industries!
This is KodeKloud Ansible Lab !
This is a Nautilus sample file, created using Ansible!
[root@stapp01 ~]#
The problem:
I’m not sure how the second line “This is KodeKloud Ansible Lab !” is getting inserted into the index.html file based on the above playbook.

I found out that the problem was because the /var/www/html/index.html already had some content before the playbook was run.

I added a task in the playbook to delete /var/www/html/index.html before recreating it with the correct content.

I think this task should be modified to either:
-add a note to check the content of the file before running the playbook;
-do not says in the task “Create a file /var/www/html/index.html” but “Modify /var/www/html/index.html”
-the index.html file should not exist on the target servers at the beginning of the task

There is too much confusion for such a simple task. I feel the statement of the task is leading to this.

Hi @jcakakpo,
Thanks for your valuable suggestion. I will forward this to the KKE team.

Regards,

1 Like

As per my understanding, this line gives me a clue that the existing file has some content and the team wants me to add more content on it with the following conditions.

Thanks, Tej-Singh-Rana !

Yes, Tej, that line in the task statement that you highlighted says “add some more content” but if we just add the new content without clearing the old content, the task validation is not successful. The validation does not expect for the old text to be in the file. It only expects the new text.