# Can lineinfile take variable as parameters?

**URL:** <https://forum.ansible.com/t/can-lineinfile-take-variable-as-parameters/15728>\
**Category:** Ansible Project\
**Created:** [July 21, 2014, 6:47pm UTC](https://forum.ansible.com/t/can-lineinfile-take-variable-as-parameters/15728 "2014-07-21T18:47:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@Sean](https://forum.ansible.com/u/Sean)\
**Post date:** [July 21, 2014, 6:47pm UTC](https://forum.ansible.com/t/can-lineinfile-take-variable-as-parameters/15728/1 "2014-07-21T18:47:27Z")

</div>

I tried to change one line in a file with lineinfile command, but it looks like this command does not resolve variables. See my playbook snippet below

#in my playbook, {{ kibana\_doc\_root }}, {{ kibana\_version }}, {{kibana\_config\_file}}, {{elasticsearch\_URL}}, {{ elasticsearch\_port }} have been defined

The following snippet can successfully change the file content in config.js, but {{elasticsearch\_URL}} is not resolved (i.e. the line shows {{elasticsearch\_URL}} as is):

----- Snippet 1 -------------

- name: debug variables  
debug: msg=" variable concatication = {{ kibana\_doc\_root }}/{{ kibana\_version }}/{{kibana\_config\_file}}"

- name: Replace the URI of the ElasticSearch service in config.js on Kibana server  
lineinfile:  
dest=/var/www/kibana/kibana-3.0.0milestone4/config.js  
regexp=“(elasticsearch.\* "http.\*)$”  
state=present  
line="elasticsearch’:’ " {{ elasticsearch\_URL }}:{{ elasticsearch\_port }} " "

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 21, 2014, 11:13pm UTC](https://forum.ansible.com/t/can-lineinfile-take-variable-as-parameters/15728/2 "2014-07-21T23:13:10Z")

</div>

A quick question or to – what version of Ansible are you trying this on?

In your ansible.cfg, is this perhaps an upgrade that does not have “fail on undefined variables” turned on?

Most people have this on now, but not all. It’s the default if you don’t have an ansible.cfg and your version is new enough.

---

<div class="post-metadata">

**Author:** ![Sean](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@Sean](https://forum.ansible.com/u/Sean)\
**Post date:** [July 21, 2014, 11:22pm UTC](https://forum.ansible.com/t/can-lineinfile-take-variable-as-parameters/15728/3 "2014-07-21T23:22:29Z")

</div>

> A quick question or to – what version of Ansible are you trying this on?

ansible 1.5.4

> In your ansible.cfg, is this perhaps an upgrade that does not have “fail on undefined variables” turned on?

The my ansible.cfg file within my project has one line like this:

error\_on\_undefined\_vars = False

Should I change it to True?

---

<div class="post-metadata">

**Author:** ![Sean](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@Sean](https://forum.ansible.com/u/Sean)\
**Post date:** [July 21, 2014, 11:26pm UTC](https://forum.ansible.com/t/can-lineinfile-take-variable-as-parameters/15728/4 "2014-07-21T23:26:19Z")

</div>

> A quick question or to – what version of Ansible are you trying this on?
> 
> In your ansible.cfg, is this perhaps an upgrade that does not have “fail on undefined variables” turned on?
> 
> Most people have this on now, but not all. It’s the default if you don’t have an ansible.cfg and your version is new enough.

BTW, I have tested that the variables have been defined.

Here is my playbook:

- name: debug variables  
debug: msg=" variable concatication = {{ kibana\_doc\_root }}/{{ kibana\_version }}/{{ kibana\_config\_file }}"

Here is the corresponding output:

TASK: [kibana | debug variables] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [kibana] =\> {

“item”: “”,

“msg”: " variable concatication = /var/www/kibana/kibana-3.0.0milestone4/config.js"

}

---

<div class="post-metadata">

**Author:** ![Sean](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@Sean](https://forum.ansible.com/u/Sean)\
**Post date:** [July 22, 2014, 1:08am UTC](https://forum.ansible.com/t/can-lineinfile-take-variable-as-parameters/15728/5 "2014-07-22T01:08:34Z")

</div>

Hi all,

Just FYI in case that other folks may find this helpful later.

I took Michael’s hint, and changed the error\_on\_undefined\_vars from False to True, which basically returns an explicit error if there exists a missing variable, as follow.

myproject\_foler/.ansible.cfg

#error\_on\_undefined\_vars = False

error\_on\_undefined\_vars = True

And I found that the original error was caused by one missing variable in the lineinfile command, {{ elasticsearch\_URL }}. Once I add those variables in my vars file, everything start working 🙂

In short, lineinfile does take varaiables and it works perfectly

My only slight complaint is that the error message was not as related to the root cause as it could be.

Thanks to Michael.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 22, 2014, 12:07pm UTC](https://forum.ansible.com/t/can-lineinfile-take-variable-as-parameters/15728/6 "2014-07-22T12:07:25Z")

</div>

Good deal!

BTW, please don’t run 1.5.4.

The current ansible is 1.6.7, and contains security fixes which you should apply.
