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)
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??