Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform: bugfix: init command when default workspace doesn't exists #5735

Merged

Conversation

dorkamotorka
Copy link
Contributor

@dorkamotorka dorkamotorka commented Dec 25, 2022

SUMMARY

If the terraform config stores its state on a remote backend, it's possible that there are multiple workspaces to choose from. At the same time, remote backends (e.g. Terraform Cloud) are not required to store a default workspace, but one can name and create workspaces as s:he wishes. This is where the Ansible Terraform module has a problem.

Line 331: The terraform init is called but expects the default workspace to exist. Consequently, it should ask you which of the available workspaces you want to choose (which happens if you do terraform init from the CLI) - but that obviously doesn't happen in the Ansible (non-interactive) workflow since the -input=false flag is appended to the terraform init command.
To "avoid" being prompted to choose the workspace and a consequent error, one can choose the Terraform workspace by setting the TF_WORKSPACE environmental variable. Since the Ansible Terraform module already has a workspace variable I just used it to set the TF_WORKSPACE.
Additionally, my opinion is that we should avoid setting the TF_WORKSPACE environmental variable directly in the ansible playbook since it causes confusion about what has precedence in comparison to the workspace variable.

Line 346: "current" workspace gets NEVER appended to the ¨"all"¨ in the workspace_ctx which then causes issues on line 493.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

plugins/modules/terraform.py

@dorkamotorka dorkamotorka changed the title feat: init when default workspace doesn't exists feat: init command when default workspace doesn't exists Dec 25, 2022
@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added cloud feature This issue/PR relates to a feature request module module new_contributor Help guide this first time contributor plugins plugin (any type) labels Dec 25, 2022
@felixfontein felixfontein changed the title feat: init command when default workspace doesn't exists terraform: feat: init command when default workspace doesn't exists Dec 26, 2022
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-6 labels Dec 26, 2022
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! Can you please add a changelog fragment and update the module's documentation accordingly? Thanks.

@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Dec 26, 2022
@github-actions
Copy link

github-actions bot commented Dec 26, 2022

Docs Build 📝

Thank you for contribution!✨

The docsite for this PR is available for download as an artifact from this run:
https://github.com/ansible-collections/community.general/actions/runs/3838042242

File changes:

  • M collections/community/general/terraform_module.html
Click to see the diff comparison.

NOTE: only file modifications are shown here. New and deleted files are excluded.
See the file list and check the published docs to see those files.

diff --git a/home/runner/work/community.general/community.general/docsbuild/base/collections/community/general/terraform_module.html b/home/runner/work/community.general/community.general/docsbuild/head/collections/community/general/terraform_module.html
index e7b5a7e..9018185 100644
--- a/home/runner/work/community.general/community.general/docsbuild/base/collections/community/general/terraform_module.html
+++ b/home/runner/work/community.general/community.general/docsbuild/head/collections/community/general/terraform_module.html
@@ -401,7 +401,7 @@ see <a class="reference internal" href="#ansible-collections-community-general-t
 <div class="ansibleOptionAnchor" id="parameter-workspace"></div><p class="ansible-option-title" id="ansible-collections-community-general-terraform-module-parameter-workspace"><strong>workspace</strong></p>
 <a class="ansibleOptionLink" href="#parameter-workspace" title="Permalink to this option"></a><p class="ansible-option-type-line"><span class="ansible-option-type">string</span></p>
 </div></td>
-<td><div class="ansible-option-cell"><p>The terraform workspace to work with.</p>
+<td><div class="ansible-option-cell"><p>The terraform workspace to work with. This sets the <code class="docutils literal notranslate"><span class="pre">TF_WORKSPACE</span></code> environmental variable that is used to override workspace selection. For more information about workspaces have a look at <a class="reference external" href="https://developer.hashicorp.com/terraform/language/state/workspaces">https://developer.hashicorp.com/terraform/language/state/workspaces</a>.</p>
 <p class="ansible-option-line"><span class="ansible-option-default-bold">Default:</span> <code class="ansible-option-default docutils literal notranslate"><span class="pre">&quot;default&quot;</span></code></p>
 </div></td>
 </tr>

@ansibullbot ansibullbot removed ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Dec 26, 2022
@dorkamotorka
Copy link
Contributor Author

Thanks for your contribution! Can you please add a changelog fragment and update the module's documentation accordingly? Thanks.

Thanks, @felixfontein for the feedback. Feel free to let me know if there's anything else I should do.

Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added two formatting comments. I hope that the module maintainers can take a look at the actual change, as I don't use terraform and are not familiar with the module's logic.

@felixfontein
Copy link
Collaborator

ISSUE TYPE
* Feature Pull Request

One question: the changelog claims this is a bugfix, while you write here it is a feature. What exactly is it now?

@dorkamotorka
Copy link
Contributor Author

ISSUE TYPE
* Feature Pull Request

One question: the changelog claims this is a bugfix, while you write here it is a feature. What exactly is it now?

I fixed it to Bugfix. Thanks for noticing.

@ansibullbot ansibullbot added the bug This issue/PR relates to a bug label Dec 28, 2022
@felixfontein felixfontein added backport-5 and removed feature This issue/PR relates to a feature request labels Dec 28, 2022
@felixfontein felixfontein changed the title terraform: feat: init command when default workspace doesn't exists terraform: bugfix: init command when default workspace doesn't exists Jan 3, 2023
@felixfontein
Copy link
Collaborator

@dorkamotorka did you maybe forgot to push an update?

@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Jan 3, 2023
@dorkamotorka
Copy link
Contributor Author

@felixfontein I don't understand what you mean. This PR is ready to get merged, but as far as I understood I should wait for the maintaners approval?

@felixfontein
Copy link
Collaborator

@dorkamotorka I left several comments on the changelog fragment and documentation above, which you marked as resolved without applying any change.

@ansibullbot ansibullbot removed the stale_ci CI is older than 7 days, rerun before merging label Jan 4, 2023
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added merge_commit This PR contains at least one merge commit. Please resolve! needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html labels Jan 4, 2023
@ansibullbot ansibullbot removed merge_commit This PR contains at least one merge commit. Please resolve! needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html labels Jan 4, 2023
@dorkamotorka
Copy link
Contributor Author

@felixfontein hey, sorry for missing that. I think now it should be fine.

Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Jan 7, 2023
@felixfontein felixfontein merged commit fc2b1aa into ansible-collections:main Jan 7, 2023
@patchback
Copy link

patchback bot commented Jan 7, 2023

Backport to stable-5: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-5/fc2b1aac4aca701f3e87706266ac2e32a905d9fe/pr-5735

Backported as #5776

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Jan 7, 2023
…#5735)

* feat: init when default workspace doesn't exists

* doc: add changelogs fragment and docs update

* fix: changelog formating fix

(cherry picked from commit fc2b1aa)
@patchback
Copy link

patchback bot commented Jan 7, 2023

Backport to stable-6: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-6/fc2b1aac4aca701f3e87706266ac2e32a905d9fe/pr-5735

Backported as #5777

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@felixfontein
Copy link
Collaborator

@dorkamotorka thanks for fixing this!
@russoz thanks for reviewing!

patchback bot pushed a commit that referenced this pull request Jan 7, 2023
…#5735)

* feat: init when default workspace doesn't exists

* doc: add changelogs fragment and docs update

* fix: changelog formating fix

(cherry picked from commit fc2b1aa)
felixfontein pushed a commit that referenced this pull request Jan 7, 2023
…d when default workspace doesn't exists (#5777)

terraform: bugfix: init command when default workspace doesn't exists (#5735)

* feat: init when default workspace doesn't exists

* doc: add changelogs fragment and docs update

* fix: changelog formating fix

(cherry picked from commit fc2b1aa)

Co-authored-by: Teodor Janez Podobnik <48418580+dorkamotorka@users.noreply.github.com>
felixfontein pushed a commit that referenced this pull request Jan 7, 2023
…d when default workspace doesn't exists (#5776)

terraform: bugfix: init command when default workspace doesn't exists (#5735)

* feat: init when default workspace doesn't exists

* doc: add changelogs fragment and docs update

* fix: changelog formating fix

(cherry picked from commit fc2b1aa)

Co-authored-by: Teodor Janez Podobnik <48418580+dorkamotorka@users.noreply.github.com>
@MehuiSeklayr
Copy link
Contributor

Hi lads :)

I recently upgraded community.general in one of our project, and I believe this PR didn't solve a bug but actually introduced one: the terraform module is now unable to create workspaces when they do not exist.

Initialisation happens on line 498, and this PR includes the workspace right in it, but creation of said workspace only happens on line 503. Hence for our playbooks to work, we now have to manually create workspaces before initialisation.

I believe this is both a breaking change and something that is quite contrary to the way Terraform actually works.
Although I understand @dorkamotorka's use case, could we revert this change until a more battle-proven solution is on the table ?

@felixfontein
Copy link
Collaborator

@dorkamotorka could you please take a look at the above message?
@m-yosefpor @rainerleber as module maintainers, what do you think about the above message?

@felixfontein
Copy link
Collaborator

I created a new issue for this: #6101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug cloud module module new_contributor Help guide this first time contributor plugins plugin (any type)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants