DEV Community

Mohsen Kokabi
Mohsen Kokabi

Posted on

Deploying DotNetCore on Ubuntu using Ansible

Ansible

Ansible is an IT automation tool but I am going to use it here as a deployment tools. Ansible manages machines in an agent-less manner, so I only need to install it on my managing machine.

Provisioning 2 VMs

I have provisioned 2 Ubuntu VMs on Azure. The managing VM is called AnsibleLocal and the other one is called node01.

Installing Ansible on the AnsibleLocal machine

From Azure cloud shell ssh to the local machine and then run following commands:



$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible


Enter fullscreen mode Exit fullscreen mode

If you have different OS find the installation instruction from here

Per Microsoft recommendation we are going to use snap module. Then ensure the version of ansible is greater than 2.8.



ansible-playbook --version


Enter fullscreen mode Exit fullscreen mode

again to see the details of this module you can try:



ansible-doc -t module snap


Enter fullscreen mode Exit fullscreen mode

Adding the node01 machine

We need to tell ansible the machine(s) which are going to be managed. Edit the ansible hosts file.



nano  /etc/ansible/hosts


Enter fullscreen mode Exit fullscreen mode

Create a group and add the IP and ssh login details of the node machine(s)



[MyGroupA]
10.0.0.5 ansible_ssh_pass={a password} ansible_ssh_user={a user}


Enter fullscreen mode Exit fullscreen mode

Creating the playbook

Create a yaml file



nano myplaybook.yaml


Enter fullscreen mode Exit fullscreen mode

and put the following content:



- name: my sample playbook
  hosts: [MyGroupA]
  remote_user: mohsen
  become: true

  tasks:
  - name: Install DotNetCore
    snap:
      name: dotnet-sdk 
      channel: 3.1
      classic: yes
      state: present


Enter fullscreen mode Exit fullscreen mode

Note: the hosts group name (MyGroupA) and the remote_user is what we have defined in /etc/ansible/hosts file.

Note: The snap module parameters used here are coming from Microsoft instruction:
sudo snap install dotnet-sdk --channel=3.1/beta --classic

In this example we are installing the dotnetsdk but in real cases we might only need dotnet runtime so the snap properties would be like:



  - name: Install DotNetCore
    snap:
      name: dotnet-runtime-31
      state: present


Enter fullscreen mode Exit fullscreen mode

In the most of ansible modules the property state accept following choices:

  • present: it would install
  • absent: it would uninstall

Before running the playbook you can do a syntax check on it:



ansible-playbook myplaybook.yaml --syntax-check


Enter fullscreen mode Exit fullscreen mode

Running

Finally, you can run our playbook:



ansible-playbook myplaybook.yaml 


Enter fullscreen mode Exit fullscreen mode

The output would be like
report
If we run the playbook again, it would detect the DotNetCore is already installed so the changed would be 0 but the ok would be still 2.
The output this time would be like
report

Now, we are going to ssh to the node01 VM and do a test. Again, as explained by Microsoft:

When .NET Core is installed using the Snap package, the default .NET Core command is dotnet-sdk.dotnet, as opposed to just dotnet

Thus we would test as:



dotnet-sdk.dotnet --version


Enter fullscreen mode Exit fullscreen mode

report

Top comments (0)