DSC for Linux example – application installation and configuration with bash script

In the following example I will show how to install the “ocsinventory-agent” package on an Ubuntu server and then pull the Inventory on the server to a file in a created directory “/var/log/ocsinventory”. This will be done by a bash script. The DSC configuration works fine but is still under testing so please give me feedback if something could be done in another way. It is tested with Azure Automation DSC as a pull server.

DSC Configuration file

Just copy and pasted the script to a PowerShell file (ps1).

Configuration InventoryOMS
{
    Import-DSCResource -Module nx
    
    Node "ocsinventory"
    {
        nxPackage ocsinventoryInstall
        {
            Name = "ocsinventory-agent"
            Ensure = "Present"
            PackageManager = "Apt"
        }

        nxScript RunInventory
        {
            GetScript = @"
#!/bin/bash
if [ -f "/var/log/ocsinventory/$HOSTNAME"* ]; then echo "Inventory file exist"; else echo "Inventory file doesn't exist";fi
"@

            SetScript = @"
#!/bin/bash
ocsinventory-agent --local "/var/log/ocsinventory"
"@

            TestScript = @'
#!/bin/bash
if [ ! -d "/var/log/ocsinventory" ]; then mkdir "/var/log/ocsinventory";fi
filecount=`ls /var/log/ocsinventory/* | wc -l`
if [ $filecount -gt 0 ]
then
    d=$(date +"%Y-%m-%d")
    f=$(stat -c %z /var/log/ocsinventory/* | sed 's/^\([0-9\-]*\).*/\1/')
    if [[ "$d" != "$f" ]]
    then
        exit 1
    else
        exit 0
    fi
else
    exit 1
fi
'@
        }
    }
}

 

Install Desired State Configuration on Linux system and connect it to Azure Automation

I received a question if it is possible to only install DSC for Linux servers without installing OMS agent as I do in my post Easy installation and registration script for OMS and DSC on Linux.

To install PowerShell DSC manually it is just to download the Open Management Infrastructure (OMI) agent from https://collaboration.opengroup.org/omi, and then PowerShell DSC from https://github.com/Microsoft/PowerShell-DSC-for-Linux.

But what if you want to install the components by a script? On the web there is a couple of ways to do this and this is my way. In my script I will download the OMS agent and then use the OMI package in there to install OMI that is needed for DSC on Linux. This is because there is no way to download the OMI package from OpenGroup with wget.

The script is using dpkg as installer and is tested on Debian and Ubuntu. To use it on systems that support rpm packages, change the dpkg and .deb files.

#!/bin/bash

# DSC Variables
DSCKEY="DSC access key"
DSCURL="DSC URL

###########################################################
# Download and install OMI
# The script will use the OMI package in the OMS package
###########################################################
wget https://github.com/Microsoft/OMS-Agent-for-Linux/releases/download/v1.1.0-28/omsagent-1.1.0-28.universal.x64.sh
chmod 755 omsagent-1.1.0-28.universal.x64.sh
./omsagent-1.1.0-28.universal.x64.sh --extract
chmod 755 ./omsbundle* -R
mv ./omsbundle* ./omsbundle
dpkg -i ./omsbundle/100/omi-1.0.8-4.universal.x64.deb

###########################################################
# Download and install DSC
###########################################################
wget https://github.com/Microsoft/PowerShell-DSC-for-Linux/releases/download/v1.1.1-70/dsc-1.1.1.packages.tar.gz
chmod 755 dsc-1.1.1.packages.tar.gz

mkdir DSC
tar -xvf dsc-1.1.1.packages.tar.gz -C DSC
chmod 755 DSC -R

dpkg -i ./DSC/dsc-1.1.1-70.ssl_100.x64.deb

/opt/microsoft/dsc/Scripts/Register.py $DSCKEY $DSCURL

 

Add module in Azure Automation if DSC Configuration compilation fails

If you get the following error, see below, when compiling DSC Configuration in Azure Automation, it is probably because the module for the DSC Configuration is missing. In my example I need the “nx” module to compile the DSC configuration file. To solve the problem, add the module to the Azure Automation account.

Add module in Azure Automation if DSC Configuration compilation fails 1

Exception calling “NewScriptBlock” with “1” argument(s): “At line:4 char:5 + Import-DSCResource -Module nx + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The DSC engine could not load the module ‘nx’. It was not found on the system. At line:8 char:9 + nxPackage apache2Install + ~~~~~~~~~ Undefined DSC resource ‘nxPackage’. Use Import-DSCResource to import the resource. At line:15 char:9 + nxService apache2Service + ~~~~~~~~~ Undefined DSC resource ‘nxService’. Use Import-DSCResource to import the resource. At line:23 char:9 + nxFile apache2File + ~~~~~~ Undefined DSC resource ‘nxFile’. Use Import-DSCResource to import the resource.” (At line:4 char:5 + Import-DSCResource -Module nx + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The DSC engine could not load the module ‘nx’. It was not found on the system. At line:8 char:9 + nxPackage apache2Install + ~~~~~~~~~ Undefined DSC resource ‘nxPackage’. Use Import-DSCResource to import the resource. At line:15 char:9 + nxService apache2Service + ~~~~~~~~~ Undefined DSC resource ‘nxService’. Use Import-DSCResource to import the resource. At line:23 char:9 + nxFile apache2File + ~~~~~~ Undefined DSC resource ‘nxFile’. Use Import-DSCResource to import the resource.)

Add Module

Open the Azure Automation account and click Asset and then Modules.

Add module in Azure Automation if DSC Configuration compilation fails2

If you have a module package you can upload it to Azure Automation by clicking “Add a module” if it exists in the Gallery, you can download and install it from there. In my example I will click “Browse Gallery” and install it from there.

Add module in Azure Automation if DSC Configuration compilation fails3

Search for the module to install. In my example “nx”.

Add module in Azure Automation if DSC Configuration compilation fails4

Click the module to install and then “Import”.

Add module in Azure Automation if DSC Configuration compilation fails5

Now the module should be installed into the Azure Automation account and be ready to use.

Add module in Azure Automation if DSC Configuration compilation fails6

Try to compile the DSC Configuration file again to see if it works!

 

 

Easy installation and registration script for OMS and DSC on Linux

This is an easy Linux bash script to install and register OMS and DSC for Azure Automation. The script uses dpkg for installation of a .deb file so use it for Linux systems that can use the installation option.

Paste the script and save it into an install.sh file. Change the variables for the OMS and DSC installation and registration. Chmod the script with execution rights and then run it as ./install.sh.

#!/bin/bash

# OMS Variables
OMSKEY="OMS Workspace key"
OMSWS="OMS Workspace ID"

# DSC Variables
DSCKEY="DSC access key"
DSCURL="DSC URL"

###########################################
# Download and install OMS
###########################################
wget https://github.com/Microsoft/OMS-Agent-for-Linux/releases/download/v1.1.0-28/omsagent-1.1.0-28.universal.x64.sh
sha256sum ./omsagent-1.1.0-28.universal.x64.sh

chmod 755 ./omsagent-1.1.0-28.universal.x64.sh
./omsagent-1.1.0-28.universal.x64.sh --upgrade -w $OMSWS -s $OMSKEY

###########################################
# Download and install DSC
###########################################
wget https://github.com/Microsoft/PowerShell-DSC-for-Linux/releases/download/v1.1.1-70/dsc-1.1.1.packages.tar.gz
chmod 755 dsc-1.1.1.packages.tar.gz

mkdir DSC
tar -xvf dsc-1.1.1.packages.tar.gz -C DSC
chmod 755 DSC -R

dpkg -i ./DSC/dsc-1.1.1-70.ssl_100.x64.deb

/opt/microsoft/dsc/Scripts/Register.py $DSCKEY $DSCURL

Remember that the links in the script can change because of new agents.