sickfear
sickfear

Reputation: 103

replacing lines in files with ansible

I'm creating an ansible playbook to perform changes on /etc/login.defs.

I cannot use a template as other teams might perform changes on it, I only want to modify what I need. Maybe there are modules to handle login.defs but I don't want to rely on external dependencies as server is not opened to the Internet.

I want to modify PASS_MAX_DAYS parameter setting its value to 60. Default one is PASS_MAX_DAYS 99999 but I don't want to specify the 99999, just replace the whole line in case it founds PASS_MAX_DAYS and uncomment it in case it is commented.

I tried the following option but it doesn't replace the line

name: configure password length
 become: yes
 lineinfile:
  dest: /etc/login.defs
  regexp: ^#(?)PASS_MAX_DAYS.*
  line: "PASS_MAX_DAYS 60"

Am I doing something wrong?

Upvotes: 1

Views: 1997

Answers (1)

rakesh_singh
rakesh_singh

Reputation: 86

This seems to work as you want if the file contains the default value, or if the value has been commented out.

  lineinfile:
    dest: /etc/login.defs
    regexp: '^[#]?[\s+]?PASS_MAX_DAYS\s+\d+'
    line: "PASS_MAX_DAYS   60"
    backup: true

Upvotes: 2

Related Questions