Using Ansible to create VM in Azure and install application

  1. login a Linux VM. Assume the CentOS 7.5 is installed in this VM.
  2. 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
  1. install ansible
sudo pip install ansible[azure]
  1. create azure credentials file
mkdir ~/.azure
vim ~/.azure/credentials
  1. edit credentials file
[default]
subscription_id=
client_id=
secret=
tenant=
cloud_environment=AzureChinaCloud
#ad_user=
#password=
  1. create an ansible playbook file
vim ~/test.yml
  1. 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: 
      network_interfaces: myNIC
      image:
        offer: CentOS
        publisher: OpenLogic
        sku: '7.5'
        version: latest
  1. execute this file
ansible-playbook test.yml
  1. the VM should be created after completing the script.
  2. create/update the hosts file
sudo vim /etc/ansible/hosts
  1. edit the hosts file
[testserver]
#testserver's ip address or domain name
10.0.2.4
test.com.cn
  1. create install.yml file
sudo vim ~/install.yml

13 edit install.yml file

---
- hosts: testserver
  remote_user: testuser
  roles:
    - tester
  1. execute the file
ansible-playbook install.yml -b

你可能感兴趣的:(Using Ansible to create VM in Azure and install application)