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?
- This parameter changes the operation of the module slightly;
        C(insertbefore) and C(insertafter) will be ignored, and if the C(regexp)
        does not match anywhere in the file, the file will be left unchanged.
      - If the C(regexp) does match, the last matching line will be replaced by
        the expanded line parameter.
    type: bool
    default: no
    version_added: "1.1"
  insertafter:
    description:
      - Used with C(state=present).
      - If specified, the line will be inserted after the last match of specified regular expression.
      - If the first match is required, use(firstmatch=yes).
      - A special value is available; C(EOF) for inserting the line at the end of the file.
      - If specified regular expression has no matches, EOF will be used instead.
      - If C(insertbefore) is set, default value C(EOF) will be ignored.
      - If regular expressions are passed to both C(regexp) and C(insertafter), C(insertafter) is only honored if no match for C(regexp) is found.
      - May not be used with C(backrefs) or C(insertbefore).
    type: str
    choices: [ EOF, '*regex*' ]
    default: EOF
  insertbefore:
    description:
      - Used with C(state=present).
      - If specified, the line will be inserted before the last match of specified regular expression.
      - If the first match is required, use C(firstmatch=yes).
      - A value is available; C(BOF) for inserting the line at the beginning of the file.
      - If specified regular expression has no matches, the line will be inserted at the end of the file.
      - If regular expressions are passed to both C(regexp) and C(insertbefore), C(insertbefore) is only honored if no match for C(regexp) is found.
      - May not be used with C(backrefs) or C(insertafter).