Register ansible_facts and with_items usage

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

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.

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

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.

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.