如何使用 ansible 在 azure 上创建实例和安装包

How to create instances and install packages on azure using ansible

我们正在尝试使用 Ansible 在 Azure 上创建和安装包。我们可以使用 Ansible Azure 模块创建实例,但是一旦创建了 VM,我们就无法安装软件包,因为我们不知道新创建的 VM 的 IP 地址是什么。

我们想一次性完成这个 运行。这可能吗?

我没有使用 Azure module so could be wrong but you should be able to use register 来存储有关您刚刚创建的实例的一些数据。

然后,您可以通过使用 add_host 模块迭代第一个任务的输出,将此数据传递到任务中动态定义的主机组。

因此您的剧本可能类似于:

- hosts: local
  connection: local
  tasks:
    - name  : Create Windows instance
      azure :
        name: "ben-Winows-23"
        hostname: "win123"
        os_type: windows
        enable_winrm: yes
        subscription_id: "{{ azure_sub_id }}"
        management_cert_path: "{{ azure_cert_path }}"
        role_size: Small
        image: 'bd507d3a70934695bc2128e3e5a255ba__RightImage-Windows-2012-x64-v13.5'
        location: 'East Asia'
        password: "xxx"
        storage_account: benooytes
        user: admin
        wait: yes
        virtual_network_name: "{{ vnet_name }}"
      register : azure

    - name  : Debug Azure output
      debug :
        var : azure

### Assumes that the output from the previous task has an instances key which in turn has a public_ip key. This may need updating to give the proper path to a resolvable hostname or connectable IP. Use the output of the debug task to help with this. ###

    - name     : Add new instance to host group
      add_host :
        hostname  : {{ item.public_ip }}
        groupname : launched
      with_items : azure.instances

### Only target newly launched instances from previous play ###

- hosts: launched
  tasks:
    - name        : Start foo service and make it auto start
      win_service :
        name       : foo
        start_mode : auto
        state      : started

    - name : Do some thing else
      ...