Batchen Regev
Batchen Regev

Reputation: 1013

Ansible how to run powershell script

Im using ansible 2.9.2 and i want to run a script that creates user on my windows machine, The script works if i run in my powershell localy like this :

powershell.exe -ExecutionPolicy Bypass -File create_user.ps1 -pass "{{ pass }}" -user "{{ user }}" -gr "{{ gr }}"

Playbook:

  tasks:
    - name: "Createing windows user"
      local_action:
        module: vmware_vm_shell
        cluster: "{{ cluster }}"
        datacenter: "{{ datacenter }}"
        hostname: "{{ vcenter_server }}"
        username: "{{ vcenter_user  }}"
        password: "{{ vcenter_pass }}"
        vm_id: "{{vm_name}}"
        vm_username: "{{ vm_username }}"
        vm_password: "{{ vm_password }}"
        vm_shell: 'C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe'
        vm_shell_args: powershell.exe -ExecutionPolicy Bypass -File create_user.ps1 -pass "{{ pass }}" -user "{{ user }}" -gr "{{ gr }}"
        vm_shell_cwd: 'C:\Users\myuser\Desktop'
        wait_for_process: yes
        validate_certs: no
      register: info
    - debug:
            msg: "{{ info }}"

From playbook i dont get any error but user is not being created, manually with same command all works. is it because i use powershell as admin localy ? is there a way to do it here?

here is the output:

ok: [localhost] => {
    "msg": {
        "changed": true,
        "cmd_line": "\"C:\\Windows\\System32\\WindowsPowershell\\v1.0\\powershell.exe\" powershell.exe -ExecutionPolicy Bypass -File create_user.ps1 -pass \"********7\" -user \"Tomy\" -gr \"Localgroup\"",
        "end_time": "2020-01-14T13:44:32+00:00",
        "exit_code": 0,
        "failed": false,
        "name": "powershell.exe",
        "owner": "myuser",
        "start_time": "2020-01-14T13:44:31+00:00",
        "uuid": "4256795-43ec-87c2-fd4bf-5dffv6f2a4"
    }
}
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Upvotes: 0

Views: 2302

Answers (1)

Batchen Regev
Batchen Regev

Reputation: 1013

After upgrading from vmtools version:10346 to version:11265 All works!

And i have made this changes in my playbook:

  tasks:
  - name: Get all tags
    vmware_vm_shell:
      cluster: "{{ cluster }}"
      datacenter: "{{ datacenter }}"
      hostname: '{{ vcenter_hostname }}'
      username: '{{ vcenter_username }}'
      password: '{{ vcenter_password }}'
      folder: "{{ folder }}"
      vm_id: "{{ name }}"
      vm_username: "{{ vm_username }}"
      vm_password: "{{ vm_password }}"
      vm_shell: 'C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe'
      vm_shell_args: -ExecutionPolicy Bypass -File create_user.ps1 -pass "{{ pass_word }}" -user "{{ user }}" -gr "{{ gr }}"
      vm_shell_cwd: 'C:\Users\Administrator\Desktop'
      wait_for_process: yes
      validate_certs: no
    delegate_to: localhost

Upvotes: 1

Related Questions