python-3.xansibleterraformansible-galaxy

Ansible failing to run Terraform Plan


I have automated ansible playbook to generate Terraform files required to create AWS resources in a folder. Once the files are generated, ansible tasks runs terraform plan(using below plan code) but it is failing with below error(error)

Ansible Version:

ansible [core 2.11.2] 
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible

Terraform Version: Terraform v0.12.31

plan code:

- name: Create Terraform plan
  check_mode: true
  community.general.terraform:
    project_path: "{{ terraform_dir }}"
    plan_file: "{{ terraform_dir }}/tfplan"
    state: "planned"
    force_init: true
  register: plan
  tags:
    - build
    - plan
    - never

error:

**fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):
  File \"/root/.ansible/tmp/ansible-tmp-1625160772.6764412-713-181137940992596/AnsiballZ_terraform.py\", line 100, in <module>
    _ansiballz_main()
  File \"/root/.ansible/tmp/ansible-tmp-1625160772.6764412-713-181137940992596/AnsiballZ_terraform.py\", line 92, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File \"/root/.ansible/tmp/ansible-tmp-1625160772.6764412-713-181137940992596/AnsiballZ_terraform.py\", line 40, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.general.plugins.modules.terraform', init_globals=dict(_module_fqn='ansible_collections.community.general.plugins.modules.terraform', _modlib_path=modlib_path),
  File \"/usr/lib/python3.8/runpy.py\", line 207, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File \"/usr/lib/python3.8/runpy.py\", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File \"/usr/lib/python3.8/runpy.py\", line 87, in _run_code
    exec(code, run_globals)
  File \"/tmp/ansible_community.general.terraform_payload_xnzkbjkr/ansible_community.general.terraform_payload.zip/ansible_collections/community/general/plugins/modules/terraform.py\", line 497, in <module>
  File \"/tmp/ansible_community.general.terraform_payload_xnzkbjkr/ansible_community.general.terraform_payload.zip/ansible_collections/community/general/plugins/modules/terraform.py\", line 393, in main
  File \"/tmp/ansible_community.general.terraform_payload_xnzkbjkr/ansible_community.general.terraform_payload.zip/ansible_collections/community/general/plugins/modules/terraform.py\", line 238, in get_version
  File \"/usr/lib/python3.8/json/__init__.py\", line 357, in loads
    return _default_decoder.decode(s)
  File \"/usr/lib/python3.8/json/decoder.py\", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File \"/usr/lib/python3.8/json/decoder.py\", line 355, in raw_decode
    raise JSONDecodeError(\"Expecting value\", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
", "module_stdout": "", "msg": "MODULE FAILURE
See stdout/stderr for the exact error", "rc": 1}**

not sure if I missed to install any required modules or if I need to make code changes. Any idea how to fix the above error?

Please let me know if any further information needed.

Thanks in Advance.


Solution

  • Fixed the above error by changing the ansible-galaxy collection version(ansible-galaxy collection install community.general:1.3.9).

    ansible-galaxy versions causing the above error are >1.3.9 as versions greater than 1.3.9 are not compatible with terraform version v0.12.31

    Previously I have ansible-galaxy collection version 2.2.0 installed, degrading the version to 1.3.9 fixed my issue.