The replace and lineinfile modules do not allow for temporary directories to be set with environment variables. See https://github.com/ansible/ansible/issues/24082 for a similar bug report.

!component =lib/ansible/modules/files/lineinfile.py

However, this is for the facts directory. Here's how it is reproducible:

On the target server we are trying to write to `/etc/ansible/facts.d/cloudera.fact`:
```
# ls -al / | grep etc
drwxr-xr-x. 151 root     root         12288 Mar  9 08:38 etc
# ls -al /etc | grep ansible
drwxr-xr-x    3 root         root           4096 Nov 22  2016 ansible
# ls -al /etc/ansible/ | grep 'facts.d'
drwxr-xr-x    2 root root  4096 Mar 13 11:53 facts.d
# ls -al /etc/ansible/facts.d/ | grep cloudera.fact
-rw-rw-r--  1 cloudera-scm cloudera-scm   10 Mar 13 11:54 cloudera.fact
```

Here is the playbook:
```
cat playbooks/test_svcacct_lineinfile.yml
- hosts: all
  become: True
  become_user: cloudera-scm
  gather_facts: False
  tasks:
    - name: Add lineinfile
      lineinfile:
        line: 'key=value2'
        state: present
        create: False
        path: '/etc/ansible/facts.d/cloudera.fact'
```

This will fail since there is no way to override the directory that lineinfile hard-codes the destination directory as the one which has the file which is being edited, instead of `remote_tmp`  or any of the `['TMP', 'TEMP', 'TMPDIR']` environment variables:

https://github.com/ansible/ansible-modules-core/blob/devel/files/lineinfile.py#L213

which calls

https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/basic.py#L2238

Hardcoding the `dir` param as the argument to the `tempfile.mkstemp()` call.

Is there a way that this can be changed to be able to set a custom temp dir?
- For C(state=present), the pattern to replace if found. Only the last line found will be replaced.
      - For C(state=absent), the pattern of the line(s) to remove.
      - If the regular expression is not matched, the line will be
        added to the file in keeping with C(insertbefore) or C(insertafter)
        settings.
      - When modifying a line the regexp should typically match both the initial state of
        the line as well as its state after replacement by C(line) to ensure idempotence.
      - Uses Python regular expressions. See U(http://docs.python.org/2/library/re.html).
    type: str
    aliases: [ regex ]
    version_added: '1.7'
  state:
    description:
      - Whether the line should be there or not.
    type: str
    choices: [ absent, present ]
    default: present
  line:
    description:
      - The line to insert/replace into the file.
      - Required for C(state=present).
      - If C(backrefs) is set, may contain backreferences that will get
        expanded with the C(regexp) capture groups if the regexp matches.
    type: str
    aliases: [ value ]
  backrefs:
    description:
      - Used with C(state=present).
      - If set, C(line) can contain backreferences (both positional and named)
        that will get populated if the C(regexp) matches.