Expand variable in PowerShell DSC File resource content property - windows

I have $Node.NodeName, $Node.Environment, $Node.Role defined in the following DSC config file. They don't appear to expand to the correct values in the MOF file and instead shows:
HOSTNAME: System.Collections.Hashtable
env: System.Collections.Hashtable
role: System.Collections.Hashtable
I was expecting each one to expand to the corresponding value as defined under AllNodes. $SQLUser and $SQLPassword display the correct passed in values.
CreateDBFile -SQLUser "myuser" -SQLPassword "mypassword" -ConfigurationData inventory.psd1 -OutputPath "C:\DSC"
inventory.psd1
#{
AllNodes = #(
#{
NodeName = "QA-WEB01"
NodeIP = "10.48.200.19"
Role = "web"
Environment = "QA"
},
#{
NodeName = "QA-WEB02"
NodeIP = "10.48.200.20"
Role = "Web"
Environment = "UAT"
}
)
}
CreateDBFile.ps1
Configuration CreateDBFile
{
param(
[String]$SQLUser,
[String]$SQLPassword
)
Node $AllNodes.Where{$_.Role -contains "web" -and $_.NodeIP -contains "10.48.200.19"}.NodeName
{
File FileExample
{
Ensure = 'Present'
Contents = "integrations:
- name: nri-mssql
env:
HOSTNAME: $Node.NodeName
USERNAME: $SQLUser
PASSWORD: $SQLPassword
PORT: 1433
ENABLE_SSL: false
TRUST_SERVER_CERTIFICATE: true
TIMEOUT: 15
interval: 15s
labels:
env: $Node.Environment
role: $Node.Role
inventory_source: config/mssql"
DestinationPath = 'D:\Logs\\mssql-config.yml'
Type = 'File'
}
}
}

Here is the working CreateDBFile.ps1 file:
Configuration CreateDBFile
{
param(
[String]$SQLUser,
[String]$SQLPassword
)
Node $AllNodes.Where{$_.Role -contains "web" -and $_.NodeIP -contains "10.48.200.19"}.NodeName
{
File FileExample
{
Ensure = 'Present'
Contents = "integrations:
- name: nri-mssql
env:
HOSTNAME: $($Node.NodeName)
USERNAME: $SQLUser
PASSWORD: $SQLPassword
PORT: 1433
ENABLE_SSL: false
TRUST_SERVER_CERTIFICATE: true
TIMEOUT: 15
interval: 15s
labels:
env: $($Node.Environment)
role: $($Node.Role)
inventory_source: config/mssql"
DestinationPath = 'D:\Logs\\mssql-config.yml'
Type = 'File'
}
}
}

Related

Ansible playbook not looping through device_list

I am trying to loop variables into a playbook, but I keep getting the following error
TASK [Add policy package to model device] ********************************************************************************************************************************************************************
failed: [fmg01] (item={'device_name': 'FGT1', 'device_ip': '192.168.0.103', 'group_name': 'Branch_Office', 'policy_package': 'default', 'device_serial': 'FGVM01TM12345678'}) => {"ansible_loop_var": "item", "changed": false, "item": {"device_ip": "192.168.0.103", "device_name": "FGT1", "device_serial": "FGVM01TM12345678", "group_name": "Branch_Office", "policy_package": "default"}, "meta": {"status": {"code": -10, "message": "The data is invalid for selected url"}, "url": "/pm/pkg/adom/root"}, "unreachable": false}
Below is my YML playbook. It works just fine when I use static variables (write out a single IP address and serial number as a string)
---
- name: Add model device to FMG and install Policy Package
hosts: fmg01
# gather_facts: no
connection: httpapi
collections:
- fortinet.fortimanager
vars_files:
- inventory.yml
tasks:
- name: Add model device
fmgr_dvm_cmd_add_device:
loose_validation: true
method: exec
params:
- data:
adom: root
device:
# device action: add_model
mgmt_mode: 'fmg'
#os_ver: 6
#mr: 4
sn: '{{item.device_serial}}'
adm_pass: 'password'
adm_usr: 'admin'
ip: '{{item.device_ip}}'
loop: '{{device_list}}'
- name: Add policy package to model device
fmgr_pm_pkg_adom_obj:
loose_validation: true
method: set
url_params:
adom: root
params:
- data:
name: default
scope member:
- name: '{{item.device_serial}}'
vdom: root
type: pkg
loop: '{{device_list}}'
- name: Install policy package to model device
fmgr_securityconsole_install_package:
loose_validation: true
method: exec
params:
- data:
adom: root
pkg: default
scope:
- name: '{{item.device_serial}}'
vdom: root
loop: '{{device_list}}'
Here is my inventory.yml file. I tested this ok with the first task. It passed in the IP address in ok. I tested the other tasks with the device_serial parameter, but that did not work.
#feeds into playbook
#add_device / add_device_group
device_list:
- device_name: FGT1
device_ip: '192.168.0.103'
group_name: Branch_Office
policy_package: default
device_serial: 'FGVM01TM12345678'
Problem turned out to be an indention problem with the params portion
- name: Add policy package to model device
fmgr_pm_pkg_adom_obj:
loose_validation: true
method: set
url_params:
adom: root
params:
- data:
name: default
scope member:
- name: '{{item.device_serial}}'
vdom: root
type: pkg
loop: '{{device_list}}'

Ansible conditionals on list variables passed into a role

I have following ansible code
- hosts: localhost
roles:
- { role: roleone }
- { role: roletwo, myvariable : ["var1","var2"] }
I need to set this list variable based on conditional.
Below is what I have tried, although it does not work:
- hosts: localhost
roles:
- { role: roleone }
- { role: roletwo, myvariable : ["foo1","foo2"], when: init=="true", myvariable : ["bar1","bar2"], when: init == "false" }
I was able to achieve this by using following code:
---
- hosts: all
roles:
- { role: roleone }
- { role: roletwo, myvariable: "{{ ['var1','var2'] if (init == "true") else ['bar1','bar2'] }}" }

Ansible handler for restarting Docker Swarm service

I need to restart containers of a Docker Swarm service with Ansible.
The basic definition looks like this:
# tasks/main.yml
- name: 'Create the service container'
docker_swarm_service:
name: 'service'
image: 'service'
networks:
- name: 'internet'
- name: 'reverse-proxy'
publish:
- { target_port: '80', published_port: '80', mode: 'ingress' }
- { target_port: '443', published_port: '443', mode: 'ingress' }
- { target_port: '8080', published_port: '8080', mode: 'ingress' }
mounts:
- { source: '{{ shared_dir }}', target: '/shared' }
replicas: 1
placement:
constraints:
- node.role == manager
restart_config:
condition: 'on-failure'
user: null
force_update: yes
So I thought that
# handlers/main.yml
- name: 'Restart Service'
docker_swarm_service:
name: some-service
image: 'some-image'
force_update: yes
should work as a handler but it seems that it's not taking all options.
So any advice how to properly restart containers of a Docker Swarm service?

import an variable file to another in ansible

I am new to ansible.
I have 2 variable files one is global and another is environment specific.
Here is the global one in global/group_var/all.yaml folder:
rel:
deployment:
webui:
dockerName: "rel"
dockerTag: "Dev_{{ travis.build_number }}"
And the specific one is in develop/group_var/all.yaml folder
docker:
registery: "xxxx"
is there any way I can import global/group_var/all.yaml to develop/group_var/all.yaml
import global/group_var/all.yaml
docker:
registery: "xxxx"
so develop/group_var/all.yaml becomes:
rel:
deployment:
webui:
dockerName: "rel"
docker:
registery: "xxxx"
dockerTag: "Dev_1111"
Thanks
It's possible to read the global variables into a dictionary. For example
$ cat develop/group_var/all.yaml
global: "{{ lookup('file', 'global/group_var/all.yaml')|from_yaml }}"
docker:
registery: "xxxx"
the playbook
- hosts: localhost
tasks:
- include_vars: develop/group_var/all.yaml
- debug:
var: global
- debug:
var: docker
gives
"global": {
"rel": {
"deployment": {
"webui": {
"dockerName": "rel"
}
}
}
}
"docker": {
"registery": "xxxx"
}

Win32_PnPSignedDriver only have 5 properties

I have some problem with querying the Win32_PnPSignedDriver.
Here is the code that i use to querying the PnPSignedDriver properties.
string query = "Select * from Win32_PnPSignedDriver";
tbLog.Clear();
ManagementObjectSearcher objSearcher = new ManagementObjectSearcher( "root\\CIMV2", query );
ManagementObjectCollection objCollection = objSearcher.Get();
var managementObject = objCollection.OfType<ManagementObject>().FirstOrDefault();
PropertyDataCollection props = managementObject.Properties;
foreach ( PropertyData prop in props )
{
tbLog.AppendText( String.Format( "Property name: {0}\r\n", prop.Name ) );
}
When I run the query on my computer, the result is:
Property name: Caption
Property name: ClassGuid
Property name: CompatID
Property name: CreationClassName
Property name: Description
Property name: DeviceClass
Property name: DeviceID
Property name: DeviceName
Property name: DevLoader
Property name: DriverDate
Property name: DriverName
Property name: DriverProviderName
Property name: DriverVersion
Property name: FriendlyName
Property name: HardWareID
Property name: InfName
Property name: InstallDate
Property name: IsSigned
Property name: Location
Property name: Manufacturer
Property name: Name
Property name: PDO
Property name: Signer
Property name: Started
Property name: StartMode
Property name: Status
Property name: SystemCreationClassName
Property name: SystemName
But when i ran the same code on the production machine the result is:
Property name: DriverDate Property name: DriverDesc
Property name: DriverVersion Property name: Index Property
name: ProviderName
Does anyone know why I have 2 different results?
Does this means that the WMI is corrupt and I need to rebuild the WMI on the production machine?
Thanks,
Fitriadi

Resources