2

Looking for guidance how to execute powershell win_command with elevated privilege.

My playbook example:

    ---
- name: Run powershell script
  hosts: win
  gather_facts: false
  tasks:
    - name: windows test command
      win_command: powershell.exe -
      args: 
        stdin: ipconfig >> c:\ipconfig.txt

This works fine since it doesnt need elevated privilege, but if I try something that requires runas administrator I cannot seem to figure out, tried adding 'become_method: runas' no luck ?

1 Answer 1

7

It should work this way:

- name: Run powershell script
  hosts: win
  gather_facts: false
  become_method: runas

  vars:
    ansible_become_password: "{{ password }}"

  tasks:
    - win_command: powershell.exe -
      args: 
        stdin: ipconfig >> c:\ipconfig.txt
      become: yes
      become_user: Administrator
Sign up to request clarification or add additional context in comments.

1 Comment

Just to note, connecting (over winrm, in my case) as Administrator in the first place is not enough. The become: yes is still needed for some actions, such as installing KB3035131 on Windows 2012 (github.com/chocolatey-community/chocolatey-coreteampackages/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.