Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron module does not add all cron job for all hosts when using delegate to localhost. #82890

Closed
1 task done
Kaol-Fu opened this issue Mar 22, 2024 · 5 comments
Closed
1 task done
Labels
affects_2.11 bug This issue/PR relates to a bug. module This issue/PR relates to a module.

Comments

@Kaol-Fu
Copy link

Kaol-Fu commented Mar 22, 2024

Summary

I found then cron module might have some bug for adding job items. It does not add all job items for all hosts when using delegate to localhost.

Issue Type

Bug Report

Component Name

cron

Ansible Version

$ ansible --version
ansible-playbook [core 2.16.4]
  config file = None
  configured module search path = ['/Users/kaol/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/lib/python3.11/site-packages/ansible
  ansible collection location = /Users/kaol/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.11.8 (main, Feb  6 2024, 21:21:21) [Clang 15.0.0 (clang-1500.1.0.2.5)] (/opt/homebrew/opt/python@3.11/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True

OS / Environment

MacOS 14.4

Steps to Reproduce

Playbook:

---
- name: Test crontab
  hosts: all
  remote_user: kaol
  gather_facts: false
  tasks:
    - name: Add cron jobs
      cron:
        name: 'Test {{ inventory_hostname }} : {{ item }}'
        minute: '0'
        hour: '0'
        month: '4'
        day: '1'
        user: kaol
        job: 'echo "This is {{ inventory_hostname }} user {{ item}}"'
      loop: '{{ users | default([], true) }}'
      delegate_to: localhost

Execute playbook with vvv options output:

ansible-playbook [core 2.16.4]
  config file = None
  configured module search path = ['/Users/kaol/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/lib/python3.11/site-packages/ansible
  ansible collection location = /Users/kaol/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.11.8 (main, Feb  6 2024, 21:21:21) [Clang 15.0.0 (clang-1500.1.0.2.5)] (/opt/homebrew/opt/python@3.11/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True
No config file found; using defaults
Parsed 192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5,192.168.1.6, inventory source with host_list plugin
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

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

PLAY [Test crontab] ****************************************************************************************************

TASK [Add cron jobs] ***************************************************************************************************
task path: /Users/kaol/ansible-workshops/test_cron.yml:6
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: kaol
<localhost> EXEC /bin/sh -c 'echo ~kaol && sleep 0'
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: kaol
<localhost> EXEC /bin/sh -c 'echo ~kaol && sleep 0'
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: kaol
<localhost> EXEC /bin/sh -c 'echo ~kaol && sleep 0'
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: kaol
<localhost> EXEC /bin/sh -c 'echo ~kaol && sleep 0'
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: kaol
<localhost> EXEC /bin/sh -c 'echo ~kaol && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/kaol/.ansible/tmp `"&& mkdir "` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.384714-50244-130905771084863 `" && echo ansible-tmp-1711331896.384714-50244-130905771084863="` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.384714-50244-130905771084863 `" ) && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/kaol/.ansible/tmp `"&& mkdir "` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.386559-50243-51322971202738 `" && echo ansible-tmp-1711331896.386559-50243-51322971202738="` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.386559-50243-51322971202738 `" ) && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/kaol/.ansible/tmp `"&& mkdir "` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.389885-50245-66537348496623 `" && echo ansible-tmp-1711331896.389885-50245-66537348496623="` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.389885-50245-66537348496623 `" ) && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/kaol/.ansible/tmp `"&& mkdir "` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.393787-50246-206979943343268 `" && echo ansible-tmp-1711331896.393787-50246-206979943343268="` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.393787-50246-206979943343268 `" ) && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/kaol/.ansible/tmp `"&& mkdir "` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.397936-50247-154269452919193 `" && echo ansible-tmp-1711331896.397936-50247-154269452919193="` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.397936-50247-154269452919193 `" ) && sleep 0'
Using module file /opt/homebrew/lib/python3.11/site-packages/ansible/modules/cron.py
Using module file /opt/homebrew/lib/python3.11/site-packages/ansible/modules/cron.py
Using module file /opt/homebrew/lib/python3.11/site-packages/ansible/modules/cron.py
<localhost> PUT /Users/kaol/.ansible/tmp/ansible-local-50241xsxmesf2/tmpxh01m9a3 TO /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.384714-50244-130905771084863/AnsiballZ_cron.py
Using module file /opt/homebrew/lib/python3.11/site-packages/ansible/modules/cron.py
Using module file /opt/homebrew/lib/python3.11/site-packages/ansible/modules/cron.py
<localhost> PUT /Users/kaol/.ansible/tmp/ansible-local-50241xsxmesf2/tmpbun17ol_ TO /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.386559-50243-51322971202738/AnsiballZ_cron.py
<localhost> PUT /Users/kaol/.ansible/tmp/ansible-local-50241xsxmesf2/tmprsh_yz6t TO /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.393787-50246-206979943343268/AnsiballZ_cron.py
<localhost> PUT /Users/kaol/.ansible/tmp/ansible-local-50241xsxmesf2/tmp__n_usyr TO /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.389885-50245-66537348496623/AnsiballZ_cron.py
<localhost> PUT /Users/kaol/.ansible/tmp/ansible-local-50241xsxmesf2/tmpiuis9xlx TO /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.397936-50247-154269452919193/AnsiballZ_cron.py
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.397936-50247-154269452919193/ /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.397936-50247-154269452919193/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.389885-50245-66537348496623/ /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.389885-50245-66537348496623/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.384714-50244-130905771084863/ /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.384714-50244-130905771084863/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.386559-50243-51322971202738/ /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.386559-50243-51322971202738/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.393787-50246-206979943343268/ /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.393787-50246-206979943343268/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c '/opt/homebrew/opt/python@3.11/bin/python3.11 /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.384714-50244-130905771084863/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c '/opt/homebrew/opt/python@3.11/bin/python3.11 /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.389885-50245-66537348496623/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c '/opt/homebrew/opt/python@3.11/bin/python3.11 /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.393787-50246-206979943343268/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c '/opt/homebrew/opt/python@3.11/bin/python3.11 /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.397936-50247-154269452919193/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c '/opt/homebrew/opt/python@3.11/bin/python3.11 /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.386559-50243-51322971202738/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.384714-50244-130905771084863/ > /dev/null 2>&1 && sleep 0'
changed: [192.168.1.2 -> localhost] => (item=test) => {
    "ansible_loop_var": "item",
    "changed": true,
    "envs": [],
    "invocation": {
        "module_args": {
            "backup": false,
            "cron_file": null,
            "day": "1",
            "disabled": false,
            "env": false,
            "hour": "0",
            "insertafter": null,
            "insertbefore": null,
            "job": "echo \"This is 192.168.1.2 user test\"",
            "minute": "0",
            "month": "4",
            "name": "Test 192.168.1.2 : test",
            "special_time": null,
            "state": "present",
            "user": null,
            "weekday": "*"
        }
    },
    "item": "test",
    "jobs": [
        "Test 192.168.1.2 : test"
    ]
}
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: kaol
<localhost> EXEC /bin/sh -c 'echo ~kaol && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/kaol/.ansible/tmp `"&& mkdir "` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331897.234956-50337-51007621730226 `" && echo ansible-tmp-1711331897.234956-50337-51007621730226="` echo /Users/kaol/.ansible/tmp/ansible-tmp-1711331897.234956-50337-51007621730226 `" ) && sleep 0'
Using module file /opt/homebrew/lib/python3.11/site-packages/ansible/modules/cron.py
<localhost> PUT /Users/kaol/.ansible/tmp/ansible-local-50241xsxmesf2/tmpi2oso4_j TO /Users/kaol/.ansible/tmp/ansible-tmp-1711331897.234956-50337-51007621730226/AnsiballZ_cron.py
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/kaol/.ansible/tmp/ansible-tmp-1711331897.234956-50337-51007621730226/ /Users/kaol/.ansible/tmp/ansible-tmp-1711331897.234956-50337-51007621730226/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c '/opt/homebrew/opt/python@3.11/bin/python3.11 /Users/kaol/.ansible/tmp/ansible-tmp-1711331897.234956-50337-51007621730226/AnsiballZ_cron.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.389885-50245-66537348496623/ > /dev/null 2>&1 && sleep 0'
changed: [192.168.1.3 -> localhost] => (item=test) => {
    "ansible_loop_var": "item",
    "changed": true,
    "envs": [],
    "invocation": {
        "module_args": {
            "backup": false,
            "cron_file": null,
            "day": "1",
            "disabled": false,
            "env": false,
            "hour": "0",
            "insertafter": null,
            "insertbefore": null,
            "job": "echo \"This is 192.168.1.3 user test\"",
            "minute": "0",
            "month": "4",
            "name": "Test 192.168.1.3 : test",
            "special_time": null,
            "state": "present",
            "user": null,
            "weekday": "*"
        }
    },
    "item": "test",
    "jobs": [
        "Test 192.168.1.3 : test"
    ]
}
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.397936-50247-154269452919193/ > /dev/null 2>&1 && sleep 0'
changed: [192.168.1.5 -> localhost] => (item=test) => {
    "ansible_loop_var": "item",
    "changed": true,
    "envs": [],
    "invocation": {
        "module_args": {
            "backup": false,
            "cron_file": null,
            "day": "1",
            "disabled": false,
            "env": false,
            "hour": "0",
            "insertafter": null,
            "insertbefore": null,
            "job": "echo \"This is 192.168.1.5 user test\"",
            "minute": "0",
            "month": "4",
            "name": "Test 192.168.1.5 : test",
            "special_time": null,
            "state": "present",
            "user": null,
            "weekday": "*"
        }
    },
    "item": "test",
    "jobs": [
        "Test 192.168.1.5 : test"
    ]
}
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.386559-50243-51322971202738/ > /dev/null 2>&1 && sleep 0'
changed: [192.168.1.1 -> localhost] => (item=test) => {
    "ansible_loop_var": "item",
    "changed": true,
    "envs": [],
    "invocation": {
        "module_args": {
            "backup": false,
            "cron_file": null,
            "day": "1",
            "disabled": false,
            "env": false,
            "hour": "0",
            "insertafter": null,
            "insertbefore": null,
            "job": "echo \"This is 192.168.1.1 user test\"",
            "minute": "0",
            "month": "4",
            "name": "Test 192.168.1.1 : test",
            "special_time": null,
            "state": "present",
            "user": null,
            "weekday": "*"
        }
    },
    "item": "test",
    "jobs": [
        "Test 192.168.1.1 : test"
    ]
}
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/kaol/.ansible/tmp/ansible-tmp-1711331896.393787-50246-206979943343268/ > /dev/null 2>&1 && sleep 0'
changed: [192.168.1.4 -> localhost] => (item=test) => {
    "ansible_loop_var": "item",
    "changed": true,
    "envs": [],
    "invocation": {
        "module_args": {
            "backup": false,
            "cron_file": null,
            "day": "1",
            "disabled": false,
            "env": false,
            "hour": "0",
            "insertafter": null,
            "insertbefore": null,
            "job": "echo \"This is 192.168.1.4 user test\"",
            "minute": "0",
            "month": "4",
            "name": "Test 192.168.1.4 : test",
            "special_time": null,
            "state": "present",
            "user": null,
            "weekday": "*"
        }
    },
    "item": "test",
    "jobs": [
        "Test 192.168.1.4 : test"
    ]
}
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/kaol/.ansible/tmp/ansible-tmp-1711331897.234956-50337-51007621730226/ > /dev/null 2>&1 && sleep 0'
changed: [192.168.1.6 -> localhost] => (item=test) => {
    "ansible_loop_var": "item",
    "changed": true,
    "envs": [],
    "invocation": {
        "module_args": {
            "backup": false,
            "cron_file": null,
            "day": "1",
            "disabled": false,
            "env": false,
            "hour": "0",
            "insertafter": null,
            "insertbefore": null,
            "job": "echo \"This is 192.168.1.6 user test\"",
            "minute": "0",
            "month": "4",
            "name": "Test 192.168.1.6 : test",
            "special_time": null,
            "state": "present",
            "user": null,
            "weekday": "*"
        }
    },
    "item": "test",
    "jobs": [
        "Test 192.168.1.3 : test",
        "Test 192.168.1.6 : test"
    ]
}

PLAY RECAP *************************************************************************************************************
192.168.1.1                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.1.2                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.1.3                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.1.4                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.1.5                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.1.6                : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Expected Results

All cron job was added successfully.

Actual Results

Just only 2 jobs have been added.

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot
Copy link
Contributor

ansibot commented Mar 22, 2024

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the component bot command.

@ansibot ansibot added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. affects_2.11 module This issue/PR relates to a module. labels Mar 22, 2024
@ansibot
Copy link
Contributor

ansibot commented Mar 22, 2024

@Kaol-Fu ansible-core 2.11 is not supported and no longer receives bug fixes. Please test against one of the supported versions of ansible-core, preferably the most recent one, to see whether the bug has been fixed.

click here for bot help

@Kaol-Fu
Copy link
Author

Kaol-Fu commented Mar 25, 2024

@Kaol-Fu ansible-core 2.11 is not supported and no longer receives bug fixes. Please test against one of the supported versions of ansible-core, preferably the most recent one, to see whether the bug has been fixed.

click here for bot help

ansible-core 2.16.4 is the same as 2.11. I have already updated the logs.

@bcoca
Copy link
Member

bcoca commented Mar 25, 2024

Not a bug in the cron module, but a race condition on delegation to the same host when using parallel processes. See https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#delegation-and-parallel-execution

@Kaol-Fu
Copy link
Author

Kaol-Fu commented Mar 26, 2024

Not a bug in the cron module, but a race condition on delegation to the same host when using parallel processes. See https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#delegation-and-parallel-execution

Thank you. You're right. I didn't notice the parallel execution problem. When I set the keyword throttle to 1 to the cron task, the problem was resolved.

@Kaol-Fu Kaol-Fu closed this as completed Mar 26, 2024
@mkrizek mkrizek removed the needs_triage Needs a first human triage before being processed. label Mar 26, 2024
@ansible ansible locked and limited conversation to collaborators Apr 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.11 bug This issue/PR relates to a bug. module This issue/PR relates to a module.
Projects
None yet
Development

No branches or pull requests

4 participants