Register ansible_facts and with_items usage

454 views
Skip to first unread message

Dmitry Horbach

unread,
Jul 30, 2013, 12:22:59 PM7/30/13
to ansible...@googlegroups.com
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 
 

Michael DeHaan

unread,
Jul 30, 2013, 12:48:49 PM7/30/13
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Dmitry Horbach

unread,
Jul 30, 2013, 2:59:37 PM7/30/13
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Jul 30, 2013, 3:09:21 PM7/30/13
to ansible...@googlegroups.com
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.


Dmitry Horbach

unread,
Jul 30, 2013, 4:06:30 PM7/30/13
to ansible...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages