0

I launch a script which give me some informations on my devices but I have the result only in my Ansible Console. I would like to have this informations on a file on my control manager. In the script they are store in the variable $Ansible.Result.

How can I put it in a file ?

Code of my playbook.yaml

- name: firsttest
  hosts: win
  tasks:
   - name: Ping my Windows vm
     win_ping:

   - name: Run basic PowerShell script
     ansible.windows.win_powershell:
       script: |
         $hostname = hostname
         $domain = Get-NetFirewallProfile -Name Domain | Select-Object -ExpandProperty Enabled | Out-String
         $private = Get-NetFirewallProfile -Name Private | Select-Object -ExpandProperty Enabled | Out-String
         $public = Get-NetFirewallProfile -Name Public | Select-Object -ExpandProperty Enabled | Out-String
         $ipaddress = Get-NetIPAddress -AddressFamily IPv4 | Select-Object -ExpandProperty IPAddress | Out-String

         $myObject = [PSCustomObject]@{
          "Hostname" = $hostname
          "IPAdress" = $ipaddress
          "Domain" = $domain
          "Private" = $private
          "Public" = $public
         }
         $Ansible.Result = $myObject
     register: result

   - name: Show PS_output
     debug:
       var: result
Onyx
  • 45
  • 8

2 Answers2

0

The question pretty much duplicates Write variable to a file in Ansible, answer with using template module

So assuming that you don't follow not recommended use of variable in content parameter of copy module, then you should create result.j2 in templates/ with just one line {{ result }} inside this file and then use template module as given in answer above.

0

I just need to use delegate_to on my loopback address (127.0.0.1) like that this task can be execute on my control node (This task is execute for each asset present in my inventory).

   - name: Stock the result in a file on my control node
     lineinfile: 
       dest: "/home/user/mydata.txt"
       line: "{{ result }}"
       create: yes
     delegate_to: 127.0.0.1

The task above create a file txt on my control manager which contains my variable result.

Onyx
  • 45
  • 8