Repository: ansible-collections/community.general

Issue:
terraform module reporting incorrect changed status in certain conditions
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
Under certain conditions, the `terraform` module will report a status of `changed: False` when resources have actually been changed. I have identified the source of the problem. To check if resources have been added/removed, the module checks that a combination of the following patterns are not present in the module's output depending on the task's action (present/absent), `0 added, 0 changed` or `0 destroyed`. This works fine until you have a multiple of 10 resources added/destroyed, and the pattern matches these. For example, adding 10 resources will cause the module's test to match the pattern `0 added, 0 changed` amongst the output string  of `Resources: 10 added, 0 changed`. I will submit a PR on the back of this issue.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
`plugins/modules/cloud/misc/terraform.py`

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.9.10
  config file = /mnt/c/code/cloud-gitlab/ansible.cfg
  configured module search path = ['/home/golds_a/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/golds_a/venvs/ansible2.9/lib/python3.8/site-packages/ansible
  executable location = /home/golds_a/venvs/ansible2.9/bin/ansible
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below
ANSIBLE_SSH_ARGS(/mnt/c/code/cloud-gitlab/ansible.cfg) = -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
DEFAULT_HOST_LIST(/mnt/c/code/cloud-gitlab/ansible.cfg) = ['/mnt/c/code/cloud-gitlab/inv.d']
DEFAULT_ROLES_PATH(/mnt/c/code/cloud-gitlab/ansible.cfg) = ['/mnt/c/code/cloud-gitlab/roles']
HOST_KEY_CHECKING(/mnt/c/code/cloud-gitlab/ansible.cfg) = False
```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->
Reproducible on various platforms:
WSL 1 running Ubuntu 20.04 (hosted on Windows 10)
CentOS Linux release 7.8.2003 (Core)

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->
Add or destroy any multiple of 10 resources using the `terraform` module.
<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Create 10 GCP instances
  terraform:
    project_path: '{{ project_dir }}'
    state: present
```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
```
TASK [Create 10 GCP instances] ***********
changed: [localhost]
```

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes -->
```paste below
TASK [Create 10 GCP instances] ***********
ok: [localhost]
```



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

If these files are inaccurate, 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/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->

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
@@ -368,7 +368,7 @@ def main():
     if needs_application and not module.check_mode and not state == 'planned':
         rc, out, err = module.run_command(command, cwd=project_path)
         # checks out to decide if changes were made during execution
-        if '0 added, 0 changed' not in out and not state == "absent" or '0 destroyed' not in out:
+        if ' 0 added, 0 changed' not in out and not state == "absent" or ' 0 destroyed' not in out:
             changed = True
         if rc != 0:
             module.fail_json(


Test patch: