# how to read a yaml file and update itself ?

**URL:** <https://forum.ansible.com/t/how-to-read-a-yaml-file-and-update-itself/35969>\
**Category:** Ansible Project\
**Created:** [November 5, 2021, 1:12am UTC](https://forum.ansible.com/t/how-to-read-a-yaml-file-and-update-itself/35969 "2021-11-05T01:12:49Z")\
**Posts on this page:** 5\
**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:** [November 5, 2021, 1:12am UTC](https://forum.ansible.com/t/how-to-read-a-yaml-file-and-update-itself/35969/1 "2021-11-05T01:12:49Z")

</div>

let’s said I have a yaml file like

centers.yml

centers:

- center\_id: ABC123  
center\_password: 123456789
- center\_id: 123ABC  
center\_password: “”

now I want to read the centers.yml , and update center\_password with pwgen(8) if center\_password is null and write back to centers.yml

How should I do the job ?

---

<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:** [November 5, 2021, 7:04am UTC](https://forum.ansible.com/t/how-to-read-a-yaml-file-and-update-itself/35969/2 "2021-11-05T07:04:32Z")

</div>

Is this file local or on your remote host?

---

<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:** [November 8, 2021, 12:58am UTC](https://forum.ansible.com/t/how-to-read-a-yaml-file-and-update-itself/35969/3 "2021-11-08T00:58:56Z")

</div>

> > Is this file local or on your remote host?

local

[dick....@geant.org](mailto:dick....@geant.org) 在 2021年11月5日 星期五下午3:04:57 [UTC+8] 的信中寫道：

---

<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:** [November 8, 2021, 10:04am UTC](https://forum.ansible.com/t/how-to-read-a-yaml-file-and-update-itself/35969/4 "2021-11-08T10:04:31Z")

</div>

This should work, although it could probably be optimised:

---

<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:** [November 12, 2021, 1:43am UTC](https://forum.ansible.com/t/how-to-read-a-yaml-file-and-update-itself/35969/5 "2021-11-12T01:43:07Z")

</div>

WOW , that’s incredible ! thanks !

[dick....@geant.org](mailto:dick....@geant.org) 在 2021年11月8日 星期一下午6:04:56 [UTC+8] 的信中寫道：
