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
'@
        }
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.