[terraform] Support initializing modules and providers at runtime (#36996)

* Fixes #36994

Added function for force initializing modules and providers

Added type for force_init parameter
This commit is contained in:
Mikhail Naletov 2018-03-05 18:01:53 +02:00 committed by Ryan Brown
parent 985f09270d
commit 7d0e1f92f4
2 changed files with 14 additions and 2 deletions

View file

@ -79,8 +79,9 @@ options:
- To avoid duplicating infra, if a state file can't be found this will - To avoid duplicating infra, if a state file can't be found this will
force a `terraform init`. Generally, this should be turned off unless force a `terraform init`. Generally, this should be turned off unless
you intend to provision an entirely new Terraform deployment. you intend to provision an entirely new Terraform deployment.
required: false
default: false default: false
required: false
type: bool
notes: notes:
- To just run a `terraform plan`, use check mode. - To just run a `terraform plan`, use check mode.
requirements: [ "terraform" ] requirements: [ "terraform" ]
@ -155,6 +156,13 @@ def _state_args(state_file):
return [] return []
def init_plugins(bin_path, project_path):
command = [bin_path, 'init']
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))
def build_plan(bin_path, project_path, variables_args, state_file, plan_path=None): def build_plan(bin_path, project_path, variables_args, state_file, plan_path=None):
if plan_path is None: if plan_path is None:
f, plan_path = tempfile.mkstemp(suffix='.tfplan') f, plan_path = tempfile.mkstemp(suffix='.tfplan')
@ -191,6 +199,7 @@ def main():
targets=dict(type='list', default=[]), targets=dict(type='list', default=[]),
lock=dict(type='bool', default=True), lock=dict(type='bool', default=True),
lock_timeout=dict(type='int',), lock_timeout=dict(type='int',),
force_init=dict(type='bool', default=False)
), ),
required_if=[('state', 'planned', ['plan_file'])], required_if=[('state', 'planned', ['plan_file'])],
supports_check_mode=True, supports_check_mode=True,
@ -203,12 +212,16 @@ def main():
variables_file = module.params.get('variables_file') variables_file = module.params.get('variables_file')
plan_file = module.params.get('plan_file') plan_file = module.params.get('plan_file')
state_file = module.params.get('state_file') state_file = module.params.get('state_file')
force_init = module.params.get('force_init')
if bin_path is not None: if bin_path is not None:
command = [bin_path] command = [bin_path]
else: else:
command = [module.get_bin_path('terraform')] command = [module.get_bin_path('terraform')]
if force_init:
init_plugins(command[0], project_path)
variables_args = [] variables_args = []
for k, v in variables.items(): for k, v in variables.items():
variables_args.extend([ variables_args.extend([

View file

@ -573,7 +573,6 @@ lib/ansible/modules/cloud/misc/rhevm.py E324
lib/ansible/modules/cloud/misc/rhevm.py E325 lib/ansible/modules/cloud/misc/rhevm.py E325
lib/ansible/modules/cloud/misc/serverless.py E324 lib/ansible/modules/cloud/misc/serverless.py E324
lib/ansible/modules/cloud/misc/serverless.py E325 lib/ansible/modules/cloud/misc/serverless.py E325
lib/ansible/modules/cloud/misc/terraform.py E323
lib/ansible/modules/cloud/misc/terraform.py E324 lib/ansible/modules/cloud/misc/terraform.py E324
lib/ansible/modules/cloud/misc/terraform.py E325 lib/ansible/modules/cloud/misc/terraform.py E325
lib/ansible/modules/cloud/misc/virt.py E322 lib/ansible/modules/cloud/misc/virt.py E322