# Help writing custom modules

**URL:** <https://forum.ansible.com/t/help-writing-custom-modules/27093>\
**Category:** Ansible Project\
**Created:** [March 23, 2018, 2:43pm UTC](https://forum.ansible.com/t/help-writing-custom-modules/27093 "2018-03-23T14:43:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Weald](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@Simon\_Weald](https://forum.ansible.com/u/Simon_Weald)\
**Post date:** [March 23, 2018, 2:43pm UTC](https://forum.ansible.com/t/help-writing-custom-modules/27093/1 "2018-03-23T14:43:59Z")

</div>

I’m currently writing my own modules to interface with my DNS provider, but I’m running into some issues regarding some execution logic. The provider currently has the following structure for their records: zone \< zone domain \< zone domain record.

- zone is a container which logically holds all the domains and common DNS records

- zone domain is a domain name

- ex: [test.com](http://test.com)

- zone domain record is a DNS record which relates to that domain.

- A record for www with 1.2.3.4 would create a DNS entry for www…test.com

creation of these is easy using the create playbook here: [https://gist.github.com/analbeard/cb5913abfc6101b4eef88674853f3c16](https://gist.github.com/analbeard/cb5913abfc6101b4eef88674853f3c16)

However deletion is problematic. If you use the delete playbook at that link, the first task will succeed and then the rest will fail due to to the fact that the zone deletion also removes everything inside the zone (domains and records). So, my question really is how would you expect that to function? I can see some ways around this.

- I could add a force flag to the zone module which will only remove a zone which is completely empty of domains/records if it isn’t set tot true, however in which case how should I handle attempted deletion of a zone with contents but without force? Fail hard? Do nothing?
- I could allow the zone deletion to occur but then have the zone domain / zone record modules just pass successfully if the zone they’re supposed to be in doesn’t exist?

What would be the best approach to this?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 23, 2018, 4:29pm UTC](https://forum.ansible.com/t/help-writing-custom-modules/27093/2 "2018-03-23T16:29:08Z")

</div>

> creation of these is easy using the create playbook here:  
> [https://gist.github.com/analbeard/cb5913abfc6101b4eef88674853f3c16](https://gist.github.com/analbeard/cb5913abfc6101b4eef88674853f3c16)
> 
> However deletion is problematic. If you use the delete playbook at that  
> link, the first task will succeed and then the rest will fail due to to the  
> fact that the zone deletion also removes everything inside the zone  
> (domains and records). So, my question really is how would you expect that  
> to function? I can see some ways around this.
> 
> &nbsp;&nbsp;&nbsp;- I could add a force flag to the zone module which will only remove a  
> &nbsp;&nbsp;&nbsp;zone which is completely empty of domains/records if it isn't set tot true,  
> &nbsp;&nbsp;&nbsp;however in which case how should I handle attempted deletion of a zone with  
> &nbsp;&nbsp;&nbsp;contents but without force? Fail hard? Do nothing?

It's a good idea to make sure the user don't shoot them self in the foot.  
I think you should fail with return code 1 (or some other code), then the user could use failed\_when: to ignore it like this

- name: delete zone  
&nbsp;&nbsp;memset\_zone  
&nbsp;&nbsp;&nbsp;&nbsp;api\_key: 0e0afa4165b141479ed07505991a223d  
&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
&nbsp;&nbsp;&nbsp;&nbsp;name: testzone  
&nbsp;&nbsp;register: result  
&nbsp;&nbsp;failed\_when: result.rc \< 2

> &nbsp;&nbsp;&nbsp;- I could allow the zone deletion to occur but then have the zone domain  
> &nbsp;&nbsp;&nbsp;/ zone record modules just pass successfully if the zone they're supposed  
> &nbsp;&nbsp;&nbsp;to be in doesn't exist?

This is the best solution, they are absent just like the user want and should be successful.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 23, 2018, 4:32pm UTC](https://forum.ansible.com/t/help-writing-custom-modules/27093/3 "2018-03-23T16:32:36Z")

</div>

> \> creation of these is easy using the create playbook here:  
> \> [https://gist.github.com/analbeard/cb5913abfc6101b4eef88674853f3c16](https://gist.github.com/analbeard/cb5913abfc6101b4eef88674853f3c16)  
> \>  
> \>  
> \> However deletion is problematic. If you use the delete playbook at that  
> \> link, the first task will succeed and then the rest will fail due to to the  
> \> fact that the zone deletion also removes everything inside the zone  
> \> (domains and records). So, my question really is how would you expect that  
> \> to function? I can see some ways around this.  
> \>  
> \>  
> \>  
> \> - I could add a force flag to the zone module which will only remove a  
> \> zone which is completely empty of domains/records if it isn't set tot true,  
> \> however in which case how should I handle attempted deletion of a zone with  
> \> contents but without force? Fail hard? Do nothing?
> 
> It's a good idea to make sure the user don't shoot them self in the foot.  
> I think you should fail with return code 1 (or some other code), then the user could use failed\_when: to ignore it like this
> 
> - name: delete zone  
> &nbsp;&nbsp;memset\_zone  
> &nbsp;&nbsp;&nbsp;&nbsp;api\_key: 0e0afa4165b141479ed07505991a223d  
> &nbsp;&nbsp;&nbsp;&nbsp;state: absent  
> &nbsp;&nbsp;&nbsp;&nbsp;name: testzone  
> &nbsp;&nbsp;register: result  
> &nbsp;&nbsp;failed\_when: result.rc \< 2

I made a little error there, to ignore it must be failed\_when: result.rc \> 1
