我通过Ansible部署了一个Azure ARM模板,似乎工作正常,但是我希望在部署计算机后添加运行2个Powershell脚本的功能。当通过ARM模板部署计算机时,我已经有了一个自定义脚本扩展,但是我也希望之后再运行2个Powershell脚本。无法在通过Ansible部署ARM模板后运行2x Powershell脚本
我的剧本:
---
- name: Deploy Azure ARM template.
hosts: localhost
connection: local
gather_facts: false
vars_files:
- ./vars/vault.yml
- ./vars/vars.yml
tasks:
- include_vars: vault.yml
- name: Create Azure Deploy
azure_rm_deployment:
client_id: "{{ client_id }}"
secret: "{{ secret }}"
subscription_id: "{{ subscription_id }}"
tenant: "{{ tenant }}"
state: present
resource_group_name: AnsibleTest1
location: UK South
template: "{{ lookup('file', 'WindowsVirtualMachine.json') }}"
parameters: "{{ (lookup('file', 'WindowsVirtualMachine.parameters.json') | from_json).parameters }}"
- name: Run powershell script
script: files/helloworld1.ps1
- name: Run powershell script
script: files/helloworld2.ps1
而且后成功部署模板中的错误:
TASK [Run powershell script] ***************************************************************************************************************************************************************************
task path: /home/beefcake/.ansible/azure-json-deploy.yml:25
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: beefcake
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '(umask 77 && mkdir -p "` echo /home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230 `" && echo ansible-tmp-1507219682.48-53342098196230="` echo /home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230 `") && sleep 0'
<127.0.0.1> PUT /home/beefcake/.ansible/files/helloworld1.ps1 TO /home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230/helloworld1.ps1
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230/ /home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230/helloworld1.ps1 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c ' /home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230/helloworld1.ps1 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230/ > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: FAILED! => {
"changed": true,
"failed": true,
"msg": "non-zero return code",
"rc": 127,
"stderr": "/home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230/helloworld1.ps1: 1: /home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230/helloworld1.ps1: =: not found\n/home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230/helloworld1.ps1: 2: /home/beefcake/.ansible/tmp/ansible-tmp-1507219682.48-53342098196230/helloworld1.ps1: Set-Content: not found\n",
"stdout": "",
"stdout_lines": []
}
to retry, use: --limit @/home/beefcake/.ansible/azure-json-deploy.retry
PLAY RECAP *********************************************************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=1
据我所知道的,剧本脚本选项应该发送脚本该机器在本地运行,但出于某种原因,它无法在剧本的子文件夹中找到我所拥有的脚本。
文件夹结构:
.ansible (folder)
- ansible.cfg
- azure-json-deploy.yml
- azure_rm.ini
- azure_rm.py
- WindowsVirtualMachine.json
- WindowsVirtualMachine.parameters.json
- vars (folder)
- vars.yml
- vault.yml
- files (folder)
- helloworld1.ps1
- helloworld2.ps1
我这么想吗?
编辑 这是我创建的第二个剧本,其中4c74356b41建议我去做。
---
# This playbook tests the script module on Windows hosts
- name: Run powershell script 1
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Run powershell script
script: files/helloworld1.ps1
# This playbook tests the script module on Windows hosts
- name: Run powershell script 2
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Run powershell script
script: files/helloworld2.ps1
仍然生成相同的错误:
fatal: [localhost]: FAILED! => {
"changed": true,
"failed": true,
"msg": "non-zero return code",
"rc": 127,
"stderr": "/home/beefcake/.ansible/tmp/ansible-tmp-1507288326.16-187870805725578/helloworld1.ps1: 1: /home/beefcake/.ansible/tmp/ansible-tmp-1507288326.16-187870805725578/helloworld1.ps1: =: not found\n/home/beefcake/.ansible/tmp/ansible-tmp-1507288326.16-187870805725578/helloworld1.ps1: 2: /home/beefcake/.ansible/tmp/ansible-tmp-1507288326.16-187870805725578/helloworld1.ps1: Set-Content: not found\n",
"stdout": "",
"stdout_lines": []
}
to retry, use: --limit @/home/beefcake/.ansible/azure-json-deploy.retry
在能够使用Ansible之前,您需要在Windows计算机上启用WinRM,修改网络接口上的连接配置文件,可能配置固件(Windows和Azure)以及可选配置SSL证书。从你的帖子和编辑来看,你可能不会在你的模板中使用它,你可能不知道这一点。至于运行PowerShell脚本 - 我会建议修改模板以使用DSC扩展而不是Ansible(无论如何您都需要为WinRM连接准备实例) - 在GitHub Azure快速入门模板上有示例。 – techraf