{ "info": { "author": "", "author_email": "", "bugtrack_url": null, "classifiers": [], "description": "Yet Another Terraform Ansible Dynamic Inventory Script (yatadis)\n================================================================\n\nAn `ansible dynamic\ninventory `__\nscript which takes `Terraform `__ state files\nas input.\n\nIn contrast with other Terraform ansible dynamic inventory scripts, this\none aims to be configurable to match your environment. It implements\nthis using `Jinja2 `__ templates to specify how\nTerraform resource attributes should be mapped onto ansible inventory\nname, group, and host\\_vars.\n\nBasic usage\n-----------\n\nAnsible calls dynamic inventory scripts with either the ``--list`` or\n``--host`` option, but no additional arguments. For that reason, yatadis\naccepts all of its options from environment variables: \\* TF\\_STATE: a\npath to a local terraform.tfstate file (default: terraform.tfstate in\nthe current directory) \\* TF\\_ANSIBLE\\_INVENTORY\\_NAME\\_TEMPLATE: a\n`Jinja2 `__ template string that is applied to\neach Terraform resource to generate the ansible inventory name (default:\n``{{ resource_name }}`` which is the resource name (TYPE+NAME) from\nTerraform is guaranteed to be unique). \\* TF\\_ANSIBLE\\_GROUPS\\_TEMPLATE:\na `Jinja2 `__ template string that is applied\nto each Terraform resource to generate a newline-delimited list of\nansible groups to which the resource should belong (default: ``all``\nwhich simply assigns all hosts to the ``all`` group) \\*\nTF\\_ANSIBLE\\_RESOURCE\\_FILTER\\_TEMPLATE: a\n`Jinja2 `__ template string that is applied to\neach Terraform resource and should produce either ``True`` (to include\nthe resource) or ``False`` (to exclude the resource). (default:\n``{{ type in [\"aws_instance\",\"azure_instance\",\"clc_server\",\"digitalocean_droplet\",\"google_compute_instance\",\"openstack_compute_instance_v2\",\"softlayer_virtualserver\",\"triton_machine\",\"ucs_service_profile\",\"vsphere_virtual_machine\"] }}``\nwhich is suitable to limit to instance/machine resources from a variety\nof Terraform providers. \\* TF\\_ANSIBLE\\_HOST\\_VARS\\_TEMPLATE: a\n`Jinja2 `__ template string that is applied to\neach Terraform resource and should generate a newline-delimited list of\nhost\\_var settings in the format ``=``. (default: a\ntemplate that will set ``ansible_host`` to the IP of the\ninstance/machine as well as setting all resource attributes prefixed\nwith ``tf_`` - see source code for details).\n\nIf you are happy with the defaults, and can arrange for the TF\\_STATE\nenvironment variable to be set to the path to the terraform.tfstate\nfile, then you can just install the yatadis.py script in the ansible\ninventory directory, make sure it is executable, and that all of the\npython modules it depends on are installed on the machine on which you\nrun ansible.\n\nIn practice, you will most likely want to call yatadis.py from a wrapper\nscript (such as a bash script) that you install into the inventory\ndirectory in place of yatadis.py itself and which sets those variables\nappropriately. For example, here is a simple shell script that simply\ninvokes yatadis.py after setting the path to the terraform.tfstate file:\n\n::\n\n #!/bin/bash\n\n export TF_STATE=/path/to/terraform.tfstate\n /path/to/yatadis.py $@\n\nYou can also specify any of these options on the command line (for\ntesting purposes) - the command line argument is simply the environment\nvariable name without the \"TF\" prefix:\n\n::\n\n ./yatadis.py --list --state /path/to/terraform.tfstate\n\nAdding terraform resources to ansible groups\n--------------------------------------------\n\nThe defaults may be all you need, as all of the primary attributes of\neach Terraform compute resource will be available in ansible as\nhost\\_vars with the prefix \"tf\\_\", and you can use ansible dynamic\ngroups (using the `group\\_by\nmodule `__ to add\nhosts to groups based on those host\\_vars values).\n\nFor example, in your site playbook you might add the following:\n\n::\n\n - hosts: all\n tasks:\n - group_by: key=tf_image_{{ tf_image_name }}\n\nIf you had a resource with a Terraform image\\_name of ``ubuntu_16.04``\nthen it should now be a member of the ansible group\n``tf_image_ubuntu_16.04``\n\nAlternatively, yatadis can assign hosts to ansible groups for you\nwithout the need for ansible's dynamic group functionality.\n\nTo do this you will need to set the ``TF_ANSIBLE_GROUPS_TEMPLATE``\n`Jinja2 `__ template such that it returns a\nnewline-delimited list of groups to which a host should belong.\n\nFor example, to add all instances to a group named after the resource\nprovider and prefixed with ``tf_provider_``, you could use the following\nwrapper script:\n\n::\n\n #!/bin/bash\n export TF_ANSIBLE_GROUPS_TEMPLATE='{{ [\"all\", \"tf_provider_\"+provider] | join(\"\\n\") }}'\n export TF_STATE=/path/to/terraform.tfstate\n /path/to/yatadis.py $@\n\nTemplate context\n----------------\n\nThe context provided to the Jinja2 templates is a dict-like Resource\nobject containing the same fields as the Terraform state resource\nfields. There is also an additional top-level entry called 'name' which\ncontains the resource name (i.e. the key value of the resource entry).\nFinally, in addition to the ``attributes`` section (in flattened\n'flatmap' format as it is in the Terraform state file), there is also an\n``expanded_attributes`` section alongside it which has been expanded\ninto nested dist and list structures.\n\nAdvanced host\\_vars templating\n------------------------------\n\nAs a special case, since ansible host\\_vars can contain complex data\nstructures, if the values output by the host\\_vars template are a dict\nor a list, they will be evaluated as such rather than as a string, so\nthat the resulting ansible host\\_vars entry can contain complex data\nstructures.\n\nFor example, the following (uninteresting) example would assign the\nfoo\\_dict and abc123\\_list host\\_vars to every resource:\n\n::\n\n #!/bin/bash\n export TF_ANSIBLE_HOST_VARS_TEMPLATE=$(cat <