Repository: ansible-collections/community.general

Issue:
`~` gets evaluated when using `community.general.osx_defaults`
### Summary

When passing `~` as value to the `community.general.osx_defaults` plugin, it gets directly evaluated to `/Users/pavelzwerschke`. 

This is probably because the value [is not passed](https://github.com/ansible-collections/community.general/blob/82d69bb871c9b8b61423090af54783f0ae78a189/plugins/modules/system/osx_defaults.py#L307) with quotes to the command line.

When doing it manually, we get the same problem:
```console
$ defaults write com.googlecode.iterm2 PrefsCustomFolder ~/.config/iterm2
$ defaults read com.googlecode.iterm2 PrefsCustomFolder
/Users/pavelzwerschke/.config/iterm2
```

Instead, we want ansible to do something like this:
```console
$ defaults write com.googlecode.iterm2 PrefsCustomFolder '~/.config/iterm2'
$ defaults read com.googlecode.iterm2 PrefsCustomFolder
~/.config/iterm2
```

Also, when executing the playbook twice, it still says `changed` instead of `ok` the second time because the string comparison is still evaluated to `false`.

### Issue Type

Bug Report

### Component Name

community.general.osx_defaults

### Ansible Version

```console (paste below)
$ ansible --version
ansible [core 2.13.3]
  config file = /Users/pavelzwerschke/Projekte/ansible-provision/ansible.cfg
  configured module search path = ['/Users/pavelzwerschke/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/Caskroom/mambaforge/base/envs/ansible/lib/python3.10/site-packages/ansible
  ansible collection location = /Users/pavelzwerschke/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/Caskroom/mambaforge/base/envs/ansible/bin/ansible
  python version = 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:41:22) [Clang 13.0.1 ]
  jinja version = 3.1.2
  libyaml = True
```


### Community.general Version

```console (paste below)
$ ansible-galaxy collection list community.general
# /Users/pavelzwerschke/.ansible/collections/ansible_collections
Collection        Version
----------------- -------
community.general 5.5.0

# /opt/homebrew/Caskroom/mambaforge/base/envs/ansible/lib/python3.10/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 5.5.0
```


### Configuration

```console (paste below)
$ ansible-config dump --only-changed
DEFAULT_HOST_LIST(/Users/pavelzwerschke/Projekte/ansible-provision/ansible.cfg) = ['/Users/pavelzwerschke/Projekte/ansible-provision/inventory']
```


### OS / Environment

macOS 12.5.1

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
---
- hosts: all
  tasks:
    - name: Ensure iTerm2 custom preferences location is set.
      community.general.osx_defaults:
        domain: com.googlecode.iterm2
        key: PrefsCustomFolder
        value: "~/.config/iterm2"
        type: string
        state: present

```


### Expected Results

Expected:
```console
$ defaults read com.googlecode.iterm2 PrefsCustomFolder
~/.config/iterm2
```

### Actual Results

```console (paste below)
$ defaults read com.googlecode.iterm2 PrefsCustomFolder
/Users/pavelzwerschke/.config/iterm2
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct
`~` gets evaluated when using `community.general.osx_defaults`
### Summary

When passing `~` as value to the `community.general.osx_defaults` plugin, it gets directly evaluated to `/Users/pavelzwerschke`. 

This is probably because the value [is not passed](https://github.com/ansible-collections/community.general/blob/82d69bb871c9b8b61423090af54783f0ae78a189/plugins/modules/system/osx_defaults.py#L307) with quotes to the command line.

When doing it manually, we get the same problem:
```console
$ defaults write com.googlecode.iterm2 PrefsCustomFolder ~/.config/iterm2
$ defaults read com.googlecode.iterm2 PrefsCustomFolder
/Users/pavelzwerschke/.config/iterm2
```

Instead, we want ansible to do something like this:
```console
$ defaults write com.googlecode.iterm2 PrefsCustomFolder '~/.config/iterm2'
$ defaults read com.googlecode.iterm2 PrefsCustomFolder
~/.config/iterm2
```

Also, when executing the playbook twice, it still says `changed` instead of `ok` the second time because the string comparison is still evaluated to `false`.

### Issue Type

Bug Report

### Component Name

community.general.osx_defaults

### Ansible Version

```console (paste below)
$ ansible --version
ansible [core 2.13.3]
  config file = /Users/pavelzwerschke/Projekte/ansible-provision/ansible.cfg
  configured module search path = ['/Users/pavelzwerschke/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/Caskroom/mambaforge/base/envs/ansible/lib/python3.10/site-packages/ansible
  ansible collection location = /Users/pavelzwerschke/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/Caskroom/mambaforge/base/envs/ansible/bin/ansible
  python version = 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:41:22) [Clang 13.0.1 ]
  jinja version = 3.1.2
  libyaml = True
```


### Community.general Version

```console (paste below)
$ ansible-galaxy collection list community.general
# /Users/pavelzwerschke/.ansible/collections/ansible_collections
Collection        Version
----------------- -------
community.general 5.5.0

# /opt/homebrew/Caskroom/mambaforge/base/envs/ansible/lib/python3.10/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 5.5.0
```


### Configuration

```console (paste below)
$ ansible-config dump --only-changed
DEFAULT_HOST_LIST(/Users/pavelzwerschke/Projekte/ansible-provision/ansible.cfg) = ['/Users/pavelzwerschke/Projekte/ansible-provision/inventory']
```


### OS / Environment

macOS 12.5.1

### Steps to Reproduce

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
---
- hosts: all
  tasks:
    - name: Ensure iTerm2 custom preferences location is set.
      community.general.osx_defaults:
        domain: com.googlecode.iterm2
        key: PrefsCustomFolder
        value: "~/.config/iterm2"
        type: string
        state: present

```


### Expected Results

Expected:
```console
$ defaults read com.googlecode.iterm2 PrefsCustomFolder
~/.config/iterm2
```

### Actual Results

```console (paste below)
$ defaults read com.googlecode.iterm2 PrefsCustomFolder
/Users/pavelzwerschke/.config/iterm2
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:
Files identified in the description:
* [`plugins/modules/system/osx_defaults.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/system/osx_defaults.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 @!UNKNOWN @Akasurde @danieljaouen @indrajitr @kyleabenson @martinm82 @notok
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
The problem is that `expand_user_and_vars=False` needs to be passed to `module.run_command()` (https://github.com/ansible-collections/community.general/blob/main/plugins/modules/system/osx_defaults.py#L307). The default value for that flag is unfortunately `True`.
Would it break anything to set the flag to `False` there?
I think that should be ok. (Every change breaks something: https://xkcd.com/1172/)
Files identified in the description:
* [`plugins/modules/system/osx_defaults.py`](https://github.com/ansible-collections/community.general/blob/main/plugins/modules/system/osx_defaults.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 @!UNKNOWN @Akasurde @danieljaouen @indrajitr @kyleabenson @martinm82 @notok
[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: notify --->
The problem is that `expand_user_and_vars=False` needs to be passed to `module.run_command()` (https://github.com/ansible-collections/community.general/blob/main/plugins/modules/system/osx_defaults.py#L307). The default value for that flag is unfortunately `True`.
Would it break anything to set the flag to `False` there?
I think that should be ok. (Every change breaks something: https://xkcd.com/1172/)

Resolving patch:
diff --git a/plugins/modules/system/osx_defaults.py b/plugins/modules/system/osx_defaults.py
--- a/plugins/modules/system/osx_defaults.py
+++ b/plugins/modules/system/osx_defaults.py
@@ -304,7 +304,8 @@ def write(self):
         if not isinstance(value, list):
             value = [value]
 
-        rc, out, err = self.module.run_command(self._base_command() + ['write', self.domain, self.key, '-' + self.type] + value)
+        rc, out, err = self.module.run_command(self._base_command() + ['write', self.domain, self.key, '-' + self.type] + value,
+                                               expand_user_and_vars=False)
 
         if rc != 0:
             raise OSXDefaultsException('An error occurred while writing value to defaults: %s' % out)


Test patch: