patternyamlMinor
Ansible: Use inventory_hostname variable in lineinfile module
Viewed 0 times
lineinfilemoduleusevariableansibleinventory_hostname
Problem
I'm using Ansible 2.7.9
and I'm having trouble with the regex definition in the lineinfile module. My file has the following line:
I want to remove that line. As {{ inventory_hostname }} contains dots, I'm trying to escape those with
When I'm executing the playbook, the following error occurs:
As you can see, I'm using the double quotes, and all examples that I've found are using this syntax. What I'm doing wrong here? Thanks for any help.
and I'm having trouble with the regex definition in the lineinfile module. My file has the following line:
host.example.com = /path/to/a/directoryI want to remove that line. As {{ inventory_hostname }} contains dots, I'm trying to escape those with
replace. The task in my playbook looks like this:- name: Remove LE webroot definition
lineinfile:
path: "/etc/path/to/config/{{ inventory_hostname }}.conf"
regexp: "^{{ inventory_hostname | replace('.', '\.') }} = /path/to/a/directory"
state: absentWhen I'm executing the playbook, the following error occurs:
[...]
The offending line appears to be:
path: "etc/path/to/config/{{ inventory_hostname }}.conf"
regexp: "^{{ inventory_hostname | replace('.', '\.') }} = /path/to/a/directory"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
[...]As you can see, I'm using the double quotes, and all examples that I've found are using this syntax. What I'm doing wrong here? Thanks for any help.
Solution
The issue seems resolved, If I escape the backslash in the regex too:
- name: Remove LE webroot definition
lineinfile:
path: "/etc/path/to/config/{{ inventory_hostname }}.conf"
regexp: "^{{ inventory_hostname | replace('.', '\\.') }} = /path/to/a/directory"
state: absentCode Snippets
- name: Remove LE webroot definition
lineinfile:
path: "/etc/path/to/config/{{ inventory_hostname }}.conf"
regexp: "^{{ inventory_hostname | replace('.', '\\.') }} = /path/to/a/directory"
state: absentContext
StackExchange DevOps Q#11617, answer score: 4
Revisions (0)
No revisions yet.