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>? Sep 19, 2017 at 14:54
  • 1
    Have you researched the "lineinfile" module?
    – Deathgrip
    Sep 19, 2017 at 14:58

1 Answer 1


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, 2017 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .