Ansible Lineinfile
Ansible Lineinfile: The lineinfile is one of the most remarkable modules in the Ansible management tool. Ansible lineinfile module is utilized to embed a line, adjust, eliminate, and supplant a current line.
Ansible lineinfile module spares your time when you work with the record and alter their substance on the run, such as adding another line in the document or refreshing, supplanting a line in the document when explicit content is found, and considerably more.
Ansible lineinfile gives numerous parameters to do the work rapidly. Similarly, you can use the condition to coordinate the line before changing and eliminating the standard expressions. You can reuse and alter the coordinated line using the backreference parameter.
Alternatives
parameter | required | default | choices | comments |
attributes (included in 2.3) | no | none | Attributes the record or directory. To get upheld flag, take a gander at the main page for chattr on the objective framework. This string contains the properties in a similar way as the one showed by lsattr. aliases: attr | |
backrefs | No | No | Yesno | It is used by state=present. Whenever set, the line can contain backreferences (both positional and named) that will get populated if the regexp matches. This banner changes the activity of the module somewhat; insertbefore and insertafter will be disregarded, and if the regexp doesn't coordinate anyplace in the document, the record will be left unaltered. If the regexp matches, the last coordinating line will be supplanted by the extended line parameter. |
backup | No | No | Yesno | Make a backup document including the timestamp data so you can get the first record back in the event that you some way or another destroyed it erroneously. |
create | No | No | Yesno | Utilized with state=present. Whenever indicated, the document will be made if it doesn't exist. Naturally, it will fizzle if the document is absent. |
group | No | Name of the gathering that should possess the record/catalog, as would be taken care of to chown. | ||
insertafter | No | EOF | EOF*regex* | Used with state=present. Whenever determined, the line will be embedded after the last match of indicated standard expression. A unique worth is accessible; EOF for embeddings the line toward the finish of the document. Whenever determined, ordinary expression has no matches, end of the file uses all things being equal. It may not be utilized with backrefs. |
insertbefore | No | BOF*regex* | Utilized with state=present. Whenever determined, the line will be embedded before the last match of indicated normal expression. A value is accessible; BOF for embeddings the line toward the start of the document. Whenever indicated customary expression has no matches, the line will be embedded toward the finish of the document. It may not be utilized with backrefs. | |
line | No | It is needed for state=present. The line to embed/supplant into the record. If backrefs is configured, it can contain backreferences that will get extended with the regexp catch gatherings if the regexp matches. | ||
mode | No | Mode the document or index ought to be. Those used to /usr/bin/chmod recollect that modes are octal numbers (such as 0784). Leaving off the main zero will probably have sudden outcomes. As of version 1.8, the mode might be indicated as an emblematic mode (for instance, u+rwx or u=rw, g=r, o=r). | ||
others | No | All contentions acknowledged by the document module additionally work here. | ||
owner | no | Name of the client who should possess the document/index would be taken care of to chown. | ||
path | Yes | The document to adjust. Before 2.3, this choice was just useful as dest, destfile, and name. aliases: dest, destfile, name | ||
regexp (included in 1.7) | no | The standard expression to search for in each line of the record. For state=present, the example of supplanting whenever found. Just the last line found will be supplanted. For state=absent, the example of the line(s) to eliminate. Utilizations Python normal expressions. | ||
selevel | No | s0 | Level part of the SELinux record reference. This is the MLS/MCS property, at times called the range. _default highlight fills in concerning seuser. | |
serole | No | Role part of SELinux document reference, _default highlight functions concerning seuser. | ||
setype | No | Type part of SELinux record reference, _default highlight fills in concerning seuser. | ||
seuser | No | Client part of SELinux record setting. Will default to framework strategy, if material. Whenever set to _default, it will use the client part of the policy. | ||
state | No | present | Presentabsent | If the line ought to be there. |
unsafe_writes (included in 2.2) | no | Yesno | Typically, this module uses nuclear activities to forestall information defilement or conflicting peruses from the target records; once in a while, the framework is arranged or just broken in manners that forestall this. One model is docker mounted records; they can't be refreshed molecularly and must be done in a hazardous way. This boolean choice permits ansible to fall back to unsafe strategies for refreshing records for those cases in which you don't have some other decision. Know that this is liable to race conditions and can prompt information defilement. | |
validate | no | none | It is the approval order to run prior to replicating into place. The way to the document to approve is passed through '%s', which must be available as in the model underneath. The order is passed safely, so shell highlights like development and lines won't work. |
Insert a Line
It tells how we perceive and how to compose a line to a record in the event when it is absent. You can set the way of the document to be changed using the way (>Ansible 2.3)/dest parameter. Also, set the line to be embedded using the line parameter.
The underneath model will compose the line "Embeddings a line in a document" to the record "remote_server.txt." The new line will be included in the EOF. On the off chance that the line beforehand exists, it won't be consolidated.
You can likewise set the make parameter, which says that it will make another record if the document is absent. The default incentive for the state is available.
- hosts: xyz tasks: - name: Ansible supplement lineinfile lineinfile: dest:/home/desktop-JRSKQQX/remote_server.txt line: Inserting a line in a record. state: present make: yes
Eliminating a Line
Set the state parameter to “missing” or eliminate the line indicated. Each event of that line will be eliminated.
- hosts: xyz tasks: - name: Ansible lineinfile eliminate the line lineinfile: dest:/home/desktop-JRSKQQX/remote_server.txt line: Removed lines. state: missing
Replacing or Modifying a Line
If you want to change a line, you need to use the Ansible backrefs boundary alongside the regexp boundary. This ought to be used with state=present.
In the event that the regexp doesn't coordinate any line, at that point, the record isn't changed. On the off chance that the regexp matches a line or different lines, at that point, the last coordinated line will be supplanted. The assembled components in regexp are manned and can be utilized for change.
In the underneath model, we are remarking on a line. The full line is caught line by putting them inside the enclosure to '\1'. The '#\1' replaces the line with '#' trailed by what was caught.
You can use various captures according to your need and call them by using '\1', '\2', '\3', and so forth.
Remarking a line with Ansible lineinfile backrefs
- name: Ansible lineinfile regexp supplant the model lineinfile: dest:/etc/ansible/ansible.cfg regexp: '(stock =/home/fedora/inventory.ini.*)' line: '#\1' backrefs: yes
Uncommenting the line with lineinfile regexp
- name: Ansible lineinfile backrefs model lineinfile: dest:/and so on/ansible/ansible.cfg regexp: '#(inventory =/home/fedora/inventory.ini.*)' line: '\1' backrefs: yes