I need to write a playbook to replace the line locking_dir = "/var/lock/lvm" to locking_dir = "/run/lock" in /etc/lvm/lvm.conf using ansible

/root # cat /etc/lvm/lvm.conf | grep locking_dir | grep -v '#'
    locking_dir = "/var/lock/lvm"


locking_dir = "/run/lock"
  • ansible -m command <cmd>? – Raman Sailopal Sep 19 '17 at 14:54
  • 1
    Have you researched the "lineinfile" module? – Deathgrip Sep 19 '17 at 14:58

As an Ad-Hoc command:

ansible '<hosts>' -m lineinfile -a "dest=/etc/lvm/lvm.conf backrefs=yes regexp='^(locking_dir)' line='\1 = \"/run/lock\"' state=present"

As part of a playbook:

- lineinfile:
    dest: /etc/lvm/lvm.conf
    backrefs: yes
    regexp: '^(locking_dir)'
    line: '\1 = \"/run/lock\"'
    state: present

I believe lvm.conf will have only one line beginning with locking_dir so I didn't bother to use a regex to match the complete line. Otherwise the regex would be something like '^locking_dir = \"/var/lock/lvm\"'

  • Thanks for your comments. Below playbook got worked for my requirement. - name: LVM config replace: path=/tmp/lvm.conf regexp='locking_dir = "/var/lock/lvm"' replace='locking_dir = "/run/lock"' backup=yes – Pandu Sep 20 '17 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.