While this answer identifies things correctly, and this one mitigates potential problems, take a look at your code.
You ask Ansible (possibly) to replace the string defined as follows:
regexp: ^Defaults env_reset
This is clearly a bad practice, and if it is repeated for a parameter other than Defaults in the sudoers file, this can cause a critical problem.
Normally, Defaults is a configuration parameter, and env_reset is one of the possible values.
You cannot assume that the actual configuration file will always contain the string ^Defaults env_reset .
If a different value was specified, the regex will not match, and you will add a second line starting with Defaults .
Thus, the correct way to use lineinfile is to use the regexp argument to match only the configuration parameter, not its value. In your case:
regexp: ^Defaults line: Defaults env_reset,timestamp_timeout
Another potential error is that sudoers contain sections that must be written in the correct order. If the file you are lineinfile does not contain the line specified in the regular expression, lineinfile will add a new line to the end of the file, where it may be ignored or lead to an error (but this should be detected by checking), and most likely cause confusion. if a person looks at the file later. Therefore, it would be advisable to specify insertafter or insertbefore .
source share