【发布时间】:2021-04-14 09:53:43
【问题描述】:
我正在学习如何在 azure 上创建 Linux VM,并使用此链接 (https://docs.microsoft.com/en-us/azure/developer/ansible/vm-configure?tabs=ansible) 中的示例剧本在 azure 上创建一个 VM。如果我想用 ansible-playbook 完全像这样部署 10 个 VM,我应该怎么做?请帮忙。提前致谢
更新:我试过这样,但创建两个公共 IP 地址后脚本失败。
- name: Create Azure VM
hosts: localhost
connection: local
tasks:
- name: Create resource group to hold VM
azure_rm_resourcegroup:
name: TestingResource
location: eastus
- name: Create virtual network
azure_rm_virtualnetwork:
resource_group: TestingResource
name: testingvnet
address_prefixes: "10.0.0.0/16"
- name: Add subnet
azure_rm_subnet:
resource_group: TestingResource
name: testingsubnet
address_prefix: "10.0.1.0/24"
virtual_network: testingvnet
- name: Create public IP address
azure_rm_publicipaddress:
resource_group: TestingResource
allocation_method: Static
name: "{{ item }}" #CHANGE HERE
loop:
- testingpublicIP2
- testingpublicIP3
register: output_ip_address
#- name: Dump public IP for VM which will be created
#debug:
#msg: "The public IP is {{ output_ip_address.state.ip_address }}."
- name: Create Network Security Group that allows SSH
azure_rm_securitygroup:
resource_group: TestingResource
name: TestingSecurityGroup
rules:
- name: SSH
protocol: Tcp
destination_port_range: 22
access: Allow
priority: 1001
direction: Inbound
- name: Create virtual network interface card
azure_rm_networkinterface:
resource_group: TestingResource
name: "{{ item }}" #CHANGE HERE
loop:
- TestingNIC2
- TestingNIC3
virtual_network: testingvnet
subnet: testingsubnet
public_ip_name: "{{ item }}" #CHANGE HERE
loop:
- testingpublicIP2
- testingpublicIP3
security_group: TestingSecurityGroup
- name: Create VM
azure_rm_virtualmachine:
resource_group: TestingResource
name: "{{ item }}" #CHANGE HERE VM NAME
loop:
- TestingResource2
- TestingResource3
vm_size: Standard_B2s
admin_username: admin
admin_password: password@123
ssh_password_enabled: true
network_interfaces: "{{ item }}" #CHANGE HERE
loop:
- TestingNIC2
- TestingNIC3
image:
offer: UbuntuServer
publisher: Canonical
sku: '18.04-LTS'
version: latest
【问题讨论】:
-
请告诉我们你已经尝试过什么
-
嗨@NancyXiong,在示例 git playbook 文件中有类似这个名称的变量:“{{ resource_group }}”,那么 playbook 从哪里获取资源组的名称?还是我必须替换这些 {{}} 括号内的内容?
-
嗨@toydarian,我包括了我尝试过的剧本
-
关于这个问题的任何更新?它解决了你的问题吗?