Adding a line to a file that is identical to another line in the same file

Hi,

I’ve been writing a playbook for openstack for our very specific openstack architecture here and, as I am completely new to ansible, I have some issues with adding some configurations at the end of a file.

My goal : Adding a block of text at the end of the nova.conf file that will reference neutron settings.

What I’ve figured out up to now : I can use the lineinfile module and loop it with “with_items” to add several lines. The regexp should be as close to the line as possible and should be unique, so that not only the line is added at the end of the file, but also isn’t written each time the playbook is run.

So this looks somewhat like this up to now :

  • name: Add neutron section to nova.conf
    lineinfile: dest=/etc/nova/nova.conf
    regexp: “{{ item.regexp }}”
    line: “{{ item.line }}”
    state: present
    insertafter: EOF
    with_items:
  • { regexp: ‘[1]’, line: ‘[neutron]’}
  • { “regexp= ‘^url = http:’, line= ‘url = http://neutron:9696’”}

*you can see I put " on my last line and replaced : with = as it may cause issues with yaml I believe. Not sure if I’m right on this one, but that’s not the main issue I have right now.

The main issue: I need to add this line in the neutron section of the config file : auth_strategy = keystone . However, this exact same line already exist in the file in the default section. So, if I specify a regexp for it, it will consider that the line already exists and won’t write the line at the end of file. However, if I specify a regexp that does not exist at all, the line will be added each time the playbook is run.

What would be the best way to bypass this problem? Alternatively, if I could just set the neutron section as a template and then add it to the nova.conf file, it would simplify this whole operation a lot. I haven’t seen a way to add a template’s content to an already existing file though.


  1. neutron ↩︎