我有一个简单的Powershell脚本,可以检查两件事情并将结果写入输出。我的想法是在Ansible Playbook中使用该输出作为将用于重启机器的变量,或者不使用(但在下面的示例中,我有一个PING测试命令)。
我的PS1
#Change computer description
$CDesc = Get-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\"
If($CDesc.GetValue("srvcomment") -eq $null) {
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\" -Name "srvcomment" -Value "PROD | AZI | V | APPLICA | SQL" -PropertyType String
} else {
$step1 = Write-Output "Step1"
}
#Check if CD drive is available
$CDletter = Get-WmiObject -Class Win32_CDROMDrive | select -ExpandProperty Drive
If($CDletter -eq $null) {
$step2 = Write-Output "Step2"
}
if (($step1) -and ($step2)) {Write-Host "already_updated"}
我的手册:
# Run post build scripts on machines taggedd with AnsibleVM1 from commad line variable with "{{ hosts }}"
# ansible-playbook test.yml -i azure_rm.py --extra-vars "hosts=displayName_AnsibleVM1" --ask-vault-pass -vvvv
- hosts: "{{ hosts }}"
gather_facts: no
vars_files:
- ./vars/vault.yml
- ./vars/vars.yml
tasks:
- include_vars: vault.yml
- name: test script
script: ./files/test.ps1
register: result
- set_fact: machineId={{result.stdout_lines.0}}
- win_ping:
when: machineId.already_ASDF
然而,PING(或即将成为REBOOT)模块仍在运行。这是我的输出:
TASK [test script] ****************************************************************************************************************************************************************
task path: /home/beefcake/cloudy/Ansible/test.yml:13
<51.x.x.x> ESTABLISH WINRM CONNECTION FOR USER: admin on PORT 5986 TO 51.x.x.x
EXEC (via pipeline wrapper)
EXEC (via pipeline wrapper)
<51.x.x.x> PUT "/home/beefcake/cloudy/Ansible/files/test.ps1" TO "C:\Users\admin\AppData\Local\Temp\ansible-tmp-1508512681.4-33957924788761\test.ps1"
EXEC (via pipeline wrapper)
EXEC (via pipeline wrapper)
changed: [AnsibleVM1] => {
"changed": true,
"failed": false,
"rc": 0,
"stderr": "",
"stdout": "already_updated\n",
"stdout_lines": [
"already_updated"
]
}
Read vars_file './vars/vault.yml'
Read vars_file './vars/vars.yml'
TASK [set_fact] *******************************************************************************************************************************************************************
task path: /home/beefcake/cloudy/Ansible/test.yml:17
ok: [AnsibleVM1] => {
"ansible_facts": {
"machineId": "already_updated"
},
"changed": false,
"failed": false
}
Read vars_file './vars/vault.yml'
Read vars_file './vars/vars.yml'
TASK [win_ping] *******************************************************************************************************************************************************************
task path: /home/beefcake/cloudy/Ansible/test.yml:19
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/windows/win_ping.ps1
<51.x.x.x> ESTABLISH WINRM CONNECTION FOR USER: admin on PORT 5986 TO 51.x.x.x
EXEC (via pipeline wrapper)
ok: [AnsibleVM1] => {
"changed": false,
"failed": false,
"ping": "pong"
}
META: ran handlers
META: ran handlers
PLAY RECAP ************************************************************************************************************************************************************************
AnsibleVM1 : ok=4 changed=1 unreachable=0 failed=0
任何想法我做错了什么?
答案 0 :(得分:0)
如果你想匹配字符串(例如,检查脚本的输出值是否已“更新”),你可以这样做:
- win_ping:
when: machineId == 'already_updated'
machineId没有属性,它是一个简单的字符串值。
为了保存步骤,您还可以:
- win_ping:
when: result.stdout_lines[0] == 'already_updated'
为了构建更复杂的Powershell逻辑并仍然与Ansible交互,你基本上有两个选择: