summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRĂ©mi REY <rrey94@gmail.com>2018-08-29 22:26:19 +0200
committerRyan Brown <sb@ryansb.com>2018-08-29 16:26:19 -0400
commitcd7c946ea5ecc2c8a8ad2d679d7efb805138331c (patch)
tree01a69622d92ecf249b9b39d37ffa59b074d96139 /lib
parent1247f4a4641e5ddf714cd6f695752609619600a2 (diff)
downloadansible-cd7c946ea5ecc2c8a8ad2d679d7efb805138331c.tar.gz
modules/terraform: add support of backend configuration (#44860)
This patch adds a new `backend_config` parameter that allows to provide the -backend-config parameter during the terraform init command. The option allows to dynamically set the backend information, like the s3 bucket name and statefile name.
Diffstat (limited to 'lib')
-rw-r--r--lib/ansible/modules/cloud/misc/terraform.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/lib/ansible/modules/cloud/misc/terraform.py b/lib/ansible/modules/cloud/misc/terraform.py
index 7f5fa6dcb1..46003ca3ec 100644
--- a/lib/ansible/modules/cloud/misc/terraform.py
+++ b/lib/ansible/modules/cloud/misc/terraform.py
@@ -98,6 +98,11 @@ options:
default: false
required: false
type: bool
+ backend_config:
+ description:
+ - A group of key-values to provide at init stage to the -backend-config parameter.
+ required: false
+ version_added: 2.7
notes:
- To just run a `terraform plan`, use check mode.
requirements: [ "terraform" ]
@@ -109,6 +114,16 @@ EXAMPLES = """
- terraform:
project_path: '{{ project_dir }}'
state: present
+
+# Define the backend configuration at init
+- terraform:
+ project_path: 'project/'
+ state: "{{ state }}"
+ force_init: true
+ backend_config:
+ region: "eu-west-1"
+ bucket: "some-bucket"
+ key: "random.tfstate"
"""
RETURN = """
@@ -175,8 +190,14 @@ def _state_args(state_file):
return []
-def init_plugins(bin_path, project_path):
+def init_plugins(bin_path, project_path, backend_config):
command = [bin_path, 'init', '-input=false']
+ if backend_config:
+ for key, val in backend_config.items():
+ command.extend([
+ '-backend-config',
+ shlex_quote('{0}={1}'.format(key, val))
+ ])
rc, out, err = module.run_command(command, cwd=project_path)
if rc != 0:
module.fail_json(msg="Failed to initialize Terraform modules:\r\n{0}".format(err))
@@ -262,6 +283,7 @@ def main():
lock=dict(type='bool', default=True),
lock_timeout=dict(type='int',),
force_init=dict(type='bool', default=False),
+ backend_config=dict(type='dict', default=None),
),
required_if=[('state', 'planned', ['plan_file'])],
supports_check_mode=True,
@@ -277,6 +299,7 @@ def main():
plan_file = module.params.get('plan_file')
state_file = module.params.get('state_file')
force_init = module.params.get('force_init')
+ backend_config = module.params.get('backend_config')
if bin_path is not None:
command = [bin_path]
@@ -284,7 +307,7 @@ def main():
command = [module.get_bin_path('terraform', required=True)]
if force_init:
- init_plugins(command[0], project_path)
+ init_plugins(command[0], project_path, backend_config)
workspace_ctx = get_workspace_context(command[0], project_path)
if workspace_ctx["current"] != workspace: