I want to insert a line with a user test999 before a construction site with a user test3 .I can't insert a line before another one, it is constantly inserted into the last one, for what reason can this happen?
---
- hosts: app_stage
become: yes
vars:
user: test999
tasks:
- name: remove immutable
file:
path: /etc/sudoers
attr: '-i'
- name: change permission
file:
path: /etc/sudoers
mode: 0660
- name: Allow ‘user’ to have passwordless sudo
lineinfile:
dest: /etc/sudoers
state: present
regexp: '^test3 ALL=(ALL) NOPASSWD: ALL'
insertbefore: '^test3 ALL=(ALL) NOPASSWD: ALL'
line: '{{ user }} ALL=(ALL) NOPASSWD: ALL'
validate: visudo -cf %s
- name: change permissions back
file:
path: /etc/sudoers
mode: 0440
owner: root
group: root
- name: back immutable
file:
path: /etc/sudoers
attr: '+i'
Check the lineinfile module documentation for insertbefore
:
Used with state=present
If specified regular expression has no matches, the line will be inserted at the end of the file.
If regular expressions are passed to both regexp
and insertbefore
, insertbefore
is only honored if no match for regexp is found.