# A Playbook which Stores Vars

**URL:** <https://forum.ansible.com/t/a-playbook-which-stores-vars/13408>\
**Category:** Ansible Project\
**Created:** [September 2, 2013, 10:53pm UTC](https://forum.ansible.com/t/a-playbook-which-stores-vars/13408 "2013-09-02T22:53:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_O\_Conne2](https://avatars.discourse-cdn.com/v4/letter/c/e0b2c6/32.png) [@Christopher\_O\_Conne2](https://forum.ansible.com/u/Christopher_O_Conne2)\
**Post date:** [September 2, 2013, 10:53pm UTC](https://forum.ansible.com/t/a-playbook-which-stores-vars/13408/1 "2013-09-02T22:53:25Z")

</div>

Hi,

I’ve been struggling with how to implement a task which creates a new var which will be used in subsequent playbooks.

For example, I have a accounts.yml vars file with entries like this:

- accounts:

- title: Some human readable name  
owner\_email: [somebody@somewhere.com](mailto:somebody@somewhere.com)  
name: sombody\_at\_somewhere\_com

api\_key: ApiKeyString

s3\_bucket: [s3.bucket.name](http://s3.bucket.name)

- title: Another human readable name

etc.

In my main playbook, I include a tasks files to generate a series of server configs based on these vars using

- include: tasks\_for\_each\_account.yml

with\_items: ${accounts}

My main playbook based on this works fine.

I also created a simple playbook (create\_account) which prompts for email and “human readable” name and uses the uri and s3 actions to create a new API key and S3 bucket for an account. The problem is that I then need to manually open up the _accounts.yml_ file and add a new entry for this newly created account.

My goal is that the create\_account playbook prompts for the two required fields, creates the API key and S3 bucket and then stores the information in such a way that it’s available to other playbooks (for example my main playbook).

I’m not at all sure that storing all of the account information in a single yml file is the best idea, but it’s what I went with as I started this.

My initial impulse is to use a local action with a template to create a series of files in the vars directory like _somebody\_at\_somewhere\_com.yml_, _person\_at\_url\_net.yml_, etc. Unfortunately, I’m not sure how I would iterate over these created vars files in other playbooks. Since these created vars files would define a series of separate top level variables, instead of a single top level list, simply saying “with\_items” wouldn’t work. I’ve looked at files\_glob, but I don’t think it would work in this case (feel free to correct me if I’m wrong).

In any case, before I go too far down the rabbit hole in this direction, I’d like some feedback. I’m also open to other suggestions (like “creating new accounts isn’t really supported in ansible, just write a helper script to do that and update the _accounts.yml_ file”), but I’d prefer to keep it in ansible if possible.

All the best,

~ Christopher

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 3, 2013, 8:47pm UTC](https://forum.ansible.com/t/a-playbook-which-stores-vars/13408/2 "2013-09-03T20:47:04Z")

</div>

Public service announcement. This post uses old style variables. Please don’t use those as you can just say:

with\_items: accounts

If you’re trying to create a variable in a playbook, any variables you use with the “register” task are accessible in “hostvars” later, even from other server operations.

You should never do include + with\_items as it’s super confusing, and this will be an error in future versions of Ansible.

Instead put your loop within the task file and use “with\_items” inside there.
