akutschi / ansible_terraform

Ansible role that invokes terraform to create a dynamic inventory

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ansible Terraform

This role creates a dynamic inventory with groups that can be used by Ansible. The groups in this inventory are coming from the Terraform output file. See this example for the Terraform code: outputs.tf.

Requirements

  • This version requires Ubuntu 20.04 LTS
  • Ansible 2.9.x, this role is not tested with Ansible 2.10 yet.
  • Terraform 0.14.x

Role Variables

This role does have two variables:

Variable Default Description
ansible_terraform_path ./../terraform/dev/vpn Path to your root Terraform root module
ansible_terraform_tfvars ../secrets.tfvars Path to the file that contains the secrets for Terraform. This path is relative to the one from ansible_terraform_path.

Following, simplified folder structure is assumed:

IaC
├── ansible
│   ├── playbook.yml
│   └── roles
│       ├── ansible-ssh
│       ├── ansible-terraform
│       └── ansible-update
└── terraform
    ├── dev
    │   ├── secrets.tfvars
    │   └── vpn
    │       ├── main.tf
    │       ├── output.tf
    │       └── variables.tf
    ├── module
    └── prod

Dependencies

This role does not have any dependencies.

Example Playbook

The setup is quite simple. Just clone or download this role into your roles folder and set up the playbook similar to this example:

---
- name: Run Terraform and build inventory dynamically
  hosts: localhost
  roles:
    - ansible-terraform

- name: Wait for all machines to become ready
  hosts: all
  gather_facts: no
  tasks:
  - name: Wait for machine to become reachable
    wait_for_connection:
      delay: 10
      sleep: 10

After ansible-terraform is finished it is recommended to make a short break and to wait until all machines become available.

If your directory with Terraform is somewhere else use the following playbook as inspiration:

- name: Run Terraform and build inventory dynamically
  hosts: localhost
  vars:
    - ansible_terraform_path: ./path/to/somewhere/else
    - ansible_terraform_tfvars: ./relative/path/to/somewhere/else
  roles:
    - ansible-terraform

- name: Wait for all machines to become ready
  hosts: all
  gather_facts: no
  tasks:
  - name: Wait for machine to become reachable
    wait_for_connection:
      delay: 10
      sleep: 10

License

GPLv3, see license.

Contributing

Feel free to open issues or merge requests if you find problems or have ideas for improvements. Thank you.

About

Ansible role that invokes terraform to create a dynamic inventory

License:GNU General Public License v3.0


Languages

Language:YAML 100.0%