# pass a dictionary to copy module while using the ansible api

**URL:** <https://forum.ansible.com/t/pass-a-dictionary-to-copy-module-while-using-the-ansible-api/32213>\
**Category:** Ansible Developer\
**Created:** [February 12, 2020, 4:43pm UTC](https://forum.ansible.com/t/pass-a-dictionary-to-copy-module-while-using-the-ansible-api/32213 "2020-02-12T16:43:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@Mario\_Garcia](https://forum.ansible.com/u/Mario_Garcia)\
**Post date:** [February 12, 2020, 4:43pm UTC](https://forum.ansible.com/t/pass-a-dictionary-to-copy-module-while-using-the-ansible-api/32213/1 "2020-02-12T16:43:16Z")

</div>

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.

`

```auto
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:
