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: