- login a Linux VM. Assume the CentOS 7.5 is installed in this VM.
- install pre-requisite packages.
sudo yum install -y gcc libffi-devel python-devel openssl-devel epel-release
sudo yum install -y python-pip python-wheel
- install ansible
sudo pip install ansible[azure]
- create azure credentials file
mkdir ~/.azure
vim ~/.azure/credentials
- edit credentials file
[default]
subscription_id=<your-subscription_id>
client_id=<security-principal-appid>
secret=<security-principal-password>
tenant=<security-principal-tenant>
cloud_environment=AzureChinaCloud
#ad_user=<your-username>
#password=<your-password>
- create an ansible playbook file
vim ~/test.yml
- an example of ansible playbook for creating vm in azure
- name: Create Azure VM
hosts: localhost
connection: local
tasks:
- name: Create resource group
azure_rm_resourcegroup:
name: myResourceGroup
location: chinaeast
- name: Create virtual network
azure_rm_virtualnetwork:
resource_group: myResourceGroup
name: myVnet
address_prefixes: "10.0.0.0/16"
- name: Add subnet
azure_rm_subnet:
resource_group: myResourceGroup
name: mySubnet
address_prefix: "10.0.1.0/24"
virtual_network: myVnet
- name: Create public IP address
azure_rm_publicipaddress:
resource_group: myResourceGroup
allocation_method: Static
name: myPublicIP
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: myResourceGroup
name: myNetworkSecurityGroup
rules:
- name: SSH
protocol: Tcp
destination_port_range: 22
access: Allow
priority: 1001
direction: Inbound
- name: Create virtual network inteface card
azure_rm_networkinterface:
resource_group: myResourceGroup
name: myNIC
virtual_network: myVnet
subnet: mySubnet
public_ip_name: myPublicIP
security_group: myNetworkSecurityGroup
- name: Create VM
azure_rm_virtualmachine:
resource_group: myResourceGroup
name: myVM
vm_size: Standard_DS1_v2
admin_username: azureuser
ssh_password_enabled: false
ssh_public_keys:
- path: /home/azureuser/.ssh/authorized_keys
key_data: <your-ssh-public-key-data>
network_interfaces: myNIC
image:
offer: CentOS
publisher: OpenLogic
sku: '7.5'
version: latest
- execute this file
ansible-playbook test.yml
- the VM should be created after completing the script.
- create/update the hosts file
sudo vim /etc/ansible/hosts
- edit the hosts file
[testserver]
#testserver's ip address or domain name
10.0.2.4
test.com.cn
- create install.yml file
sudo vim ~/install.yml
13 edit install.yml file
---
- hosts: testserver
remote_user: testuser
roles:
- tester
- execute the file
ansible-playbook install.yml -b