Skip to content

Instantly share code, notes, and snippets.

@ninjarobot
Last active April 7, 2024 13:13
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ninjarobot/16484d0d524c5d1e48dedf2e086a932d to your computer and use it in GitHub Desktop.
Save ninjarobot/16484d0d524c5d1e48dedf2e086a932d to your computer and use it in GitHub Desktop.
Ansible playbook to install the .NET Core SDK on an Ubuntu server.
---
- hosts: all
tasks:
- name: Download MS product repository
get_url:
url: https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
dest: /tmp/packages-microsoft-prod.deb
- name: Install MS product repository
apt: deb=/tmp/packages-microsoft-prod.deb
become: true
- name: Make sure HTTPS is supported by apt
apt:
name: apt-transport-https
state: present
update_cache: no
become: true
- name: Install .NET Core SDK
apt:
name: dotnet-sdk-2.2
state: present
update_cache: yes
become: true
@ninjarobot
Copy link
Author

The hosts file contains the hostname of the servers where the SDK should be deployed.
ansible-playbook dotnet.yml -i hosts --ask-pass -K

@ninjarobot
Copy link
Author

From a bare system, to get ansible on the controller machine (in a virtualenv so as not to pollute things):

sudo easy_install pip
sudo pip install virtualenv
mkdir dotnet_ansible
cd dotnet_ansible
virtualenv ansible_env
source ansible_env/bin/activate
pip install ansible

@baronfel
Copy link

There's a PPA for it as well if you prefer that to managing pip-installed versions: https://launchpad.net/~ansible/+archive/ubuntu/ansible

@ninjarobot
Copy link
Author

Thanks @baronfel, but I'm virtualenv all the way :)

@baronfel
Copy link

Oh duh, I completely skipped past the virtualenv :)

@jashuRc
Copy link

jashuRc commented Jun 8, 2023

Alternatively install .NET with install-dotnet script.

#https://learn.microsoft.com/en-us/dotnet/core/install/linux-scripted-manual#scripted-install
---
- hosts: all
  vars:
    dotnet_runtime: dotnet
    dotnet_channel: 6.0
    dotnet_script_path: /tmp
    dotnet_installation_path: /home/jashu-local-001
    dotnet_dependencies:
      - libc6
      - libgcc1
      - libgssapi-krb5-2
      - libicu66
      - libssl1.1
      - libstdc++6
      - zlib1g

  tasks:
    - name : Make sure HTTPS is supported by apt
      apt:
        name: apt-transport-https
        state: latest
        update_cache: yes
      become: yes

    - name: Install dotnet dependencies #https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu-2004#dependencies
      apt:
        name: "{{ dotnet_dependencies }}"
        state: latest
        update_cache: yes
      become: yes

    - name: Download MS dotnet-install script
      get_url:
        url: https://dot.net/v1/dotnet-install.sh
        dest: "{{ dotnet_script_path }}/dotnet-install.sh"
        force: true

    - name: Make dotnet-install script executable
      ansible.builtin.command: chmod +x "{{ dotnet_script_path }}/dotnet-install.sh"
      become_user: yes

    - name: Install dotnet
      ansible.builtin.shell:
        cmd: "{{ dotnet_script_path }}/dotnet-install.sh --channel {{ dotnet_channel }} --runtime {{ dotnet_runtime }}"
        chdir: "{{ dotnet_installation_path  }}"

    - name: Cleanup dotnet-install files
      ansible.builtin.file:
        path: "{{ dotnet_script_path  }}/dotnet-install.sh"
        state: absent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment