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 module - ignores state_file when state=absent #6490

Open
1 task done
kemopq opened this issue May 4, 2023 · 4 comments
Open
1 task done

Terraform module - ignores state_file when state=absent #6490

kemopq opened this issue May 4, 2023 · 4 comments
Labels
bug This issue/PR relates to a bug has_pr module module plugins plugin (any type)

Comments

@kemopq
Copy link

kemopq commented May 4, 2023

Summary

When using terraform module with state=present with defined state_file, the resource are created and terraform state is written to specified state_file.
ok: [localhost] => {
"terraform_output": {
"changed": true,
"command": "/usr/bin/terraform apply -no-color -input=false -auto-approve -lock=true /tmp/tmp5q4lttoj.tfplan",

When using terraform module with state=absent with defined state_file, no resource are destroyed as terraform state file is ignored.
ok: [localhost] => {
"terraform_output": {
"changed": false,
"command": "/usr/bin/terraform destroy -no-color -auto-approve -lock=true -var-file /opt/terraform/vm_opvm/vm_destroy.tfvars.json",
"failed": false,
"stdout": "\nNo changes. No objects need to be destroyed.\n\nEither you have not created any objects yet or the existing objects were\nalready deleted outside of Terraform.\n\nDestroy complete! Resources: 0 destroyed.\n",

Solution proposal:
In a file terraform.py add "command.extend(_state_args(state_file))" to the line 610:
*** 607,613 ****

  if state == 'absent':
      command.extend(variables_args)
  •     command.extend(_state_args(state_file))
    elif state == 'present' and plan_file:
        if any([os.path.isfile(project_path + "/" + plan_file), os.path.isfile(plan_file)]):
            command.append(plan_file)
    

--- 607,612 ----

Issue Type

Bug Report

Component Name

terraform

Ansible Version

$ ansible --version
ansible [core 2.14.5]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/kpog/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/kpog/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True

Community.general Version

$ ansible-galaxy collection list community.general
# /usr/lib/python3/dist-packages/ansible_collections
Collection        Version
----------------- -------
community.general 6.6.0  

Configuration

$ ansible-config dump --only-changed
CONFIG_FILE() = /etc/ansible/ansible.cfg

OS / Environment

Ubuntu22.04

Steps to Reproduce

- name: Creating VM by terraform
  community.general.terraform:
    project_path: "{{ terraform_project_path }}"
    state: present
    state_file: "{{ file_stat.stat.exists | ternary(terraform_vm_path + '/terraform.tfstate', omit) }}"
    variables_files:
      - "{{ terraform_vm_path }}/vm_deploy.tfvars.json"
    force_init: false
  register: terraform_output

- name: Destroying VM by terraform
  terraform:
    project_path: "{{ terraform_project_path }}"
    state: absent
    state_file: "{{ file_stat.stat.exists | ternary(terraform_vm_path + '/terraform.tfstate', omit) }}"
    variables_files:
      - "{{ terraform_vm_path }}/vm_destroy.tfvars.json"
    force_init: false
  register: terraform_output

Expected Results

The resource will be destroyed when running terraform module with state=absent

Actual Results

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibullbot
Copy link
Collaborator

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added bug This issue/PR relates to a bug module module plugins plugin (any type) labels May 4, 2023
@felixfontein
Copy link
Collaborator

Could this be related to #6296? CC @LanceNero @russoz

@LanceNero
Copy link
Contributor

yes, I've checked code for this issue, seems it does exist, the argument "state_file" will not work when the state="absent", will try to fix it later.

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 has_pr module module plugins plugin (any type)
Projects
None yet
Development

No branches or pull requests

4 participants