I am brand new to Ansible Playbooks and I have checked a bunch of different documentation without success. So here is what I want to do. My company wants me to check to see if certain fields are present using PowerShell within winRM listeners and if they aren't they want me to make them present and if they are there they want me to skip that portion of the playbook.
- name: Windows test
hosts: all
gather_facts: true
tasks:
- name: Run basic PowerShell script
ansible.windows.win_powershell:
script: |
$winRM = Get-WSManInstance -ResourceURI winrm/config/listener -SelectorSet @{Address="*";Transport="https"}
$winRMPort = $winRM.Port
$winRMTransport = $winRM.Transport
$winRMThumbprint = $winRM.CertificateThumbprint
if($winRMPort -ne $null){
#task goes in here
}
else{
#task goes in here
}
My question is can I put a task within the if statement to tell Ansible to run that task if it is there and don't if it isn't? If now I am not sure how I would go about doing this. My code below doesn't have the entire logic for the if statement just yet but I am just trying to test before I go further with it. I have tried embedding it and it comes back with an error so I am thinking that I either am not doing it right or it can't be done at all. Any help is appreciated. Thank you.
CodePudding user response:
You cannot run/execute ansible task/module within powershell script. But you can convert the task into powershell commands/script and run it within powershell script instead of ansible task/module.
CodePudding user response:
Since you are using win_shell, it is all PowerShell from it and not much Ansible. You could use the PS1 logic to check for condition on the WinRM port and then execute the condition.
Moreover, Ansible is idempotent so it the state is present it would by default skip the code where the condition is for "present".
Refer https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_powershell_module.html for more details.