优化 Ansible 任务以在配置的时间重新启动 Windows 任务计划程序

问题描述 投票:0回答:1

我有以下简单的 Windows 调度程序重新启动任务,它工作得很好,但是我希望可以选择有时将 restart_morning_time 或 restart_evening_time 设置为 NULL。最干净的方法是什么?我尝试将它作为一个列表,并用逗号分隔,这样我就可以传递任意数量的 restart_times,而不是有 2 个变量,我只需要一个名为“restart_times”的变量(这将是理想的),但这只会给出一个错误;

致命:[windows-test-1]:失败! => {"已更改": false, "msg": "触发 选项“start_boundary”必须采用“YYYY-MM-DDThh:mm:ss”格式 格式但为 '2024-08-10T07:00:00 2024-08-10T19:00:00'"}

- name: Schedule restart for windows machine 
  community.windows.win_scheduled_task:
    name: "Scheduled restart machine"
    description: "This task restarts the machine at configured date && time"
    actions:
      - path: shutdown.exe
        arguments: "/r /f /t 0"
    triggers:
      - type: "{{ trigger_type }}"
        start_boundary: "{{ restart_morning_time }}"
      - type: "{{ trigger_type }}"
        start_boundary: "{{ restart_evening_time }}"
    username: "SYSTEM"
    run_level: highest
    state: present

这些是我要传递的变量;

  vars:
    trigger_type: "daily"  # Options could be 'daily', 'weekly', 'monthly'
    restart_morning_time: "2024-08-10T07:00:00"
    restart_evening_time: "2024-08-10T19:00:00"
windows ansible
1个回答
0
投票

我发现的一个解决方法是向触发器添加默认值(省略),但如果有更好的方法,我愿意接受建议;

triggers:
  - type: "{{ trigger_type }}"
    start_boundary: "{{ restart_morning_time | default(omit) }}"
  - type: "{{ trigger_type }}"
    start_boundary: "{{ restart_evening_time | default(omit) }}"
© www.soinside.com 2019 - 2024. All rights reserved.