--- - name: Create group group: name: "{{ octoprint_group }}" - name: Create user user: name: "{{ octoprint_user }}" home: "/home/{{ octoprint_user }}" group: "{{ octoprint_group }}" append: yes groups: - video - dialout - name: Add octoprint user to additional groups user: name: "{{ octoprint_user }}" append: yes groups: "{{ octoprint_groups }}" when: octoprint_groups is defined - name: Install requirements apt: name: - python3-pip - python3-virtualenv - name: Create octoprint directory file: path: "{{ octoprint_dir }}" state: directory owner: "{{ octoprint_user }}" group: "{{ octoprint_group }}" - name: Create virtual env and install python dependencies pip: name: octoprint virtualenv: "{{ octoprint_dir }}" become: true become_user: "{{ octoprint_user }}" - name: Install default octoprint plugins pip: name: "{{ item }}" virtualenv: "{{ octoprint_dir }}" become: true become_user: "{{ octoprint_user }}" with_items: "{{ octoprint_default_plugins }}" notify: Restart octoprint - name: Install additional octoprint plugins pip: name: "{{ item }}" virtualenv: "{{ octoprint_dir }}" become: true become_user: "{{ octoprint_user }}" with_items: "{{ octoprint_additional_plugins }}" when: octoprint_additional_plugins is defined notify: Restart octoprint - name: Create configuration directories file: path: "/home/{{ octoprint_user }}/{{ item }}" owner: "{{ octoprint_user }}" group: "{{ octoprint_group }}" state: directory mode: u+rw,g+rw,o+r with_items: - .octoprint - .octoprint/printerProfiles - .octoprint/data - .octoprint/data/appkeys - name: Install systemd unit template: src=octoprint.service.j2 dest=/lib/systemd/system/octoprint.service notify: - Reload systemd - Restart octoprint ################################################################################ # Octoprint configuration ################################################################################ - name: Register if config already exists on target host stat: path: "/home/{{ octoprint_user }}/.octoprint/config.yaml" register: octoprint_register_config_stat - name: Default octoprint config set_fact: octoprint_default_config: "{{ lookup('template', 'config.yaml.j2') | from_yaml }}" - name: Apply user config set_fact: octoprint_config_combined: "{{ octoprint_default_config | combine(octoprint_config, recursive=True)}}" when: octoprint_config is defined - name: Apply user config set_fact: octoprint_config_combined: "{{ octoprint_default_config }}" when: octoprint_config is not defined - name: Configure Octoprint block: - name: Initial configuration of octoprint copy: content: "{{ octoprint_config_combined | to_nice_yaml }}" dest: "/home/{{ octoprint_user }}/.octoprint/config.yaml" when: not octoprint_register_config_stat.stat.exists - name: Update existing Octoprint configuration block: - name: Read Octoprint config file slurp: src: "/home/{{ octoprint_user }}/.octoprint/config.yaml" register: octoprint_register_config - name: Parse Octoprint config file set_fact: current_octoprint_config: "{{ octoprint_register_config['content'] | b64decode | from_yaml}}" - name: Write updated octoprint configuration copy: content: "{{ current_octoprint_config | combine(octoprint_config_combined, recursive=True) | to_nice_yaml}}" dest: "/home/{{octoprint_user}}/.octoprint/config.yaml" notify: Restart octoprint when: octoprint_register_config_stat.stat.exists become: true become_user: "{{ octoprint_user }}" ################################################################################ # Octoprint printer configuration ################################################################################ - name: Setup printer profiles copy: content: "{{ item | to_nice_yaml}}" dest: "/home/{{ octoprint_user }}/.octoprint/printerProfiles/{{item.id}}.profile" loop: "{{ octoprint_printer_profiles }}" become: true become_user: "{{ octoprint_user }}" ################################################################################ # Octoprint Users ################################################################################ - name: Setup octoprint users block: - name: Register if user databse already exists on target host stat: path: "/home/{{ octoprint_user }}/.octoprint.users.yaml" register: octoprint_register_users - name: Copy default users copy: content: "{{ octoprint_users_list | to_nice_yaml }}" dest: "/home/{{ octoprint_user }}/.octoprint/users.yaml" when: not octoprint_register_users.stat.exists - name: Merge users database with default users block: - name: Read Octoprint users file slurp: src: "/home/{{ octoprint_user }}/.octoprint/users.yaml" register: octoprint_register_users_file - name: Parse Octoprint users file set_fact: current_octoprint_users: "{{ octoprint_register_users_file['content'] | b64decode | from_yaml }}" - name: Merge current users with default users set_fact: octoprint_users_db: "{{ current_octoprint_users | combine(octoprint_users_list)}}" - name: Copy new users file copy: content: "{{ octoprint_users_db | to_nice_yaml }}" dest: "/home/{{ octoprint_user }}/.octoprint/users.yaml" when: octoprint_register_users.stat.exists when: octoprint_users_list is defined become: true become_user: "{{ octoprint_user }}" ################################################################################ # Octoprint app keys ################################################################################ - name: Install application keys block: - name: Register existing app keys stat: path: /home/{{ octoprint_user }}/.octoprint/data/appkeys/keys.yaml register: app_keys_file - name: Read existing app keys slurp: src: /home/{{ octoprint_user }}/.octoprint/data/appkeys/keys.yaml register: existing_app_keys_content when: app_keys_file.stat.exists - name: Combine local and remote app keys set_fact: app_keys_combined: "{{ existing_app_keys_content['content'] | b64decode | from_yaml | combine(octoprint_app_keys ,recursive=True) }}" when: app_keys_file.stat.exists - name: Copy default app keys copy: content: "{{ app_keys_combined | to_nice_yaml }}" dest: /home/{{ octoprint_user }}/.octoprint/data/appkeys/keys.yaml when: app_keys_file.stat.exists - name: Copy default app keys copy: content: "{{ octoprint_app_keys | to_nice_yaml }}" dest: /home/{{ octoprint_user }}/.octoprint/data/appkeys/keys.yaml when: not app_keys_file.stat.exists when: octoprint_app_keys is defined notify: Restart octoprint ################################################################################ # Webcam ################################################################################ - name: Setup webcam block: - name: Install dependencies apt: name: - ffmpeg - ustreamer - name: Install systemd unit template: src=ustreamer.service.j2 dest=/lib/systemd/system/ustreamer.service notify: - Reload systemd - Restart ustreamer - name: Enable and start ustreamer service: name: ustreamer state: started enabled: yes when: octoprint_has_webcam ################################################################################ # Reverse proxy ################################################################################ - name: Configure reverse proxy block: - name: Ensure certificates are available command: openssl req -x509 -nodes -newkey rsa:2048 -keyout /etc/apache2/ssl/{{ octoprint_domain }}.key -out /etc/apache2/ssl/{{ octoprint_domain }}.crt -days 730 -subj "/CN={{ octoprint_domain }}" creates=/etc/apache2/ssl/{{ octoprint_domain }}.crt notify: Restart apache2 when: octoprint_ssl - name: Configure certificate manager for octoprint template: src=certs.j2 dest=/etc/acertmgr/{{ octoprint_domain }}.conf notify: Run acertmgr when: octoprint_ssl - name: Configure vhost template: src=nginx_vhost.j2 dest=/etc/nginx/sites-available/octoprint notify: Restart nginx - name: Enable vhost file: src=/etc/nginx/sites-available/octoprint dest=/etc/nginx/sites-enabled/octoprint state=link notify: Restart nginx - name: Enable octoprint service: name: octoprint state: started enabled: yes