Repository: ansible-collections/community.general

Issue:
nagios module not idempotent
##### SUMMARY

The task below repeatedly results in `changed: true`

```yaml
  - community.general.nagios:
      action: enable_alerts
      host: planc.example.org
      services: HTTP
```

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME

nagios

##### ANSIBLE VERSION
```paste below
shell> ansible --version
ansible 2.9.6
  config file = /export/home/admin.config/.ansible/projects/rp1.example.org/ansible.cfg
  configured module search path = ['/home/admin/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]
```
```
shell> git remote -v
origin	https://github.com/vbotka/community.general.git (fetch)
origin	https://github.com/vbotka/community.general.git (push)
upstream	https://github.com/ansible-collections/community.general.git (fetch)
upstream	https://github.com/ansible-collections/community.general.git (push)

shell> git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean

shell> git rev-parse HEAD
1c538949209058648cd8ddfd6ec78b55667753cd
```

##### CONFIGURATION
```paste below
ANSIBLE_PIPELINING(/export/home/admin.config/.ansible/projects/rp1.example.org/ansible.cfg) = True
DEFAULT_HOST_LIST(/export/home/admin.config/.ansible/projects/rp1.example.org/ansible.cfg) = ['/home/admin/.ansible/projects/rp1.example.org/hosts']
DEFAULT_ROLES_PATH(/export/home/admin.config/.ansible/projects/rp1.example.org/ansible.cfg) = ['/home/admin/.ansible/roles']
DEFAULT_STDOUT_CALLBACK(/export/home/admin.config/.ansible/projects/rp1.example.org/ansible.cfg) = yaml
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /home/admin/.vault_pass.txt
RETRY_FILES_ENABLED(/export/home/admin.config/.ansible/projects/rp1.example.org/ansible.cfg) = False
```

##### OS / ENVIRONMENT
Master:
```
Linux plana 5.4.0-56-generic #62-Ubuntu SMP Mon Nov 23 19:20:19 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
```
Remote (Nagios):
```
FreeBSD rp1.example.org 12.2-STABLE FreeBSD 12.2-STABLE r368787 GENERIC  arm64
```
Host (monitored by Nagios):
```
FreeBSD planc.example.org 10.3-STABLE FreeBSD 10.3-STABLE #2 r301542: Sun Jun 12 08:40:21 CEST 2016
```

##### STEPS TO REPRODUCE

The task below repeatedly results in `changed: true`

```yaml
  - community.general.nagios:
      action: enable_alerts
      host: planc.example.org
      services: HTTP
```

##### EXPECTED RESULTS

`changed: false`

##### ACTUAL RESULTS

(role https://github.com/vbotka/ansible-freebsd-nagios)

```
TASK [vbotka.freebsd_nagios : actions: Debug] ********************************************
ok: [rp1-wlan.example.org] => 
  bsd_nagios_actions:
  - action: enable_alerts
    host: planc.example.org
    services: HTTP

TASK [vbotka.freebsd_nagios : actions: Nagios downtime and notifications] ****************
changed: [rp1-wlan.example.org] => (item=enable_alerts planc.example.org nocommand)

TASK [vbotka.freebsd_nagios : actions: Debug] ********************************************
ok: [rp1-wlan.example.org] => 
  result:
    changed: true
    msg: All items completed
    results:
    - ansible_loop_var: item
      changed: true
      failed: false
      invocation:
        module_args:
          action: enable_alerts
          author: Ansible
          cmdfile: /var/spool/nagios/rw/nagios.cmd
          command: null
          comment: Scheduling downtime
          host: planc.example.org
          minutes: 30
          servicegroup: null
          services: HTTP
          start: null
      item:
        action: enable_alerts
        host: planc.example.org
        services: HTTP
      nagios_commands:
      - '[1609683051] ENABLE_SVC_NOTIFICATIONS;planc.example.org;HTTP'

PLAY RECAP *******************************************************************************
rp1-wlan.example.org: ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
```



Discussion:
Files identified in the description:
None

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 --->
Files identified in the description:
* [`plugins/modules/monitoring/nagios.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/monitoring/nagios.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 --->
cc @tbielawa @tgoetheyn
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Hi @vbotka ,
I am aware that the module is not idempotent.
This is because it is based on "external commands" which is a fire-and-forget mechanism.

To fix this, the whole module should be re-writen to make use of the newer API.
But this API is also limited in functionality, so it is possible not al functionality in the current module can be made available through the API.

Greeting,
Tony
> I am aware that the module is not idempotent.

Others might be interested in learning it too. Wouldn't you mind updating the description of the module? For example

```
The module nagios is not idempotent.
```
Talking about the doc, the statement below is not accurate, I think. Without "delegate_to" the module works as expected. Wouldn't you mind removing this statement?

```
 When using the `nagios' module you will need to specify your Nagios server using the `delegate_to' parameter.
```
feel free to make a pull request and adjust the things that need clarification ;-)
> feel free to make a pull request and adjust the things that need clarification ;-)

I've submitted the PR. Would it be possible to review it? Thank you.

Resolving patch:
diff --git a/plugins/modules/monitoring/nagios.py b/plugins/modules/monitoring/nagios.py
--- a/plugins/modules/monitoring/nagios.py
+++ b/plugins/modules/monitoring/nagios.py
@@ -19,6 +19,7 @@
 short_description: Perform common tasks in Nagios related to downtime and notifications.
 description:
   - "The C(nagios) module has two basic functions: scheduling downtime and toggling alerts for services or hosts."
+  - The C(nagios) module is not idempotent.
   - All actions require the I(host) parameter to be given explicitly. In playbooks you can use the C({{inventory_hostname}}) variable to refer
     to the host the playbook is currently running on.
   - You can specify multiple services at once by separating them with commas, .e.g., C(services=httpd,nfs,puppet).
@@ -26,7 +27,6 @@
     e.g., C(service=host). This keyword may not be given with other services at the same time.
     I(Setting alerts/downtime/acknowledge for a host does not affect alerts/downtime/acknowledge for any of the services running on it.)
     To schedule downtime for all services on particular host use keyword "all", e.g., C(service=all).
-  - When using the C(nagios) module you will need to specify your Nagios server using the C(delegate_to) parameter.
 options:
   action:
     description:


Test patch: