Ansible: Insert line if not exists

2019-02-04 08:13发布

问题:

I'm trying insert a line in a property file using ansible. I want to add some property if it does not exist, but not replace it if such property already exists in the file.

I add to my ansible role

- name: add couchbase host to properties
  lineinfile: dest=/database.properties regexp="^couchbase.host"  line="couchbase.host=127.0.0.1"

But this replaces the property value back to 127.0.0.1 if it exists already in the file.

What I'm doing wrong?

回答1:

The lineinfile module does what it's supposed to: It ensures the line as defined in line is present in the file and the line is identified by your regexp. So no matter what value your setting already has, it will be overridden by your new line.

If you don't want to override the line you first need to test the content and then apply that condition to the lineinfile module. There is no module for testing the content of a file so you probably need to run grep with a shell command and check the .stdout for content. Something like this (untested):

- name: Test for line
  shell: grep "^couchbase.host" /database.properties
  register: test_grep

And then apply the condition to your lineinfile task:

- name: add couchbase host to properties
  lineinfile:
    dest: /database.properties
    line: couchbase.host=127.0.0.1
  when: test_grep.stdout != ""

The regexp then can be removed since you already made sure the line doesn't exist so it never would match.

But maybe you're doing things back to front. Where does that line in the file come from? When you manage your system with Ansible there should be no other mechanisms in place which interfere with the same config files. Maybe you can work around this by adding a default value to your role?



回答2:

This is the only way I was able to get this to work.

- name: checking for host
  shell: cat /database.properties | grep couchbase.host | wc -l
  register: test_grep

- debug: msg="{{test_grep.stdout}}"

- name: adding license server
  lineinfile: dest=/database.properties line="couchbase.host=127.0.0.1"
  when: test_grep.stdout == "0"


回答3:

By a long way of "Trials and errors" I come to this:

- name: check existence of line in the target file
  command: grep -Fxq "ip addr add {{ item }}/32 dev lo label lo:{{ app | default('app') }}" /etc/rc.local
  changed_when: false
  failed_when: false
  register: ip_test
  with_items:
    - "{{ list_of_ips }}"

- name: add autostart command
  lineinfile: dest=/etc/rc.local 
              line="ip addr add {{ item.item }}/32 dev lo label lo:{{ app | default('app') }}"
              insertbefore="exit 0"
              state=present
  when: item.rc == 1
  with_items:
    - "{{ ip_test.results }}"


回答4:

Same idea as presented here : https://stackoverflow.com/a/40890850/7231194

Steps are:

  • Try to replace the line.
  • If replace mod change it, restore
  • If replace mod doesn't change, add the line

Example

# Vars
- name: Set parameters
  set_fact:
    ipAddress    : "127.0.0.1"
    lineSearched : "couchbase.host={{ ipAddress }}"
    lineModified : "couchbase.host={{ ipAddress }} hello"

# Tasks
- name: Try to replace the line
  replace:
    dest    : /dir/file
    replace : '{{ lineModified }} '
    regexp  : '{{ lineSearched }}$'
    backup  : yes
 register  : checkIfLineIsHere

# If the line not is here, I add it
- name: Add line
  lineinfile:
  state   : present
  dest    : /dir/file
  line    : '{{ lineSearched }}'
  regexp  : ''
  insertafter: EOF
when: checkIfLineIsHere.changed == false

# If the line is here, I still want this line in the file, Then restore it
- name: Restore the searched line.
  lineinfile:
    state   : present
    dest    : /dir/file
    line    : '{{ lineSearched }}'
    regexp  : '{{ lineModified }}$'
  when: checkIfLineIsHere.changed


回答5:

Ok, here is mine naive solution... probably not a cross-platform and native Ansible (I've just started to use this tool and still learn it), but definitely shorter:

- name: Update /path/to/some/file
  shell: grep -q 'regex' /path/to/some/file && echo exists || echo 'text-to-append' >> /path/to/some/file
  register: result
  changed_when: result.stdout.find('exists') == -1