# Register ansible\_facts and with\_items usage

**URL:** <https://forum.ansible.com/t/register-ansible-facts-and-with-items-usage/13220>\
**Category:** Ansible Project\
**Created:** [July 30, 2013, 4:22pm UTC](https://forum.ansible.com/t/register-ansible-facts-and-with-items-usage/13220 "2013-07-30T16:22:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitry\_Horbach](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@Dmitry\_Horbach](https://forum.ansible.com/u/Dmitry_Horbach)\
**Post date:** [July 30, 2013, 4:22pm UTC](https://forum.ansible.com/t/register-ansible-facts-and-with-items-usage/13220/1 "2013-07-30T16:22:59Z")

</div>

Hi All,

I’m trying to use custom fact registration in module but found problems when combined it with with\_items

> results = {}  
> facts = {}  
> results[‘ansible\_facts’] = facts  
> facts[var] = os.path.exists(name)  
> module.exit\_json(\*\*results)

While it works in simple cases - after with\_items is added custom fact will not be available.

> - file\_check: name=${item} var=filename  
> with\_items:
> - file1
> - file2
> - debug: msg=“${filename}”

> TASK: [file\_check name=${item} var=filename] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> \<127.0.0.1\> REMOTE\_MODULE file\_check name=file1 var=filename  
> ok: [127.0.0.1] =\> (item=file1) =\> {“ansible\_facts”: {“filename”: false}, “changed”: false, “item”: “file1”}  
> \<127.0.0.1\> REMOTE\_MODULE file\_check name=file2 var=filename  
> ok: [127.0.0.1] =\> (item=file2) =\> {“ansible\_facts”: {“filename”: false}, “changed”: false, “item”: “file2”}  
> TASK: [debug msg=“${filename}”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [127.0.0.1] =\> {“msg”: “ **${filename}** ”}

Is this some known issue?

When not using with\_items:

> TASK: [file\_check name=somefile var=filename] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> \<127.0.0.1\> REMOTE\_MODULE file\_check name=somefile var=filename  
> ok: [127.0.0.1] =\> {“ansible\_facts”: {“filename”: false}, “changed”: false}  
> TASK: [debug msg=“${filename}”] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [127.0.0.1] =\> {“msg”: “ **False** ”}

Thanks,  
Dzmitry

---

<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:** [July 30, 2013, 4:48pm UTC](https://forum.ansible.com/t/register-ansible-facts-and-with-items-usage/13220/2 "2013-07-30T16:48:49Z")

</div>

I’d have to see your full module source to be able to tell what may be going on here.

Also I recommend using new style variables like {{ foo }} for consistency.

The old forms are not quite deprecated, but are discouraged.

---

<div class="post-metadata">

**Author:** ![Dmitry\_Horbach](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@Dmitry\_Horbach](https://forum.ansible.com/u/Dmitry_Horbach)\
**Post date:** [July 30, 2013, 6:59pm UTC](https://forum.ansible.com/t/register-ansible-facts-and-with-items-usage/13220/3 "2013-07-30T18:59:37Z")

</div>

The module itself is quite simple to test ansible var registration:

def main():  
module = AnsibleModule(  
argument\_spec = dict(  
name = dict(required=True, type=‘str’),  
var = dict(required=False, type=‘str’, default=‘file\_check’)  
)  
)  
var = module.params[‘var’]  
name = module.params[‘name’]  
results = {}  
facts = {}  
results[‘ansible\_facts’] = facts  
facts[var] = os.path.exists(name)  
module.exit\_json(\*\*results)

# include magic from lib/ansible/module\_common.py

#\<\<INCLUDE\_ANSIBLE\_MODULE\_COMMON\>\>  
main()

I’m using latest 1.3 HEAD

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 30, 2013, 7:09pm UTC](https://forum.ansible.com/t/register-ansible-facts-and-with-items-usage/13220/4 "2013-07-30T19:09:21Z")

</div>

with\_items is probably not happy when being used with fact modules. This is not /too/ surprising.

In either case, you’d want to make sure you changed the variable each time.

It would be much easier to just use the ‘stat’ module in 1.3 with register, or a basic shell test, but please file a github ticket for this and we can investigate.

---

<div class="post-metadata">

**Author:** ![Dmitry\_Horbach](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@Dmitry\_Horbach](https://forum.ansible.com/u/Dmitry_Horbach)\
**Post date:** [July 30, 2013, 8:06pm UTC](https://forum.ansible.com/t/register-ansible-facts-and-with-items-usage/13220/5 "2013-07-30T20:06:30Z")

</div>

[https://github.com/ansible/ansible/issues/3704](https://github.com/ansible/ansible/issues/3704)  
I discovered this problem while testing our custom “unpack” module which does generic unzip/untar

> - unpack: src=${item} dest=/somepath fact=unpack\_dir  
> with\_items:
> - test.zip
> - test.tar

> - test.gz

Here I want to register a single fact unpack\_dir in order to not copy/paste complex dest value in subsequent tasks.  
This fact value should stay same when single dest directory used for multiple input archives.
