0

I wanted to know how to implement nested loop concept to schedule a job in task scheduler which schedules 2 executables, This is my source yaml

---
scheduledTasks:
# 2 ACTIONS PLEASE CHECK
  - name: random_name
    description: no description
    triggers:
      type: "daily"
      start_boundary: "2022-11-06T12:58:54"
      execution_time_limit: PT4H
      enabled: true
      repetition:
        interval: PT15M
        stop_at_duration_end: false
    run_level: highest
    multiple_instances: 2 #ask
    disallow_start_if_on_batteries: true
    allow_hard_terminate: true
    start_when_available: false
    run_only_if_network_available: false
    allow_demand_start: true
    enabled: true
    hidden: false
    run_only_if_idle: false
    wake_to_run: false
    execution_time_limit: PT4H
    priority: 7
    actions:
      - path: C:\Program Files\WindowsApps\Microsoft.WindowsNotepad_11.2406.9.0_x64__8wekyb3d8bbwe\Notepad\Notepad.exe
      - path: C:\windows\system32\cmd.exe

This is my Ansible playbook

---
- name: Schedule a Python script
  hosts: windows_server
  vars_files:
    - /mnt/d/programs/source7.yaml
  tasks:
    - name: Schedule a job from Ansible
      community.windows.win_scheduled_task:
        name: "{{ item.name }}"
        description: "{{ item.description }}"
        actions:
          - path: "{{ item.actions.path }}"
            arguments: "{{ item.action.arguments | default(omit) }}"
        run_level: "{{ item.run_level }}"
        allow_demand_start: "{{ item.allow_demand_start | default(omit) }}"
        multiple_instances: "{{ item.multiple_instances }}"
        disallow_start_if_on_batteries: "{{ item.disallow_start_if_on_batteries | default(omit) }}"
        stop_if_going_on_batteries: "{{ item.stop_if_going_on_batteries | default(omit) }}"
        allow_hard_terminate: "{{ item.allow_hard_terminate }}"
        start_when_available: "{{ item.start_when_available }}"
        run_only_if_network_available: "{{ item.run_only_if_network_available }}"
        run_only_if_idle: "{{ item.run_only_if_idle | default(omit) }}"
        hidden: "{{ item.hidden }}"
        enabled: "{{ item.enabled | default(omit) }}"
        execution_time_limit: "{{ item.execution_time_limit }}"
        wake_to_run: "{{ item.wake_to_run }}"
        priority: "{{ item.priority | default(omit) }}"
        triggers: 
          - type: "{{ item.triggers.type | default(omit) }}"
            start_boundary: "{{ item.triggers.start_boundary }}"
            user_id: "{{ item.triggers.user_id | default(omit) }}"
            enabled: "{{ item.triggers.enabled | default(omit) }}"
            execution_time_limit: "{{ item.triggers.execution_time_limit | default(omit) }}"
            repetition:
              interval: "{{ item.triggers.repetition.interval | default(omit) }}"
              stop_at_duration_end: "{{ item.triggers.repetition.stop_at_duration_end | default(omit) }}"
      loop: "{{ scheduledTasks }}"

I wanted to cycle through the list of paths in actions in the source file and schedule it in the task scheduler, I've been going through the documents, couldn't find a appropriate solution.

I tried to use with_nested loop, but that doesn't seem appropriate.

3
  • Check out ˋwith_subelementsˋ Commented Sep 2, 2024 at 20:54
  • From your current description it is unclear if you like to schedule one task with two actions (for which you could just provide a list of actions) or if you like to schedule two separate tasks with one action each. Can you also describe in more detail the Parameter: multiple_instances? It there any reason for the question "How to implement nested loop concept to schedule a job in task scheduler which schedules 2 executables?" and focus on a maybe solution? Commented Sep 3, 2024 at 7:31
  • 1
    @U880D I wanted to schedule one task with two actions, as you suggested I used list of actions and it worked, thank you! I initially thought I had to implement a nested loop to provide an executable path under the actions variable. But parsing the whole list got the job done. Commented Sep 4, 2024 at 10:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.