2015-10-20 80 views
1

我们正在尝试使用Ansible在Azure上创建和安装软件包。我们可以使用Ansible Azure模块创建实例,但是我们一直在创建虚拟机之后安装包,因为我们不知道新创建的虚拟机的IP地址是什么。如何创建实例并使用Azure在azure上安装软件包

我们希望在单次运行中完成此操作。这可能吗?

回答

1

我还没有使用Azure模块,因此可能是错误的,但您应该可以使用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 
     ... 
+0

它的工作。但是,有没有办法对密码进行加密而不是提供文本? – Ram

+0

这是一个单独的问题本身,但通常你;看看使用[Vault](http://docs.ansible.com/ansible/playbooks_vault.html)或类似的东西。 – ydaetskcoR

+0

好的。非常感谢你的帮助。 – Ram