Giter Club home page Giter Club logo

terraform-state-ansible-module's Introduction

Ansible module for retrieving Terraform state properties

Build Status

Coming full circle: provision machines using Terraform and Ansible, then use the Terraform state to power your Ansible!

Installation

Download latest release from the prebuilt releases on GitHub. Rename the released binary to terraform_state and place it in ~/.ansible/plugins/modules or any other valid Ansible module directory.

Usage

The module supports two Terraform file types: terraform.tf and vars.tf. When passing the path to terraform.tf file, an existence of a properly configured backend is assumed.

Handling state using terraform.tf

---
- hosts: all
  become: no
  connection: local
  gather_facts: no
  tasks:
    - terraform_state:
        terraform_file_path: /path/to/my/terraform.tf
        retrieves:
         - { retrieve: o/bucket_backups }
         - { retrieve: r/aws_s3_bucket.backups/bucket_domain_name }
         - { retrieve: r/aws_s3_bucket.backups/bucket }
      register: retrieved_data
    - set_fact:
        retrieved_terraform_state_data: "{{ retrieved_data.msg | from_json }}"
    - debug:
        msg: " ==> I have got {{ retrieved_terraform_state_data.aws_s3_bucket.backups }}"

Handling variables using vars.tf

---
- hosts: all
  become: no
  connection: local
  gather_facts: no
  tasks:
    - terraform_state:
        terraform_file_path: /path/to/my/vars.tf
      register: retrieved_vars
    - set_fact:
        retrieved_vars_data: "{{ retrieved_vars.msg | from_json }}"
    - debug:
        msg: " ==> I have these vars {{ retrieved_vars_data }}"

The data retrieved from the vars.tf file contains only the values provided with the default property. Furthermore, the default property existence is explicit in the output. For a vars.tf file:

variable "key" {
    default = "test value"
}

the value is avialble via <retrieved_vars>.key.default.

Parameters

  • terraform_file_path: string, required, path to the terraform.tf or vars.tf file, no default

Following configuration settings take effect only when terraform.tf is used:

  • state: string, optional, state name to use, default value default
  • retrieves: a list of values to retrieve, at least one is required, each retrieve
    • retrieve: string, required, property to retrieve; more below
    • module_path: string, optional, default empty string (maps to the root path)
  • require_all: boolean, default false; if true, the module will fail when at least one of the values could not be found in the Terraform state

Retrieve paths

Currently, only two possible combinations can be reetrieved:

  • output: o/<output name>
  • resource: r/<resource>/<attribute>

Creating releases

To cut a release, run:

curl -sL https://raw.githubusercontent.com/radekg/git-release/master/git-release --output /tmp/git-release
chmod +x /tmp/git-release
/tmp/git-release --repository-path=$GOPATH/src/github.com/radekg/terraform-state-ansible-module
rm -rf /tmp/git-release

After the release is cut, build the binaries for the release:

git checkout v${RELEASE_VERSION}
./bin/build-release-binaries.sh

After the binaries are built, upload the to GitHub release.

terraform-state-ansible-module's People

Contributors

radekg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.