Need to Extract data from the Ansible debug out put

Hi ,

I have an playbook for Vmware Powercli :

tasks:

  • name: Run PowerShellGet VM script
    command: pwsh /tmp/vcsa_getVM.ps1
    ignore_errors: yes
    changed_when: false
    register: powershell_output_getVM

  • name: Debug GetVM output
    debug:
    msg:

  • “{{ powershell_output_getVM.stdout_lines }}”

Out put:

TASK [Debug GetVM output] ********************************************************************************************************************************************
ok: [localhost] => {
“msg”: [
[
“”,
“Name PowerState Num CPUs MemoryGB”,
“---- ---------- -------- --------”,
“BVI12DS2 PoweredOn 4 4.000”,
“BVI13PL1 PoweredOn 8 16.000”,
“BVI06ID1 PoweredOn 4 4.000”,
“BVI14PL1 PoweredOn 8 16.000”,
“BVI10VA1 PoweredOn 4 4.000”,
“BVI13CS1 PoweredOn 4 8.000”,

I want my o/p should only be list of the following:

BVI12DS2
BVI13PL1
BVI06ID1
BVI14PL1
BVI10VA1
BVI13CS1

Rakesh,

Either massage the data with a shell task using grep and cut, or write a custom info module in order to manipulate the data in Python.

This shell task might get you what you need, then use the value of 'stdout_lines'.

- shell: pwsh /tmp/vcsa_getVM.ps1 | grep -Ev '^(Name|-+)' | cut -d ' ' -f 1

You could use something like grep to extract it but you could also add the following in your powershell script

`

Select-Object -ExpandProperty Name

`

This must come after your Get-* cmdlet call, i.e. ‘Get-VMs | Select-Object -ExpandProperty Name’. This works by taking in the output from the cmdlet and outputting only the values of the Name property.

Thanks

Jordan

Hi Rakesh,

I agree with Sam and Jordan. You can take a look at this custom VMware powershell modules to write your own - https://github.com/Akasurde/ansible-vmware_update_manager

Thanks

Hi Jordan ,

Thanks for your suggestion .

It worked for some extent , now iam getting the o/p as below. Actually i am new to PowerCLI so dont have much idea on PowerCLi cmdlets, But is there a way to remove the inverted commas and Comma using the PowerCLi Commad.

“BVI12DS2”,
“BVI13PL1”,
“BVI06ID1”,
“BVI14PL1”,
“BVI10VA1”,
“BVI13CS1”,
“BVI03CA1”,
“BVI13SC2”,
“BVI12DB2”,
“BVI05CS2”,
“BVI04OR2”,

Is there a way to remove the Special charecters using the command input.

Hi Abhijeet and Sam,

Actually the Vcenter in my organisation has policy enforced not have SSL connectivity , so i cld nt use ansible vm-modules . That is why i had to switch to PWSH using ansible.
Jordan’s Suggestion worked . But i need to figure out how to remove special characters from o/p using PowerCLi cmds

Here is my script whcih i am using:

Connect-VIServer -Server x.x.x.x -User:xxxxx.com\yyyy -Password:zzzz | Out-Null
$result = Get-VM -Name BV* |Where-object {$_.powerstate -eq “poweredon”} | Select-Object -ExpandProperty Name
$result

If you want to remove “” via ansible , thats possible