Giter Club home page Giter Club logo

ansible_fetch_terraform_backend_outputs's Introduction

ansible_fetch_terraform_backend_outputs

Ansible module that fetches output dictionary from Terraform tfstate file from s3 backend.

Requirements

  • boto3
  • json

Module Options

  • bucket - Name of the s3 bucket where Terraform state is stored. Required.
  • object - Name of the s3 object where Terraform state is stored. Required.
  • aws_profile - Name of the aws profile to be used. Default "default".
  • aws_access_key - AWS access key to be used for bucket access. If declared aws_profile option is ignored and aws_secret_access_key option is required. Default "".
  • aws_secret_access_key - AWS secret access key to be used for bucket access. If declared aws_profile option is ignored and aws_access_key option is required. Default "".
  • aws_region - ID of AWS region to connect to s3 bucket from. Default "us-east-1".

Examples

The following play fetches Terraform outputs from arn:aws:s3:::terraform-state-repository/ireland/katapult_cloud_networking.tfstate using default AWS profile in ~/.aws/credentials.

---
- hosts: localhost
  become: false
  tasks:
    - name: fetch Terraform networking outputs from Ireland region
      fetch_terraform_backend_outputs:
        bucket: "terraform-state-repository"
        object: "ireland/katapult_cloud_networking.tfstate"
      register: vpc_networking

    - name: set vpc id
      set_fact:
        vpc_id: "{{ vpc_networking.vars.katapult_cloud_vpc_id }}"
...

The following play fetches Terraform outputs from arn:aws:s3:::terraform-state-repository/ireland/katapult_cloud_networking.tfstate using AWS access and secret access keys.

---
- hosts: localhost
  become: false
  tasks:
    - name: fetch Terraform networking outputs from Ireland region
      fetch_terraform_backend_outputs:
        bucket: "terraform-state-repository"
        object: "ireland/katapult_cloud_networking.tfstate"
        aws_access_key: AAABBBTTGSSSS45
        aws_secret_access_key: jbd63ij2bdft/812ebud1f2623m2837rmmqj
      register: vpc_networking

    - name: set vpc id
      set_fact:
        vpc_id: "{{ vpc_networking.vars.katapult_cloud_vpc_id }}"
...

Recommendations

Utilize profile or access keys with minimal privileges to AWS resources. If possible utilize credentials with read only access to the Terraform state bucket.

License

GPL-v3

Author Information

Stefan Roman ([email protected])

ansible_fetch_terraform_backend_outputs's People

Contributors

katapultcloud avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ansible_fetch_terraform_backend_outputs's Issues

INVALID OUTPUT FORMAT

Does it still work?

$ /tmp/ansible/hacking/test-module -m ./fetch_terraform_backend_outputs.py -a 'aws_profile=default bucket=web.xxxxx.com object=web_xxxxx_web.tfstate'
* including generated source, if any, saving to: /home/jiri/.ansible_module_generated
* ansiballz module detected; extracted module source to: /home/jiri/debug_dir
***********************************
RAW OUTPUT

Traceback (most recent call last):
  File "/home/jiri/debug_dir/ansible/modules/fetch_terraform_backend_outputs.py", line 164, in <module>
    main()
  File "/home/jiri/debug_dir/ansible/modules/fetch_terraform_backend_outputs.py", line 157, in main
    result = backend_pull(s3_client, module.params)
  File "/home/jiri/debug_dir/ansible/modules/fetch_terraform_backend_outputs.py", line 105, in backend_pull
    return format_data(raw_data)
  File "/home/jiri/debug_dir/ansible/modules/fetch_terraform_backend_outputs.py", line 90, in format_data
    for element in pretty_data['modules']:
KeyError: 'modules'

***********************************
INVALID OUTPUT FORMAT

Traceback (most recent call last):
  File "/tmp/ansible/hacking/test-module", line 238, in runtest
    results = json.loads(out)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

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.