1

I have ORIGINAL .conf file which includes multiple commented lines (header). I need to throw an error if the ORIGINAL "comments only" file has any uncommented lines added during the installation process. If there is any line other than starting with #/comment, I want to throw a warning.

I was thinking if Ansible is able to do that.

So far, I came with some ideas:

Loop through the ORIGINAL file, creating a COPY of the current ORIGINAL file, but copy only lines starting with #/comments (header) from the ORIGINAL (possibly any empty white space lines too) After that, the COPY file will have only commented lines(header) and empty/white space lines, excluded of any other lines.

Then do the diff/compare original and copy. If the COPY file which has only commented lines (header), doesn't match the ORIGINAL file, that means uncommented lines are present in the ORIGINAL and I need to throw an error.

Ohter ideas:

  1. warning if uncommented line was added after last commented line
  2. Lines in file present starting with character other than #
  3. Count uncommented lines and throw error if more than 0 present
  4. End of file has changed warning
  5. Endoffile/ last line is in state as uncommented

I think that is in some ways a hack solution using Ansible.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 4
    _I need to create an ansible to throw error if more lines are added to the original file/file has changed._ > so, to be clear, you have the original version of the file in the playbook and wants a warning if the file have changed? – β.εηοιτ.βε Apr 29 '23 at 19:12
  • Hi thank you for your response. That is correct. The file has a header composed of comments. I want to make sure that there is nothing more than the header, and if there is an uncommented line, I need to create a warning. The file needs to be empty of uncommented lines during the last steps of installation to be execute correctly. When running the playbook and the file changed, this ansible will create a warning, If theres just comments in the file(original version only with comments), the playbook goes through. – Marián Šlepecký Apr 29 '23 at 21:14
  • 3
    Use the `copy` module with `check_mode: true`. Register the result. Fire a warning if the result is `changed` – Zeitounator Apr 30 '23 at 05:28
  • Thanks folks. With this approach of "copy" + "check_mode: true" to register the result/file state, and then do a diff. Shoudnt this be two ansibles to first copy/register the file and then compare later on? I am brand new with Ansible, so trying to figure out. Can you write a short example with a dummy file? Also, what if the file already had uncommented lines in it and the copy is already compromised? – Marián Šlepecký Apr 30 '23 at 14:10
  • 1
    If there is any line other than starting with "#" as a comment, I want to throw a warning. – Marián Šlepecký Apr 30 '23 at 14:18
  • if the file was already compromised from the start, then creating a copy of that original file while removing everything after the last comment, "after line", then compare the original file with the copy after the install and throw an error if different. Would this be a solution? – Marián Šlepecký Apr 30 '23 at 14:46

1 Answers1

0

Use Case Description

How to throw a warning when a file contains uncommented lines? ... I need to ... throw an error if the original "comments only" file has any uncommented lines ... If there is any line other than starting with # ...

I understand that you like to know if the file contains more than zero (0) lines starting without # and just as you described in your simplest idea #3

"Count uncommented lines and throw error if more than 0 present".

Solution Proposal

A simple quick and lazy approach can be to use the shell module and just count the lines. So a minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    FILE_TO_TEST: "file.txt"

  tasks:

  - name: Count uncommented lines
    shell:
      cmd:  grep ^[^#] {{ FILE_TO_TEST }} | wc -l
    register: result
    # Since this is a reporting task
    check_mode: false
    changed_when: false
    failed_when: result.rc != 0

  - name: Throw error if > 0
    assert:
      that:
        - result.stdout == 0
      fail_msg: "There are {{ result.stdout }} uncommented lines!"

will provide the requested information.

Please Take Note

Printing a WARNING message is something different than throwing an ERROR and let a task FAIL (WARNING != ERROR). You may need to consider this into your final solution, in example by adding ignore_errors: true to the assert as in Ansible: How to print WARNINGs from playbook?.

How to proceed further?

If interested in a more advanced and generic approach and some background information one may proceed further with

U880D
  • 8,601
  • 6
  • 24
  • 40