# How best to express desired merged or replaced state for module that accepts a list of items

**URL:** <https://forum.ansible.com/t/how-best-to-express-desired-merged-or-replaced-state-for-module-that-accepts-a-list-of-items/39895>\
**Category:** Get Help\
**Tags:** dev, module, galaxy-ng\
**Created:** [January 29, 2025, 3:10am UTC](https://forum.ansible.com/t/how-best-to-express-desired-merged-or-replaced-state-for-module-that-accepts-a-list-of-items/39895 "2025-01-29T03:10:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![opoplawski](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/opoplawski/32/2112_2.png) [@opoplawski](https://forum.ansible.com/u/opoplawski)\
**Post date:** [January 29, 2025, 3:10am UTC](https://forum.ansible.com/t/how-best-to-express-desired-merged-or-replaced-state-for-module-that-accepts-a-list-of-items/39895/1 "2025-01-29T03:10:13Z")

</div>

I’m working on the [pfsensible.core.pfsense\_dns\_resolver](https://galaxy.ansible.com/ui/repo/published/pfsensible/core/content/module/pfsense_dns_resolver/) module and am looking for suggestions on how to express something. It accepts two lists of items: domainoverrides and hosts. Currently it will replace all of each with the items passed to the module. Users would like to be able have the call simply add/modify the items in the list but keep any existing items. I’d like suggestions on how to express this in the module.

I think I’d like to avoid using the `state` option, but could explore it if it is the recommended way to do it.

Thanks!

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [January 29, 2025, 1:32pm UTC](https://forum.ansible.com/t/how-best-to-express-desired-merged-or-replaced-state-for-module-that-accepts-a-list-of-items/39895/2 "2025-01-29T13:32:42Z")

</div>

The aws modules do this with `purge_` options, for example amazon.aws.ec2\_group has [purge\_tags](https://docs.ansible.com/ansible/6/collections/amazon/aws/ec2_group_module.html#parameter-purge_tags) which defaults to True, so tags are replaced by default, but only if the `tags` parameter is provided.

---

<div class="post-metadata">

**Author:** ![opoplawski](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/opoplawski/32/2112_2.png) [@opoplawski](https://forum.ansible.com/u/opoplawski)\
**Post date:** [January 31, 2025, 4:54am UTC](https://forum.ansible.com/t/how-best-to-express-desired-merged-or-replaced-state-for-module-that-accepts-a-list-of-items/39895/3 "2025-01-31T04:54:30Z")

</div>

That seems reasonable to me. Thanks.

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [January 31, 2025, 11:13am UTC](https://forum.ansible.com/t/how-best-to-express-desired-merged-or-replaced-state-for-module-that-accepts-a-list-of-items/39895/4 "2025-01-31T11:13:32Z")

</div>

Another example is:

[https://docs.ansible.com/ansible/latest/collections/ansible/posix/authorized\_key\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/posix/authorized_key_module.html)

and option called `exclusive`. It tells the module to remove anything that is not specified in a list. In your case you would have multiple options like `domainoverrides_exclusive` and `hosts_exclusive`, both boolean values. I think `exclusive` as a word better describes the intent than `purge`. It’s more declarative.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 2, 2025, 11:14am UTC](https://forum.ansible.com/t/how-best-to-express-desired-merged-or-replaced-state-for-module-that-accepts-a-list-of-items/39895/5 "2025-03-02T11:14:16Z")

</div>

Closed on March 2, 2025, 11:14am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
