# Adding a value to a host\_vars file from an ansible play

**URL:** <https://forum.ansible.com/t/adding-a-value-to-a-host-vars-file-from-an-ansible-play/20981>\
**Category:** Ansible Project\
**Created:** [March 24, 2016, 2:31pm UTC](https://forum.ansible.com/t/adding-a-value-to-a-host-vars-file-from-an-ansible-play/20981 "2016-03-24T14:31:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [March 24, 2016, 2:31pm UTC](https://forum.ansible.com/t/adding-a-value-to-a-host-vars-file-from-an-ansible-play/20981/1 "2016-03-24T14:31:30Z")

</div>

Hi again,

next question:

Bootstrapping a new server, creating a user, setting a random  
password. Works like a charm.

But is it possible to save the generated password in the corresponding  
host\_vars file for the new server?

Johannes

---

<div class="post-metadata">

**Author:** ![Nigel\_Metheringham](https://avatars.discourse-cdn.com/v4/letter/n/3ec8ea/32.png) [@Nigel\_Metheringham](https://forum.ansible.com/u/Nigel_Metheringham)\
**Post date:** [March 24, 2016, 5:23pm UTC](https://forum.ansible.com/t/adding-a-value-to-a-host-vars-file-from-an-ansible-play/20981/2 "2016-03-24T17:23:54Z")

</div>

I have a slightly different approach to this. I have a relatively generic pooldata action plugin. This is used in a play like this:-

```auto
- name: Get root password
  pooldata:
    data_file: pooldata/rootpw/used/{{inventory_hostname}}.json
    pool_directory: pooldata/rootpw/pool

- name: Set root password
  user:  
    name: root
    state: present
    password: '{{ generated_root_password.crypted_password }}'
    comment: 'Superuser Account'

```

If there is a file corresponding to the `data_file` then the (JSON) contents of that are loaded into facts.

Otherwise a random file from the pool\_directory is renamed to the `data_file` and that is loaded.

The files are autogenerated by a script, and look like this:-

```auto
{
   "generated_root_password" : {
      "crypted_password" : "$6$U2.gCoRx$r19ANf9aUP2/Eqvj.SciaMFo5QXDS8pp1gKtQQMGJrb571V8Tp.dCNCeKhnqWllQ39g.5VZjBI7a4vjnJqwr31",
      "tag" : "ceVePaDa",
      "record_number" : 2049
   }
}

```

The script that generates them also generates a lookup table stored separately of the actual password against the tag/record number. This means the clear text root passwords are not stored on the ansible box.

This dance is done to allow the “database” to be easily held in git without potentially hitting difficult merge issues if there are branches etc.

I’m very happy to make the pooldata plugin available - I should have cleaned it up and pushed it to github years ago!

```auto
Nigel.

```

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [March 24, 2016, 8:07pm UTC](https://forum.ansible.com/t/adding-a-value-to-a-host-vars-file-from-an-ansible-play/20981/3 "2016-03-24T20:07:09Z")

</div>

Hi Nigel,

> I have a slightly different approach to this. I have a relatively generic pooldata action plugin. This is used in a p

Thanks for the answer. I am always interested in new things to learn,  
so if you drop me a line if you put the script on github that would be  
awesome.

Nevertheless, the root password stuff is not the only thing I would  
need the change-stuff-in-host\_vars-file for. So I would be interested  
if this is possible. Without using a 'lineinfile' on the local host...

Johannes
