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 modules does not used supplied parameters in planned state #3707

Closed
1 task done
egnirra opened this issue Nov 12, 2021 · 3 comments · Fixed by #3726
Closed
1 task done

Terraform modules does not used supplied parameters in planned state #3707

egnirra opened this issue Nov 12, 2021 · 3 comments · Fixed by #3726
Labels
bug This issue/PR relates to a bug cloud module module plugins plugin (any type)

Comments

@egnirra
Copy link
Contributor

egnirra commented Nov 12, 2021

Summary

Setting State: Planned
Does not use other command options such as lock: true or lock_timeout: 10

Issue Type

Bug Report

Component Name

terraform.py

Ansible Version

$ ansible --version
ansible [core 2.11.6]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/thomas/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/thomas/.local/lib/python3.8/site-packages/ansible
  ansible collection location = /home/thomas/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/thomas/.local/bin/ansible
  python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
  jinja version = 3.0.3
  libyaml = True

Community.general Version

$ ansible-galaxy collection list community.general

# /home/thomas/.local/lib/python3.8/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 3.8.1

# /home/thomas/.ansible/collections/ansible_collections
Collection        Version
----------------- -------
community.general 4.0.1

Configuration

$ ansible-config dump --only-changed

OS / Environment

Ubuntu, Windows 10/11, RHEL 7/8

Steps to Reproduce

- hosts: localhost
  gather_facts: false
  tasks:
    - name: Basic plan
      community.general.terraform:
        project_path: 'path where main.tf is located'
        state: planned
        lock: yes
        force_init: yes
        lock_timeout: 1000
        plan_file: "path where main.tf is located"/plan.tf
      register: plan

If you run the above playbook at the same time as the locking variables will not be used in the "Plan" stage.

Expected Results

The file is locked and it waits for it to be unlocked.

Actual Results

PLAY [localhost] *****************************************************************************************************************************************************************************************************************************

TASK [plan] **********************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Terraform plan could not be created\r\nSTDOUT: \r\n\r\nSTDERR: \nError: Error acquiring the state lock\n\nError message: resource temporarily unavailable\nLock Info:\n  ID:        b5f44b8e-3b7b-882d-c8b6-eb2ab9240815\n  Path:      terraform.tfstate\n  Operation: OperationTypePlan\n  Who:       thomas@DESKTOP-I3KQLV1\n  Version:   1.0.10\n  Created:   2021-11-12 18:31:16.804241946 +0000 UTC\n  Info:      \n\n\nTerraform acquires a state lock to protect the state from being written\nby multiple users at the same time. Please resolve the issue above and try\nagain. For most commands, you can disable locking with the \"-lock=false\"\nflag, but this is not recommended.\n"}

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

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 cloud module module plugins plugin (any type) labels Nov 12, 2021
@egnirra
Copy link
Contributor Author

egnirra commented Nov 12, 2021

Here is the code for build_plan, as you can see, the variable "command" is not actually used for running a plan.
The only options added to the plan command are those stated in "plan_command"
Other options such as -lock=true etc are only used when the Plan command is run later in the code.

def build_plan(command, project_path, variables_args, state_file, targets, state, plan_path=None):
    if plan_path is None:
        f, plan_path = tempfile.mkstemp(suffix='.tfplan')

    plan_command = [command[0], 'plan', '-input=false', '-no-color', '-detailed-exitcode', '-out', plan_path]

    for t in targets:
        plan_command.extend(['-target', t])

    plan_command.extend(_state_args(state_file))

    rc, out, err = module.run_command(plan_command + variables_args, cwd=project_path)

    if rc == 0:
        # no changes
        return plan_path, False, out, err, plan_command if state == 'planned' else command
    elif rc == 1:
        # failure to plan
        module.fail_json(msg='Terraform plan could not be created\r\nSTDOUT: {0}\r\n\r\nSTDERR: {1}'.format(out, err))
    elif rc == 2:
        # changes, but successful
        return plan_path, True, out, err, plan_command if state == 'planned' else command

    module.fail_json(msg='Terraform plan failed with unexpected exit code {0}. \r\nSTDOUT: {1}\r\n\r\nSTDERR: {2}'.format(rc, out, err))

It can be "solved" with the changes below, however, I don't feel like this is the best solution?

def build_plan(command, project_path, variables_args, state_file, targets, state, APPLY_ARGS, plan_path=None):
    if plan_path is None:
        f, plan_path = tempfile.mkstemp(suffix='.tfplan')

    local_command = command.copy()

    plan_command = [command[0], 'plan', '-input=false', '-no-color', '-detailed-exitcode', '-out', plan_path]

    if state == "planned":
        for c in local_command[1:]:
            plan_command.insert(2, c)

    if state == "present":
        for a in APPLY_ARGS:
            local_command.remove(a)
        for c in local_command[1:]:
            plan_command.insert(2, c)

    for t in targets:
        plan_command.extend(['-target', t])

    plan_command.extend(_state_args(state_file))

    rc, out, err = module.run_command(plan_command + variables_args, cwd=project_path)

    if rc == 0:
        # no changes
        return plan_path, False, out, err, plan_command if state == 'planned' else command
    elif rc == 1:
        # failure to plan
        module.fail_json(msg='Terraform plan could not be created\r\nSTDOUT: {0}\r\n\r\nSTDERR: {1}'.format(out, err))
    elif rc == 2:
        # changes, but successful
        return plan_path, True, out, err, plan_command if state == 'planned' else command

    module.fail_json(msg='Terraform plan failed with unexpected exit code {0}. \r\nSTDOUT: {1}\r\n\r\nSTDERR: {2}'.format(rc, out, err))

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

Successfully merging a pull request may close this issue.

2 participants