I'm trying to replace values in /etc/nslcd.conf file with values in a parameters file. There are two parameters that are causing me so much heartache.

base dc=example,dc=com
base   group ou=groups,dc=example,dc=com

The parameters file has values for these two variables like this

base = dc=company,dc=org
base   group = ou=admingroups,dc=company,dc=org

I'm cycling through each parameter after storing them in a dictionary like this

for key in sorted(d):
  item = key
  print("Processing Key:", item)
  with open ('/etc/nslcd.conf', 'r') as fr:
    with open ('/tmp/nslcd_tmp', 'w+') as fw:
      # Iterate through lines in nslcd.conf file
      for line in fr:
        # Some options are commented and some are not
        itemy = '#' + item
        if line.startswith(item) or line.startswith(itemy):
          print('Replaced value for:', item)
          fw.write(item + '' + d[item])
        elif item == 'base   group':
          fw.write(item + '' + d[item])

The problem I'm having... 1. Since there are keys such as uri that are mentioned multiple times in the file, I have to cycle through all the lines and replace all of them.
2. So, first 'base' goes through and replaces all lines beginning with 'base' with 'base dc=company,dc=org' and there is no more 'base group' item to replace.

How can I tell it not to match 'base' in 'base group'? These are not the only items I'm replacing and so I to keep it generic as much as possible. Thanks.

0 Answers