You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible cron模块stdout返回值未注册问题解决方法

问题解决方法

1. 为什么output.stdout未定义?

Ansible的cron模块不会返回stdout字段,它的返回结果是任务执行的状态元数据(比如是否修改了crontab、任务状态等),没有命令执行的标准输出。直接访问不存在的stdout字段就会触发VARIABLE IS NOT DEFINED!错误。

2. 修正调试任务

如果想查看cron模块的执行结果,直接调试整个output变量,或者访问它的有效字段:

- name: Sonucu göster 1
  debug:
    var: output  # 直接查看整个注册变量的结构

或者查看具体状态字段:

- name: Sonucu göster 1
  debug:
    msg: "Crontab是否被修改: {{ output.changed }}, 任务状态: {{ output.state }}"

3. 修复job参数的语法错误

原Playbook中job字段的Python代码存在括号不匹配和引号转义问题,会导致cron任务执行失败,修正如下:

job: "python3 -c 'print(\"{{ ansible_job_id }}\")'"

或者改用单引号嵌套写法避免转义:

job: 'python3 -c "print(\"{{ ansible_job_id }}\")"'

修正后的完整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

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

内容的提问来源于stack exchange,提问作者Penguen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 23:02:17