# Conditionally updating a list?

**URL:** <https://forum.ansible.com/t/conditionally-updating-a-list/14156>\
**Category:** Ansible Project\
**Created:** [December 14, 2013, 4:38am UTC](https://forum.ansible.com/t/conditionally-updating-a-list/14156 "2013-12-14T04:38:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross\_Becker](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Ross\_Becker](https://forum.ansible.com/u/Ross_Becker)\
**Post date:** [December 14, 2013, 4:38am UTC](https://forum.ansible.com/t/conditionally-updating-a-list/14156/1 "2013-12-14T04:38:26Z")

</div>

So, I’m looking for a way to add items to a list conditionally. The point of the list is to be provided for a with\_items clause. I know I can evaluate python in jinja expressions, but it seems that those changes may be localized to the expression where they are evaluated.

Here’s a general idea of what I’m attempting to do. I have tested this playbook, and the output at the end is “foo”, “bar”, “baz”, with “harpo” and “groucho” nowhere to be found, and yes, I’ve verified that enable\_harpo and enable\_groucho evaluate to true.

---

<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:** [December 14, 2013, 3:20pm UTC](https://forum.ansible.com/t/conditionally-updating-a-list/14156/2 "2013-12-14T15:20:15Z")

</div>

There’s no way to dynamically append to a list presently.

Since you’ve shared an abstract example, it might be a good idea to explain the use case and we can share some other ways to model it?

---

<div class="post-metadata">

**Author:** ![Ross\_Becker](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Ross\_Becker](https://forum.ansible.com/u/Ross_Becker)\
**Post date:** [December 14, 2013, 4:54pm UTC](https://forum.ansible.com/t/conditionally-updating-a-list/14156/3 "2013-12-14T16:54:09Z")

</div>

Well,  
In this case, I have a set of services, each of which needs to have some operations applied to them (the same operations across all services). The use case really is what you see in the abstract example; there are times when some of the services have not been enabled, and if they are enabled, I need to perform the task on them, if not, I don’t. I’ve already got the service operations abstracted out to an ansible task, so instead of dynamically altering the list, I can simply “include” the task again if the service is enabled. Updating a list and using with\_items one time simply felt much cleaner… if it could have been done.

---

<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:** [December 14, 2013, 5:00pm UTC](https://forum.ansible.com/t/conditionally-updating-a-list/14156/4 "2013-12-14T17:00:26Z")

</div>

Perhaps you could do something like this:

- name: update the stuff  
when: services[name].enabled  
with\_items: service\_names

It sounds like you are driving this from inventory and not the state of the current system but it’s not clear to me from the above.

---

<div class="post-metadata">

**Author:** ![Ross\_Becker](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Ross\_Becker](https://forum.ansible.com/u/Ross_Becker)\
**Post date:** [December 14, 2013, 5:41pm UTC](https://forum.ansible.com/t/conditionally-updating-a-list/14156/5 "2013-12-14T17:41:55Z")

</div>

I am driving it from inventory. That’s actually a good call, adding an “\_enable” value for every service, including the services which are never disabled. Thanks!
