0

I'm trying to create a cron job using Ansible with this playbook:

---
- name: Crontab dosyasını düzenle
  hosts: localhost
  gather_facts: false
  vars:
    ansible_job_id: "1.0.0"

  tasks:
    - name: Crontab görevini ekle
      cron:
        name: "Run Python script daily"
        minute: "0"
        hour: "0"
        job: "python3 -c 'print((\"{{ ansible_job_id }}\")'")
        state: present
      register: output

    - name: Sonucu göster 1
      debug:
        var: output.stdout

    - name: Sonucu göster 2
      debug:
        var: ansible_job_id

But, when I run the playbook, the task named Sonucu göster 1 gives the error

VARIABLE IS NOT DEFINED!

It seems that the output.stdout variable is not properly registered.
How can I resolve this issue?

U880D
  • 8,601
  • 6
  • 24
  • 40
Penguen
  • 16,836
  • 42
  • 130
  • 205
  • 2
    Not all tasks do return a `stdout`. Have you tried debugging `output` instead? What does it gives? (You should add this piece of information in an [edit] of your question). – β.εηοιτ.βε Jul 16 '23 at 12:09
  • Questions about configuring cron for systems or administration are OFF TOPIC. – Rob Jul 16 '23 at 12:22
  • 1
    This is a question about an Ansible module and it's Return Values. – U880D Jul 16 '23 at 12:23

1 Answers1

1

It seems that the output.stdout variable is not properly registered.

That is correct and the expected behavior.

According the documentation about cron module – Manage cron.d and crontab entries, the module does not provide Module Specific Return Values

Ansible modules normally return a data structure that can be registered into a variable, or seen directly when output by the ansible program. Each module can optionally document its own unique return values ...

but only Common Return Values

stdout Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on). This field contains the normal output of these utilities.

Therefore registering the results is only necessary if interested in the Common Return Values and it will not contain stdout.


If one is interested in further questions and answers ...

U880D
  • 8,601
  • 6
  • 24
  • 40