Repository: ansible-collections/community.general

Issue:
Terraform error is unreadable
### Summary

Ansible present terraform error in not readable state.
There is some weird escaping ...

```
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Basic deploy of a service] *****************************************************************************************************************

TASK [Run TF] ************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "cmd": "/home/findmyname/bin/terraform validate", "msg": "\u001b[31m╷\u001b[0m\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m\u001b[1m\u001b[31mError: \u001b[0m\u001b[0m\u001b[1mUnsupported attribute\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m\u001b[0m  on providers.tf line 3, in provider \"aws\":\n\u001b[31m│\u001b[0m \u001b[0m   3:   shared_credentials_file = local.cfg.aws\u001b[4m.shared_credentials_file\u001b[0m\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m    \u001b[90m├────────────────\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m\u001b[0m    \u001b[90m│\u001b[0m \u001b[1mlocal.cfg.aws\u001b[0m is object with 6 attributes\n\u001b[31m│\u001b[0m \u001b[0m\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0mThis object does not have an attribute named \"shared_credentials_file\".\n\u001b[31m╵\u001b[0m\u001b[0m", "rc": 1, "stderr": "\u001b[31m╷\u001b[0m\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m\u001b[1m\u001b[31mError: \u001b[0m\u001b[0m\u001b[1mUnsupported attribute\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m\u001b[0m  on providers.tf line 3, in provider \"aws\":\n\u001b[31m│\u001b[0m \u001b[0m   3:   shared_credentials_file = local.cfg.aws\u001b[4m.shared_credentials_file\u001b[0m\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m    \u001b[90m├────────────────\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0m\u001b[0m    \u001b[90m│\u001b[0m \u001b[1mlocal.cfg.aws\u001b[0m is object with 6 attributes\n\u001b[31m│\u001b[0m \u001b[0m\u001b[0m\n\u001b[31m│\u001b[0m \u001b[0mThis object does not have an attribute named \"shared_credentials_file\".\n\u001b[31m╵\u001b[0m\u001b[0m\n", "stderr_lines": ["\u001b[31m╷\u001b[0m\u001b[0m", "\u001b[31m│\u001b[0m \u001b[0m\u001b[1m\u001b[31mError: \u001b[0m\u001b[0m\u001b[1mUnsupported attribute\u001b[0m", "\u001b[31m│\u001b[0m \u001b[0m", "\u001b[31m│\u001b[0m \u001b[0m\u001b[0m  on providers.tf line 3, in provider \"aws\":", "\u001b[31m│\u001b[0m \u001b[0m   3:   shared_credentials_file = local.cfg.aws\u001b[4m.shared_credentials_file\u001b[0m\u001b[0m", "\u001b[31m│\u001b[0m \u001b[0m    \u001b[90m├────────────────\u001b[0m", "\u001b[31m│\u001b[0m \u001b[0m\u001b[0m    \u001b[90m│\u001b[0m \u001b[1mlocal.cfg.aws\u001b[0m is object with 6 attributes", "\u001b[31m│\u001b[0m \u001b[0m\u001b[0m", "\u001b[31m│\u001b[0m \u001b[0mThis object does not have an attribute named \"shared_credentials_file\".", "\u001b[31m╵\u001b[0m\u001b[0m"], "stdout": "", "stdout_lines": []}

PLAY RECAP ***************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
```

Terraform error:

```
Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v3.70.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
2023-01-10T21:43:58.853+0100 [ERROR] vertex "provider[\"registry.terraform.io/hashicorp/aws\"]" error: Unsupported attribute
╷
│ Error: Unsupported attribute
│ 
│   on providers.tf line 3, in provider "aws":
│    3:   shared_credentials_file = local.cfg.aws.shared_credentials_file
│     ├────────────────
│     │ local.cfg.aws is object with 6 attributes
│ 
│ This object does not have an attribute named "shared_credentials_file".
```

### Issue Type

Bug Report

### Component Name

terraform

### Ansible Version

```console (paste below)
└──╼$ ansible --version
ansible [core 2.14.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/findmyname/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.11/site-packages/ansible
  ansible collection location = /home/findmyname/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.1 (main, Dec  7 2022, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
```


### Community.general Version

```console (paste below)
└──╼$ ansible-galaxy collection list community.general

# /usr/lib/python3.11/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 6.1.0
```


### Configuration

```console (paste below)
└──╼$ ansible-config dump --only-changed
CONFIG_FILE() = /etc/ansible/ansible.cfg
```


### OS / Environment

Fedora 37

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
I would say it can be any error e.g. you can use invalid configuration. In my case I renamed attribute on var local.cfg.aws. 

### Expected Results

I expect to receive error which can be read and understood.

### Actual Results

_No response_

### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/terraform.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/terraform.py)

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](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @m-yosefpor @rainerleber
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@findmyname666 This weird output is ANSI escape codes since validation step did not include `-no-color` argument during validation.

Fixed in https://github.com/ansible-collections/community.general/pull/5843

Resolving patch:
diff --git a/plugins/modules/cloud/misc/terraform.py b/plugins/modules/cloud/misc/terraform.py
--- a/plugins/modules/cloud/misc/terraform.py
+++ b/plugins/modules/cloud/misc/terraform.py
@@ -299,9 +299,9 @@ def preflight_validation(bin_path, project_path, version, variables_args=None, p
     if not os.path.isdir(project_path):
         module.fail_json(msg="Path for Terraform project '{0}' doesn't exist on this host - check the path and try again please.".format(project_path))
     if LooseVersion(version) < LooseVersion('0.15.0'):
-        rc, out, err = module.run_command([bin_path, 'validate'] + variables_args, check_rc=True, cwd=project_path)
+        module.run_command([bin_path, 'validate', '-no-color'] + variables_args, check_rc=True, cwd=project_path)
     else:
-        rc, out, err = module.run_command([bin_path, 'validate'], check_rc=True, cwd=project_path)
+        module.run_command([bin_path, 'validate', '-no-color'], check_rc=True, cwd=project_path)
 
 
 def _state_args(state_file):


Test patch: