Ansible Lineinfile Module - Task Failed?

Hi @Inderpreet,

Could you please review my work once. I have created playbook and ran it multiple times to be sure and it ran without any errors, still when i submitted it says wrong content in App server1.

incorrect_content_ansible

Can someone please comment on this, if anyone has came across this error?

Hello, ramashish.sharma
What is the actual question is? They will be give you a response. Don’t worry. Tag to Inderpreet.

Thanks for the response @Tej-Singh-Rana. Here’s the question.

The Nautilus DevOps team want to install and set up a simple httpd web server on all app servers in Stratos DC. They also want to deploy a sample web page using Ansible only. Therefore, prepare the required playbook to complete this task. Find more details about the task below.

We already have an inventory file under /home/thor/ansible on jump host. Create a playbook.yml under /home/thor/ansible on jump host itself.

a. Using the playbook install httpd web server on all app servers, and make sure its service is up and running.
b. Create a file /var/www/html/index.html with content:
==> This is a Nautilus sample file, created using Ansible!

c. 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.

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

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

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.

Yeah. It looks fine. Have you setup file permissions and user & group correctly?

Yes, file permissions and ownership / group was set correctly.

@Inderpreet,

Could you please verify my work and let me know where it is wrong.

Hi @Inderpreet,

I have failed this task with the same validation message as that of @ramashish.sharma though I have index.html with the right contents specified from the task instruction. Please check.

Validation:

The task instruction and curl test to the application servers

Cheers,
Salim

Hi @Salim

The order of the lines within the index.html is not correct.

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

Hi @ivbor,

Do you see anything wrong in my task worked. If so could you help to find the exact cause of the issue.

@Inderpreet,
Could you validate this once at your convenience.

ooh! man, why i did not see that until now! :sweat_smile: @ivbor thanks

1 Like

Not at all, @Salim :slight_smile: It’s your win!

Hi @ramashish.sharma

I agree with @Tej-Singh-Rana curl output matches the expected results. Without seeing the playbook code I can only assume that playbook works a little different than expected. In particular, I would check additionally the code sections which response for creating a file and adding a new line. Highly likely that after some code changes the playbook stopped doing what expected, i.e. ansible stopped change the index.html content during your tests. In contrary, validation script, doing all operations from scratch, reveals the issue with the file content.

Hi @ivbor,

Thanks for your time on analysing my work.
Please find my playbook which i used for this query.

@Francilio @Tej-Singh-Rana ,
Thank you too for checking my work. Appreciate it.

==========

vi playbook.yml
---
  - name: Install and configure web server
    hosts: all
    become: yes
    become_user: root
    tasks:
      - name: Install httpd package on all application servers
        yum:
          name: httpd
          state: installed

      - name: Start httpd service
        service:
          name: httpd
          state: started
          enabled: true

      - name: Create blank file and add contents to it
        copy:
          dest: "/var/www/html/index.html"
          content: |
            This is a Nautilus sample file, created using Ansible!

      - name: Adding more info to index.html file
        lineinfile:
          path: /var/www/html/index.html
          line: 'Welcome to xFusionCorp Industries!'
          insertbefore: BOF

      - name: Change file ownership, group and permissions
        file:
          path: /var/www/html/index.html
          owner: apache
          group: apache
          mode: '0655'

============

1 Like

I’m not an ansible expert, I’m just studying. I think that the mistake lies in this piece of code (multiline syntax used):

content: |
            This is a Nautilus sample file, created using Ansible!

As a result, you may end up with extra lines at the end of the file or unnecessary leading spaces (due to next line indentation). In such cases try to use content: |-
IMHO, using inline content syntax would be more unambiguous in very this task:
content: 'This is a Nautilus sample file, created using Ansible!'
Checking: cat -nET /var/www/html/index.html

Hi ivbor,

I have tested this playbook in my laptop using the content: | method and it didnt generated any blank spaces.

content: |
            This is a Nautilus sample file, created using Ansible!

[root@ansibleclient ~]# cat -nET /var/www/html/index.html
  1  Welcome to xFusionCorp Industries!$
  2  This is a Nautilus sample file, created using Ansible!$
[root@ansibleclient ~]#

I hope my playbook still looks good as per requirement.

I think you should have not specified become-user: root as it was not specified in the task and in copy module you should have specified content as content: ‘This is a Nautilus sample file, created using Ansible!’. I successfully completed the task this way. Hope you will get same task in future.

Thanks for checking that pratikshag. Different algorithms used for checking…so can’t comment more.

I have already asked @Inderpreet to review and let me know where the problem is, but seems he missed to check and validate this task.

Anyways i know what i did was correct, of course the algorithm to check may vary, but the end result matters.

@ramashish.sharma Below is the screen shot from your original answer

ram

You have a typo here, Ansbile vs Ansible

Thank you very much @Inderpreet for checking and let me know. Now I see what mistake I did.