Pushing a DSC configuration to a Linux node

Once DSC for Linux is fully installed and set up, we can begin to use DSC configurations on the Linux node. The following is an example DSC Configuration for a Linux node that ensures a very important file is present:

Configuration LinuxDemoSetup
{
Import-DSCResource -Module nx

Node 'localhost'
{
nxFile myTestFile
{
Ensure = "Present"
Type = "File"
DestinationPath = "/tmp/dsctest"
Contents = "This is my DSC Test!"
}
}
}

Note how it looks exactly like the DSC configurations we've been using in the entire book. The promises we read about in the first chapter of using a DSL for multiple platforms without learning new syntax or systems has finally come true.

In order to push this DSC Configuration to our Linux target node, we'll put it in a .ps1 file and use the knowledge we gained in the previous chapter to extract the configuration data to a manifest file. Then, we'll compile the DSC Configuration into an MOF for deployment. Since DSC uses the same transmission protocols regardless of whether it's on Windows or Linux, we can author and compile the DSC Configuration scripts on our Windows desktop. This is the code to do that, and in the downloadable source code for this book is the full source for the DSC Configuration and attendant files:

$outputPath = ([IO.Path]::Combine($PSScriptRoot, 'LinuxDemoSetup'))
$dataScript = ([IO.Path]::Combine($PSScriptRoot, 'linux_config_data.ps1'))
$configData = &$dataScript

.linux_configuration.ps1 -OutputPath $outputPath -ConfigData $configData

This approach should look familiar; we used the same approach in the previous chapter to install DSC Pull Servers.

We can use the built-in remoting capabilities of the DSC cmdlets to push our compiled DSC Configuration to our Linux server. This should look familiar to the approaches we covered in Chapter 6, Pushing DSC Configurations, with the exception of using specific settings and options to control the remoting session:

$password = "password" | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential('root',$password)
$CIOpt = New-CimSessionOption -SkipCACheck -SkipCNCheck `
-SkipRevocationCheck -Encoding Utf8 -UseSsl
$CimSession = New-CimSession -Authentication Basic -Credential $credential `
-ComputerName 'dsc-box2' -port 5986 `
-SessionOption $CIOpt

With the remoting session set up and connected, we can now interrogate the remote target node to ensure we can manage it with DSC:

[PS]> Get-CimInstance -CimSession $CimSession -namespace root/omi -ClassName omi_identify

InstanceID : 2FDB5542-5896-45D5-9BE9-DC04430AAABE
SystemName : dsc-box2
ProductName : OMI
ProductVendor : Microsoft
ProductVersionMajor : 1
ProductVersionMinor : 1
ProductVersionRevision : 0
ProductVersionString : 1.1.0-0
Platform : LINUX_X86_64_GNU
OperatingSystem : LINUX
Architecture : X86_64
Compiler : GNU
ConfigPrefix : GNU
ConfigLibDir : /opt/omi/lib
ConfigBinDir : /opt/omi/bin
ConfigIncludeDir : /opt/omi/include
ConfigDataDir : /opt/omi/share
ConfigLocalStateDir : /var/opt/omi
ConfigSysConfDir : /etc/opt/omi/conf
ConfigProviderDir : /etc/opt/omi/conf
ConfigLogFile : /var/opt/omi/log/omiserver.log
ConfigPIDFile : /var/opt/omi/run/omiserver.pid
ConfigRegisterDir : /etc/opt/omi/conf/omiregister
ConfigSchemaDir : /opt/omi/share/omischema
ConfigNameSpaces : {root-omi, root-Microsoft-DesiredStateConfiguration,
root-Microsoft-Windows-DesiredStateConfiguration}
PSComputerName : dsc-box2

Satisfied that we can connect and interrogate the target node, we can now push our DSC Configuration out to the Linux node:

[PS]> Start-DSCConfiguration -CimSession $CimSession -wait -Verbose -Path $outputPath
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' =
SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' =
root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 0.225 seconds

We can verify that the DSC Configuration we pushed was applied by running Get-DSCConfiguration and Test-DSCConfiguration:

[PS]> Get-DSCConfiguration -CimSession $CimSession

DestinationPath : /tmp/dsctest
SourcePath :
Ensure : present
Type : file
Force : False
Contents : This is my DSC Test!
Checksum :
Recurse : False
Links : follow
Group : root
Mode : 644
Owner : root
ModifiedDate : 8/10/2017 2:51:11 AM
PSComputerName : dsc-box2
CimClassName : MSFT_nxFileResource

[PS]> Test-DscConfiguration -CimSession:$CimSession
True

We can verify this manually by logging onto the Linux server and checking the file contents:

[vagrant@dsc-box2 ~]$ cat /tmp/dsctest
This is my DSC Test![vagrant@dsc-box2 ~]$

With a little extra effort, you are now able to manage the Linux nodes in your environment from your desktop, just like any other Windows node.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset