ansible / ansible-modules-extras

Ansible extra modules - these modules ship with ansible
947 stars 1.47k forks source link

Fix run cronvar as current unprivileged user #3687

Closed mkwardakov closed 7 years ago

mkwardakov commented 7 years ago
ISSUE TYPE
COMPONENT NAME
ANSIBLE VERSION
ansible 2.3.0
  config file = /home/michael/.ansible.cfg
  configured module search path = Default w/o overrides
SUMMARY

Got bug in case when module is run as uprivileged user: it always fails because cron requires sudo when -u option is specified. When user: parameter is not set, cronvar tries to make changes for root.

So I made it behaves exactly like cron module when handling self.user.

playbook:

$ cat a.yml
---
- hosts: ansible
  tasks:
    - name: Add var to crontab
      cronvar:
        name: "TEST"
        value: "Test"

before:

Using /home/michael/.ansible.cfg as config file

PLAYBOOK: a.yml ****************************************************************
1 plays in a.yml

PLAY [ansible] *****************************************************************

TASK [setup] *******************************************************************
...
ok: [localhost]

TASK [Add var to crontab] ******************************************************
task path: /home/michael/repos/ansible/a.yml:4
...
fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_args": {
            "backup": false,
            "cron_file": null,
            "insertafter": null,
            "insertbefore": null,
            "name": "TEST",
            "state": "present",
            "user": null,
            "value": "Test"
        },
        "module_name": "cronvar"
    },
    "msg": "must be privileged to use -u\n"
}
        to retry, use: --limit @/home/michael/repos/ansible/a.retry

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1

after:

PLAY [ansible] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [localhost]

TASK [Add var to crontab] *******************************************************************************************************************************************************************
changed: [localhost]

PLAY RECAP **********************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0
ansibot commented 7 years ago

This repository has been locked. All new issues and pull requests should be filed in https://github.com/ansible/ansible

Please read through the repomerge page in the dev guide.