# how to dynamic update value in vars\_file

**URL:** <https://forum.ansible.com/t/how-to-dynamic-update-value-in-vars-file/32920>\
**Category:** Ansible Project\
**Created:** [May 13, 2020, 1:34am UTC](https://forum.ansible.com/t/how-to-dynamic-update-value-in-vars-file/32920 "2020-05-13T01:34:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Chang](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@Eric\_Chang](https://forum.ansible.com/u/Eric_Chang)\
**Post date:** [May 13, 2020, 1:34am UTC](https://forum.ansible.com/t/how-to-dynamic-update-value-in-vars-file/32920/1 "2020-05-13T01:34:11Z")

</div>

I have a var file like

`  
nodes:

- name: rk12  
location: tp-101  
ma:  
own:
- name: rk14  
location: tp-201  
ma: p11023  
own: p11023
- name: rk15  
location: tc-332  
ma:  
own: p92032

`

and I want to update nodes.ma with somevalue based on location in playbook , if ma is not defined.  
I try lineinfile , but there could be more lines that ma was not defined. (like rk12 , rk15 )  
can I update specified nodes.ma in playbook ?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 13, 2020, 7:04am UTC](https://forum.ansible.com/t/how-to-dynamic-update-value-in-vars-file/32920/2 "2020-05-13T07:04:14Z")

</div>

Update vars files based on the content of a variable it contains  
sounds like doing things backwards.  
Could you tell more about your ACTUAL deployment goal(s)? I.e. what  
are you trying to achieve with ansible?

---

<div class="post-metadata">

**Author:** ![Eric\_Chang](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@Eric\_Chang](https://forum.ansible.com/u/Eric_Chang)\
**Post date:** [May 13, 2020, 7:32am UTC](https://forum.ansible.com/t/how-to-dynamic-update-value-in-vars-file/32920/3 "2020-05-13T07:32:04Z")

</div>

like I said , I want to update [nodes.ma](http://nodes.ma) with somevalue based on location in playbook , if ma is not defined.  
ma is matainer’s id , owner is customer’s id.  
with different location , there are different matainers.  
and because some reason , we didn’t keep all that records , so there are plenty of missing maintainers  
I want to update these data via ansible , if ma is missing , then I will assign a employee to it based on location.  
thats what I want to do.

Dick Visser於 2020年5月13日星期三 UTC+8下午3時04分42秒寫道：

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 13, 2020, 8:02am UTC](https://forum.ansible.com/t/how-to-dynamic-update-value-in-vars-file/32920/4 "2020-05-13T08:02:50Z")

</div>

> like I said , I want to update nodes.ma \<[http://nodes.ma](http://nodes.ma)\> with somevalue based on location in playbook , if ma is not  
> defined.  
> ma is matainer's id , owner is customer's id.  
> with different location , there are different matainers.  
> and because some reason , we didn't keep all that records , so there are plenty of missing maintainers  
> I want to update these data via ansible , if ma is missing , then I will assign a employee to it based on location.  
> thats what I want to do.

How does your playbook look like right now?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Eric\_Chang](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@Eric\_Chang](https://forum.ansible.com/u/Eric_Chang)\
**Post date:** [May 13, 2020, 1:58pm UTC](https://forum.ansible.com/t/how-to-dynamic-update-value-in-vars-file/32920/5 "2020-05-13T13:58:34Z")

</div>

location is defined in inventory file , but I have trouble to update nodes.pub

`

- hosts: srvs  
become: yes  
vars:  
location: l13

tasks:

- name: debug vars  
debug:  
var: nodes

- name: update vars.yml when pub is not defined  
lineinfile:  
line: “public: none”  
path: vars.yml  
state: present  
when: item.location == location and item.pub == “”  
with\_items: “{{ nodes }}”

`

Stefan Hornburg (Racke)於 2020年5月13日星期三 UTC+8下午4時03分16秒寫道：

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 13, 2020, 2:12pm UTC](https://forum.ansible.com/t/how-to-dynamic-update-value-in-vars-file/32920/6 "2020-05-13T14:12:23Z")

</div>

> location is defined in inventory file , but I have trouble to update nodes.pub

lineinfile is not appropriate for any structured data (JSON, YAML, XML, ...).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Eric\_Chang](https://avatars.discourse-cdn.com/v4/letter/e/e68b1a/32.png) [@Eric\_Chang](https://forum.ansible.com/u/Eric_Chang)\
**Post date:** [May 14, 2020, 1:30pm UTC](https://forum.ansible.com/t/how-to-dynamic-update-value-in-vars-file/32920/7 "2020-05-14T13:30:46Z")

</div>

so , what module you recommend ?

Stefan Hornburg (Racke)於 2020年5月13日星期三 UTC+8下午10時12分58秒寫道：
