通过Ansible

时间:2017-10-06 09:03:46

标签: powershell azure ansible azure-resource-manager

我通过Ansible playbook部署Azure ARM模板似乎工作正常,但我希望在部署机器后添加运行2x Powershell脚本的功能。当通过ARM模板部署机器时,我已经运行了自定义脚本扩展,但我还希望之后再运行2个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   

据我所知,playbook script 选项应该将脚本发送到机器并在本地运行,但由于某种原因它找不到我在剧本的子文件夹中的脚本。

文件夹结构:

.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

1 个答案:

答案 0 :(得分:0)

ansible正在尝试将文件从localhost复制到localhost。因为该游戏的范围是localhost。

我想你在启动剧本时没有在hosts文件中拥有该主机 您需要将主机添加到该主机的ansible和scope脚本任务。

您可以创建另一个Playbook来执行此操作,也可以在当前步骤中添加add_host步骤。

- add_host:
    name: name

要将任务范围扩展到新主机,我正在使用import_playbook指令,该指令会导入另一个作用于相关主机的Playbook。可能有更好的方法。