Ansible Playbook to install Mujoco

This will download a specific Mujoco version from Github and install it to /opt/mujoco

---
- name: Install MuJoCo
  hosts: all
  become: true
  vars:
    mujoco_version: "3.2.7"
    mujoco_install_dir: "/opt/mujoco"
    mujoco_download_url: "https://github.com/google-deepmind/mujoco/releases/download/{{ mujoco_version }}/mujoco-{{ mujoco_version }}-linux-x86_64.tar.gz"
    mujoco_tmp_path: "/tmp/mujoco-{{ mujoco_version }}.tar.gz"

  tasks:
    - name: Check if MuJoCo is already installed
      stat:
        path: "{{ mujoco_install_dir }}/lib/libmujoco.so"
      register: mujoco_installed

    - name: Ensure installation directory exists
      file:
        path: "{{ mujoco_install_dir }}"
        state: directory
        mode: '0755'
      when: not mujoco_installed.stat.exists

    - name: Download MuJoCo
      get_url:
        url: "{{ mujoco_download_url }}"
        dest: "{{ mujoco_tmp_path }}"
        mode: '0644'
      when: not mujoco_installed.stat.exists

    - name: Extract MuJoCo
      unarchive:
        src: "{{ mujoco_tmp_path }}"
        dest: "{{ mujoco_install_dir }}"
        remote_src: yes
        extra_opts:
          - --strip-components=1
        creates: "{{ mujoco_install_dir }}/lib/libmujoco.so"
      when: not mujoco_installed.stat.exists

    - name: Clean up downloaded archive
      file:
        path: "{{ mujoco_tmp_path }}"
        state: absent
      when: not mujoco_installed.stat.exists

    - name: Set environment variables
      blockinfile:
        path: /etc/profile.d/mujoco.sh
        create: yes
        block: |
          export MUJOCO_PATH="{{ mujoco_install_dir }}"
          export LD_LIBRARY_PATH="$MUJOCO_PATH/lib:$LD_LIBRARY_PATH"
        mode: '0644'