Junos OS

 View Only
last person joined: 2 days ago 

Ask questions and share experiences about Junos OS.
  • 1.  Schedule reboot for "tomorrow" in Ansible

    Posted 10 days ago

    I'm building an Ansible playbook to install firmware updates on multiple switches and then schedule the devices to restart at 2 AM the next day.

    What's the best way to accomplish the "reboot at 2 AM tomorrow" part?

    I found the below article, but it doesn't suggest a way to schedule a reboot using a relative date.  If I use "02:00" for the "at" time, I get an error:  "reboot failed. Error: RpcError(severity: error, bad_element: None, message: that time is already past.)"

    (I don't want to edit the reboot date every time I run the playbook.)

    I assume there is a function in Ansible to calculate tomorrow's date, format it, and assign to a variable.   However, I'm still a newbie at using Ansible, so I'm hoping there is a simpler/better way.

      Use Ansible to Halt, Reboot, or Shut Down Junos Devices

    Juniper remove preview
    Use Ansible to Halt, Reboot, or Shut Down Junos Devices
    SUMMARY Use the Juniper Networks Ansible modules to halt, reboot, or shut down Junos devices.
    View this on Juniper >

    Thanks in advance for any advice or suggestions.



    ------------------------------
    djz
    ------------------------------


  • 2.  RE: Schedule reboot for "tomorrow" in Ansible

     
    Posted 8 days ago
    Edited by asharp 8 days ago

    Not very pretty, but works I think.

    "at time" supports yymmddhhmm as per reference https://www.juniper.net/documentation/us/en/software/junos/cli-reference/topics/ref/command/request-system-reboot.html#request-system-reboot-command__d63561e236

    ---
    - name: Calculate today's date and tomorrow at 2:00 AM
      hosts: localhost
      gather_facts: false
      tasks:
        - name: Get today's date
          set_fact:
            today_date: "{{ lookup('pipe', 'date +%y%m%d') }}"
    
        - name: Get the current epoch time
          set_fact:
            current_epoch: "{{ lookup('pipe', 'date +%s') | int }}"
    
        - name: Get tomorrow's date
          set_fact:
            tomorrow_date: "{{ ((current_epoch | int) + 86400 + 7200) }}"
    
        - name: Restart date
          set_fact:
            restart_date: "{{ '%y%m%d0200' | ansible.builtin.strftime(second=tomorrow_date, utc=true) }}"
    
        - name: Display today's date
          debug:
            msg: "Today's date is {{ today_date }}"
    
        - name: Display tomorrow's date at 2:00 AM
          debug:
            msg: "Restart date is {{ restart_date }}"
    

    Results:

    PLAY [Calculate today's date and tomorrow at 2:00 AM] *************************************************************************************************************************************
    
    TASK [Get today's date] *********************************************************************************************************************
    ok: [localhost]
    
    TASK [Get the current epoch time] ***********************************************************************************************************
    ok: [localhost]
    
    TASK [Get tomorrow's date] ******************************************************************************************************************
    ok: [localhost]
    
    TASK [Restart date] ***************************************************************************************************************************
    ok: [localhost]
    
    TASK [Display today's date] ***********************************************************************************************************************************************
    ok: [localhost] => {
        "msg": "Today's date is 240710"
    }
    
    TASK [Display tomorrow's date at 2:00 AM] ***********************************************************************************************************************************************
    ok: [localhost] => {
        "msg": "Restart date is 2407110200"
    }
    
    PLAY RECAP **********************************************************************************************************************************
    localhost                  : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

    Regards



    ------------------------------
    Andy Sharp
    ------------------------------



  • 3.  RE: Schedule reboot for "tomorrow" in Ansible
    Best Answer

    Posted 8 days ago
    Edited by DANIEL ZOOK 8 days ago
    Thanks for the reply!
    After some digging, help from a Juniper SE, and much trial and error, I came up with this:

    ---
    - name: Perform a Junos OS software upgrade
      hosts:
        - test102.emu.edu
      connection: local
      gather_facts: no
      roles:
        - Juniper.junos

      tasks:
        - name: Create formatted date string for 2 AM tomorrow
          set_fact:
            tomorrow: "{{ now(fmt='%y%m%d') | int + 1 }}0200"
        - name: Request reboot at 2 AM
          juniper_junos_system:
            action: "reboot"
            at: "{{ tomorrow }}"
          register: response
        - name: Print the response
          debug:
          var: response.mg

    Results:
    PLAY [Schedule Juniper system(s) to reboot at 2 AM] *********************************************************************************************************************************
    
    TASK [Create formatted date string for 2 AM tomorrow] *******************************************************************************************************************************
    ok: [test129.emu.edu]
    
    TASK [Request reboot at 2 AM] *******************************************************************************************************************************************************
    changed: [test129.emu.edu]
    
    TASK [Print the response] ***********************************************************************************************************************************************************
    ok: [test129.emu.edu] => {
        "response.msg": "reboot successfully initiated. Response got Shutdown at Thu Jul 11 02:00:00 2024. [pid 16959]"
    }
    
    PLAY RECAP **************************************************************************************************************************************************************************
    test129.emu.edu            : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    


  • 4.  RE: Schedule reboot for "tomorrow" in Ansible

     
    Posted 7 days ago

    Thanks for sharing your example, a lot more simple and elegant than mine.



    ------------------------------
    Andy Sharp
    ------------------------------