pass a dictionary to copy module while using the ansible api

7 views
Skip to first unread message

Mario Garcia

unread,
Feb 12, 2020, 11:43:16 AM2/12/20
to Ansible Development
Hello,

I would like to use the copy module while using the ansible api and instead of passing a source I would like to pass directtly the content.. the content is stored in a dictionary wher ethe key is the hostname and the value is the content of the file.

for some reason i do not manage to have the script perhaps is the ansible play to expand the variable 'inventory_hostname' that is passed as a key to a dictionary.

the play can  display the variable because I also display using the debug module.




from custom lib import generate_content
file_content
= {}
for hostname in hostnames:
    file_content
[hostname] = generate_content(hostname)

loader
= DataLoader()
passwords
= { }

context
.CLIARGS = ImmutableDict(tags={}, listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh',
                    module_path
=None, forks=100,  private_key_file=None,
                    ssh_common_args
=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None,become=True,
                    become_method
='sudo',become_user='root', verbosity=True, check=False, start_at_task=None)


Inventory = InventoryManager(loader=loader, sources=my_inv)

variable_manager
= VariableManager(loader=loader, inventory=Inventory, version_info=CLI.version_info(gitinfo=False))

play_source
=  dict(
        name
= "Ansible Play",
        hosts
= 'all',
        gather_facts
= 'yes',
        tasks
= [
            dict
(action=dict(module='debug', args=dict(msg='{{inventory_hostname}}'))),
            dict
(action=dict(module='copy', content=file_content[{{inventory_hostname}}], dest='/etc/content', backup='yes'))
         
]
   
)

play
= Play().load(play_source, variable_manager=variable_manager, loader=loader)


tqm
= None
try:
    tqm
= TaskQueueManager(
              inventory
=Inventory,
              variable_manager
=variable_manager,
              loader
=loader,
              passwords
=passwords,
              stdout_callback
="default",
         
)
    result
= tqm.run(play)
finally:

   
if tqm is not None:
        tqm
.cleanup()
    shutil
.rmtree(C.DEFAULT_LOCAL_TMP, True)


this gives me a NameError:

 dict(action=dict(module='copy', content=file_content[inventory_hostname], dest='/etc/content', backup='yes')) NameError: name 'inventory_hostname' is not defined


if i use single quotes to surround the variable i get a KeyError:

 dict(action=dict(module='copy', content=sudoers_content["{{inventory_hostname}}"], dest='/etc/sudoers5', backup='yes'), register='shell_out')
KeyError: u'{{inventory_hostname}}'

I am using python 2.7 no possibility to test on python3.

is there a limitation on ansible API to pass a dict to the content.. the problem really seems to expanding the variable because i hardcode the hostname on the copy module line.. it deploys the file properly

per instance this works:



play_source
=  dict(
        name
= "Ansible Play",
        hosts
= 'all',
        gather_facts
= 'yes',
        tasks
= [
            dict
(action=dict(module='debug', args=dict(msg='{{inventory_hostname}}'))),
            dict
(action=dict(module='copy', content=file_content['vmtest'], dest='/etc/content', backup='yes'))


so why i cannot expand the inventory_hostname or even the ansible_nodename variable when passing it as a key to a dict.. because of all evidence it accept a dict['key'] as imput for tht contents??


Reply all
Reply to author
Forward
0 new messages