Need to Extract data from the Ansible debug out put

30 views
Skip to first unread message

Rakesh Parida

unread,
Nov 13, 2019, 11:40:06 AM11/13/19
to Ansible Project
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  

Sam Doran

unread,
Nov 13, 2019, 2:11:16 PM11/13/19
to ansible...@googlegroups.com
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

---

Sam

Jordan Borean

unread,
Nov 13, 2019, 3:14:39 PM11/13/19
to Ansible Project
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

Abhijeet Kasurde

unread,
Nov 13, 2019, 11:28:18 PM11/13/19
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cfaa8c15-8b6e-48b7-9d20-90f17f08713b%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Rakesh Parida

unread,
Nov 14, 2019, 8:17:31 AM11/14/19
to Ansible Project
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.

Rakesh Parida

unread,
Nov 14, 2019, 8:21:10 AM11/14/19
to Ansible Project
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 


On Thursday, November 14, 2019 at 9:58:18 AM UTC+5:30, Abhijeet Kasurde wrote:
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

On Thu, Nov 14, 2019 at 1:44 AM Jordan Borean <jbor...@gmail.com> wrote:
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Rakesh Parida

unread,
Nov 14, 2019, 8:23:05 AM11/14/19
to Ansible Project
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

Vandana Thakur

unread,
Nov 14, 2019, 8:25:48 AM11/14/19
to ansible...@googlegroups.com
If you want to remove “” via ansible , thats possible

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6b6794ad-28d5-4373-8811-f5864b7a8d74%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages