Puppet Azure provisioning

introduction

Creating the instance and deploying the webserver where these all were provisioned over the cloud virtualization on Azure using Puppet.

Its credentials were exported using export commands. Provisioning is done through puppet manifest. 


Pre-requisites

Before creating a manifest directly to provision instance in AZURE we need to follow some pre requesting steps that are needed for this process,

As Usual, procedurally install the module on the puppet machine from puppet forge.

Install the module  on the puppet machine,

[root@puppetmaster ~]# puppet module install puppetlabs-azure_arm --version 0.2.2

Notice: Preparing to install into /etc/puppetlabs/code/environments/production/modules ...

Notice: Downloading from https://forgeapi.puppet.com ...

Notice: Installing -- do not interrupt ...

/etc/puppetlabs/code/environments/production/modules

└── puppetlabs-azure_arm (v0.2.2)


Integrating  puppet with azure with credentials,

Go  to the given link in the browser and give the code for azure CLI login,


You can  now use the azure cli,

Credentials

Export the credentionals for using azure cli,

export azure_subscription_id=’xxxx'
export azure_tenant_id='xxxx'
export azure_client_id='xxxx'
export azure_client_secret='xxxx'

PROVISIONG THE INSTANCE IN AZURE

[root@puppetmaster centos]# cd /etc/puppetlabs/code/environments/production/modules/azure_arm/

[root@puppetmaster azure_arm]# cd  examples/

[root@puppetmaster examples]#

# Demo script for quickly spinning up a Linux node
# which is auto-classified with role "sample_website"
#$role = 'sample_website'
#$master_ip = '220.158.142.58'

$master_url = "https://puppetmaster.zippyops.com:8140/packages/current/install.bash"

$instance_name = 'azure-sample-host'

$user_data = @("USERDATA"/L)
#! /bin/bash
hostnamectl set-hostname puppetazure.zippyops.com
echo "220.158.142.58 puppetmaster.zippyops.com" >> /etc/hosts
yum install curl -y
curl -k $master_url | sudo bash
| USERDATA

$base_name = 'zippyops'
$rg = "${base_name}-rg"
$storage_account = "${base_name}saccount"
$nsg = "${base_name}-nsg"
$vnet = "${base_name}-vnet"
$subnet = "${base_name}-subnet"
$publicip = "${base_name}-publicip"
$location = 'uksouth'
$subscription_id = 'xxxxx'

# Base names for the vm's
$nic_base_name = "${base_name}-nic"
$vm_base_name = "${base_name}-vm"

azure_resource_group { $rg:
ensure => present,
parameters => {},
location => $location
}

azure_storage_account { $storage_account:
ensure => present,
parameters => {},
resource_group_name => $rg,
account_name => $storage_account,
location => $location,
sku => {
name => 'Standard_LRS',
tier => 'Standard',
}
}

azure_network_security_group { $nsg :

ensure => present,
parameters => {},
resource_group_name => $rg,
location => $location,
properties => {
securityRules => [
{ name => 'http',
properties => {
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '8080',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 100,

direction => 'Inbound',
}
},
{ name => 'ssh',
properties => {
description => 'Allow SSH',
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '22',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 201,
direction => 'Inbound',
},
},

{ name => 'https',
properties => {
description => 'MCollective',
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '443',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 103,
direction => 'Inbound',
},
},
]
}
}

azure_virtual_network { $vnet:
ensure => present,
parameters => {},
location => $location,
resource_group_name => $rg,
properties => {
addressSpace => {
addressPrefixes => ['10.0.0.0/24', '10.0.2.0/24']
},
dhcpOptions => {
dnsServers => ['8.8.8.8', '8.8.4.4']
},
subnets => [
{
name => $subnet,
properties => {
addressPrefix => '10.0.0.0/24'
}
}]
}
}

azure_subnet { $subnet:
ensure => present,
subnet_parameters => {},
virtual_network_name => $vnet,
resource_group_name => $rg,
properties => {
addressPrefix => '10.0.0.0/24',
networkSecurityGroup => {
properties => {

},

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/networkSecurityGroups/${nsg}"

}
}
}

# Public IP Address
azure_public_ip_address { $publicip:
ensure => present,
location => $location,
resource_group_name => $rg,
subscription_id => $subscription_id,

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/publicIPAddresses/${publicip}",

parameters => {
idleTimeoutInMinutes => '10',
},
}

# Create multiple NIC's and VM's
azure_network_interface { $nic_base_name:
ensure => present,
parameters => {},
resource_group_name => $rg,
location => $location,
properties => {
ipConfigurations => [{
properties => {
privateIPAllocationMethod => 'Dynamic',
publicIPAddress => {

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/publicIPAddresses/${publicip}",

},
subnet => {

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/virtualNetworks/${vnet}/subnets/${subnet}", #lint:ignore:140chars

properties => {
addressPrefix => '10.0.0.0/24',
provisioningState => 'Succeeded'
},
name => $subnet
},
},
name => "${base_name}-nic-ipconfig"
}]
}
}



azure_virtual_machine { $vm_base_name:
ensure => present,
parameters => {},
location => $location,
resource_group_name => $rg,
properties => {
hardwareProfile => {
vmSize => 'Standard_D4s_v3'
},
storageProfile => {
imageReference => {
offer => 'CentOS',
publisher => 'OpenLogic',
sku => '7-CI',
version => 'latest'
},
osDisk => {
name => $vm_base_name,
createOption => 'FromImage',
caching => 'None',
vhd => {
uri => "https://${$storage_account}.blob.core.windows.net/${vm_base_name}-container/${vm_base_name}.vhd"
}
},
dataDisks => []
},


osProfile => {
computerName => $vm_base_name,
adminUsername => 'zippyops',
adminPassword => 'Devops!',
linuxConfiguration => {
disablePasswordAuthentication => false
}
},
networkProfile => {
networkInterfaces => [
{

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/networkInterfaces/${nic_base_name}", #lint:ignore:140chars

primary => true
}]
},
},
type => 'Microsoft.Compute/virtualMachines',
}


azure_virtual_machine_extension { 'script' :
type => 'Microsoft.Compute/virtualMachines/extensions',
extension_parameters => '',
location => $location,
tags => {
displayName => "${vm_base_name}/script",
},
properties => {
protectedSettings => {
commandToExecute => $user_data,
},
publisher => 'Microsoft.Azure.Extensions',
type => 'CustomScript',
typeHandlerVersion => '2.0',
},
resource_group_name => $rg,
subscription_id => $subscription_id,
vm_extension_name => 'script',
vm_name => $vm_base_name,
}


 For Ubuntu instance,

# Demo script for quickly spinning up a Linux node

# which is auto-classified with role "sample_website"
#$role = 'sample_website'
#$master_ip = '220.158.142.58'
$master_url = "https://puppetmaster.zippyops.com:8140/packages/current/install.bash"
$instance_name = 'azure-sample-host'
$user_data = @("USERDATA"/L)

#! /bin/bash
hostnamectl set-hostname puppetazure.zippyops.com
echo "220.158.142.58 puppetmaster.zippyops.com" >> /etc/hosts
yum install curl -y
curl -k $master_url | sudo base
| USERDATA

$base_name = 'zippyops'
$rg = "${base_name}-rg"
$storage_account = "${base_name}saccount"
$nsg = "${base_name}-nsg"
$vnet = "${base_name}-vnet"
$subnet = "${base_name}-subnet"
$publicip = "${base_name}-publicip"
$location = 'uksouth'
$subscription_id = '589b47bf-88b9-48aa-9fb9-86fd723b8472'


# Base names for the vm's

$nic_base_name = "${base_name}-nic"
$vm_base_name = "${base_name}-vm"
azure_resource_group { $rg:
ensure => present,
parameters => {},
location => $location
}

azure_storage_account { $storage_account:
ensure => present,
parameters => {},
resource_group_name => $rg,
account_name => $storage_account,
location => $location,
sku => {
name => 'Standard_LRS',
tier => 'Standard',
}
}


azure_network_security_group { $nsg :
ensure => present,
parameters => {},
resource_group_name => $rg,
location => $location,
properties => {
securityRules => [
{ name => 'http',
properties => {
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '8080',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 100,
direction => 'Inbound',
},
},
{ name => 'ssh',
properties => {
description => 'Allow SSH',
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '22',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 201,
direction => 'Inbound',
},
},
{ name => 'https',
properties => {
description => 'MCollective',
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '443',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 103,
direction => 'Inbound',
},
},
]
}
}



azure_virtual_network { $vnet:
ensure => present,
parameters => {},
location => $location,
resource_group_name => $rg,
properties => {
addressSpace => {
addressPrefixes => ['10.0.0.0/24', '10.0.2.0/24']
},
dhcpOptions => {
dnsServers => ['8.8.8.8', '8.8.4.4']
},
subnets => [
{
name => $subnet,
properties => {
addressPrefix => '10.0.0.0/24'
}
}]
}
}


azure_subnet { $subnet:
ensure => present,
subnet_parameters => {},
virtual_network_name => $vnet,
resource_group_name => $rg,
properties => {
addressPrefix => '10.0.0.0/24',
networkSecurityGroup => {
properties => {

},

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/networkSecurityGroups/${nsg}"
}
}
}



# Public IP Address
azure_public_ip_address { $publicip:
ensure => present,
location => $location,
resource_group_name => $rg,
subscription_id => $subscription_id,
id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/publicIPAddresses/${publicip}",
parameters => {
idleTimeoutInMinutes => '10',
},
}


# Create multiple NIC's and VM's
azure_network_interface { $nic_base_name:
ensure => present,
parameters => {},
resource_group_name => $rg,
location => $location,
properties => {
ipConfigurations => [{
properties => {
privateIPAllocationMethod => 'Dynamic',
publicIPAddress => {

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/publicIPAddresses/${publicip}",

},
subnet => {

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/virtualNetworks/${vnet}/subnets/${subnet}", #lint:ignore:140chars

properties => {
addressPrefix => '10.0.0.0/24',
provisioningState => 'Succeeded'
},
name => $subnet
},
},
name => "${base_name}-nic-ipconfig"
}]
}
}


azure_virtual_machine { $vm_base_name:
ensure => present,
parameters => {},
location => $location,
resource_group_name => $rg,
properties => {
hardwareProfile => {
vmSize => 'Standard_D4s_v3'
},
storageProfile => {
imageReference => {
offer => 'UbuntuServer',
publisher => 'canonical',
sku => '16.04.0-LTS',
version => 'latest'
},
osDisk => {
name => $vm_base_name,
createOption => 'FromImage',
caching => 'None',
vhd => {

uri => "https://${$storage_account}.blob.core.windows.net/${vm_base_name}-container/${vm_base_name}.vhd"

}
},
dataDisks => []
},
osProfile => {
computerName => $vm_base_name,
adminUsername => 'zippyops',
adminPassword => 'Devops!',
linuxConfiguration => {
disablePasswordAuthentication => false
}
},
networkProfile => {
networkInterfaces => [
{

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/networkInterfaces/${nic_base_name}", #lint:ignore:140chars

primary => true
}]
},
},
type => 'Microsoft.Compute/virtualMachines',
}


azure_virtual_machine_extension { 'script' :
type => 'Microsoft.Compute/virtualMachines/extensions',
extension_parameters => '',
location => $location,
tags => {
displayName => "${vm_base_name}/script",
},
properties => {
protectedSettings => {
commandToExecute => $user_data,
},
publisher => 'Microsoft.Azure.Extensions',
type => 'CustomScript',
typeHandlerVersion => '2.0',

},
resource_group_name => $rg,
subscription_id => $subscription_id,
vm_extension_name => 'script',
vm_name => $vm_base_name,
}


For windows instance,

# Demo script for quickly spinning up a Linux node
# which is auto-classified with role "sample_website"
#$role = 'sample_website'
#$master_ip = '220.158.142.58'
#$master_url = "https://puppetmaster.zippyops.com:8140/packages/current/install.bash"
#$instance_name = 'azure-sample-host'
#$user_data = @("USERDATA"/L)
# $hostfile = "C:\Windows\System32\drivers\etc\hosts"
# Write-Host –NoNewLine "Updating server"
# "220.158.142.58 puppetmaster.zippyops.com" | Out-File $hostfile -encoding ASCII -append
# Write-Host "Done!"
# | USERDATA

$base_name = 'zippyops'
$rg = "${base_name}-rg"
$storage_account = "${base_name}saccount"
$nsg = "${base_name}-nsg"
$vnet = "${base_name}-vnet"
$subnet = "${base_name}-subnet"
$publicip = "${base_name}-publicip"
$location = 'uksouth'
$subscription_id = '589b47bf-88b9-48aa-9fb9-86fd723b8472'


# Base names for the vm's
$nic_base_name = "${base_name}-ni"
$vm_base_name = "${base_name}-vm"
azure_resource_group { $rg:
ensure => present,
parameters => {},
location => $location
}

azure_storage_account { $storage_account:
ensure => present,
parameters => {},
resource_group_name => $rg,
account_name => $storage_account,
location => $location,
sku => {
name => 'Standard_LRS',
tier => 'Standard',
},
}

exec {"upload exec":
command => 'az storage blob upload --container-name zippyops-vm-container --name puppet --file azure.ps1 --account-name zippyopssaccount',

cwd => '/etc/puppetlabs/code/environments/production/modules/azure_arm/examples',

path => ['/usr/bin', '/usr/sbin',],
}

azure_network_security_group { $nsg :
ensure => present,
parameters => {},
resource_group_name => $rg,
location => $location,
properties => {
securityRules => [
{ name => 'http',
properties => {
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '8080',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 100,
direction => 'Inbound',
},


},
{ name => 'WinRm-HTTP',
properties => {
description => 'ALLOW WINRM',
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '5985',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 201,
direction => 'Inbound',
},
},
{ name => 'powershell',
properties => {
description => 'ALLOW WINRM',
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '5986',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 203,
direction => 'Inbound',
},
},
{ name => 'RDP',
properties => {
description => 'ALLOW WINRM',
protocol => 'Tcp',
sourcePortRange => '*',

destinationPortRange => '3389',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 205,
direction => 'Inbound',
},
},

{ name => 'https',
properties => {
description => 'MCollective',
protocol => 'Tcp',
sourcePortRange => '*',
destinationPortRange => '443',
sourceAddressPrefix => '*',
destinationAddressPrefix => '*',
access => 'Allow',
priority => 103,
direction => 'Inbound',
},
},
]
}
}

azure_virtual_network { $vnet:
ensure => present,
parameters => {},
location => $location,
resource_group_name => $rg,
properties => {
addressSpace => {
addressPrefixes => ['10.0.0.0/24', '10.0.2.0/24']
},
dhcpOptions => {
dnsServers => ['8.8.8.8', '8.8.4.4']
},
subnets => [
{
name => $subnet,
properties => {
addressPrefix => '10.0.0.0/24'

}
}]
}
}


azure_subnet { $subnet:
ensure => present,
subnet_parameters => {},
virtual_network_name => $vnet,
resource_group_name => $rg,
properties => {
addressPrefix => '10.0.0.0/24',
networkSecurityGroup => {
properties => {

},

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/networkSecurityGroups/${nsg}"

}
}
}



# Public IP Address
azure_public_ip_address { $publicip:
ensure => present,
location => $location,
resource_group_name => $rg,
subscription_id => $subscription_id,

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/publicIPAddresses/${publicip}",

parameters => {
idleTimeoutInMinutes => '10',
},
}

# Create multiple NIC's and VM's
azure_network_interface { $nic_base_name:
ensure => present,
parameters => {},
resource_group_name => $rg,
location => $location,
properties => {
ipConfigurations => [{
properties => {
privateIPAllocationMethod => 'Dynamic',
publicIPAddress => {

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/publicIPAddresses/${publicip}",

},
subnet => {

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/virtualNetworks/${vnet}/subnets/${subnet}", #lint:ignore:140chars

properties => {
addressPrefix => '10.0.0.0/24',
provisioningState => 'Succeeded'
},
name => $subnet
},
},
name => "${base_name}-nic-ipconfig"
}]
}
}

azure_virtual_machine { $vm_base_name:
ensure => present,
parameters => {},
location => $location,
resource_group_name => $rg,
properties => {
hardwareProfile => {
vmSize => 'Standard_D1_v2'
},
storageProfile => {
imageReference => {
offer => 'WindowsServer',
publisher => 'MicrosoftWindowsServer',
sku => '2012-R2-Datacenter',
version => 'latest'
},
osDisk => {
name => $vm_base_name,
createOption => 'FromImage',
caching => 'None',
vhd => {
uri => "https://${$storage_account}.blob.core.windows.net/${vm_base_name}-container/${vm_base_name}.vhd"
}
},
dataDisks => []
},
osProfile => {
computerName => $vm_base_name,
adminUsername => 'zippyops',
adminPassword => 'Admin123',
#linuxConfiguration => {
# disablePasswordAuthentication => false
#}
},

networkProfile => {
networkInterfaces => [
{

id => "/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/networkInterfaces/${nic_base_name}", #lint:ignore:140chars

primary => true
}]
},
},
type => 'Microsoft.Compute/virtualMachines',
}

azure_virtual_machine_extension { 'script'
type => 'Microsoft.Compute/virtualMachines/extensions',
extension_parameters => '',
location => $location,
tags => {
displayName => "${vm_base_name}/script",
},
properties => {
protectedSettings => {
fileUris => [ 'https://${$storage_account}.blob.core.windows.net/${vm_base_name}-container/gcp.ps1' ],
commandToExecute => 'powershell -ExecutionPolicy Unrestricted -File gcp.ps1',
},
publisher => 'Microsoft.Compute',
type => 'CustomScriptExtension',
typeHandlerVersion => '1.9',
},
resource_group_name => $rg,
subscription_id => $subscription_id,
vm_extension_name => 'script',
vm_name => $vm_base_name,
}

Run puppet apply,

You can  view the instance  provisioned in the Azure portal,


Install the packages to connect the GCP from the local machine.

C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\zippyops\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Pictures\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\a25.PNG

[root@ansiblemaster ansible]# sudo yum check-update; sudo yum install -y gcc libffi-devel python-devel openssl-devel epel-release

C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\zippyops\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Pictures\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\a5.PNG

The Google Cloud Platform (GCP) modules require both the requests and the google-auth libraries to be installed.

C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\zippyops\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Pictures\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\g16.PNG

Playbook

This file is used to create the instance in GCP,

C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\zippyops\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Pictures\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\g17.PNG

- name: Create instance(s)
  hosts: localhost
  gather_facts: no
  connection: local

  vars:
      machine_type: n1-standard-1 # default
      image: projects/centos-cloud/global/images/centos-7-v20190515
      service_account_email: < service_account_email >
      credentials_file: < json file path in local machine >
      project_id: < project_id >
      region: us-central1
      zone: us-central1-a

  tasks:
      - name: create a disk
        gcp_compute_disk:
            name: disk-instance
            size_gb: 50
            source_image: "{{ image }}"
            zone: us-central1-a
            project: "{{ project_id }}"
            auth_kind: serviceaccount
            scopes:
                - https://www.googleapis.com/auth/compute
            service_account_file: "{{ credentials_file }}"
            state: present
  register: disk


- name: create a network
   gcp_compute_network:
       name: network
       auto_create_subnetworks: 'true'
       project: "{{ project_id }}"
       auth_kind: serviceaccount
       scopes:
           - https://www.googleapis.com/auth/compute
       service_account_file: "{{ credentials_file }}"
       state: present
       register: networkid

- name: create a subnetwork
  gcp_compute_subnetwork:
      name: ansiblenet
      region: "{{ region }}"
      network: "{{ networkid }}"
      ip_cidr_range: 172.16.0.0/16
      project: "{{ project_id }}"
      auth_kind: serviceaccount
      service_account_file: "{{ credentials_file }}"
      scopes:
           - https://www.googleapis.com/auth/compute
       state: present
       register: subnetid


- name: create a address

      gcp_compute_address:

        name: address-instance

        region: "{{ region }}"

        project: "{{ project_id }}"

        auth_kind: serviceaccount

        scopes:

          - https://www.googleapis.com/auth/compute

        service_account_file: "{{ credentials_file }}"

        state: present

      register: address

  - name: create a firewall

      gcp_compute_firewall:

        name: firewall 

        network: "projects/{{ project_id }}/global/networks/{{ networkid.name }}"

        allowed:

        - ip_protocol: tcp

          ports: ['80','22'] 

        target_tags:

        - test-ssh-server

        source_ranges: ['0.0.0.0/0']

        project: "{{ project_id }}"

        auth_kind: serviceaccount

        scopes:

          - https://www.googleapis.com/auth/compute

        service_account_file: "{{ credentials_file }}"

        state: present

 

    - name: Launch instances

      gcp_compute_instance:

        name: test

        machine_type: "{{ machine_type }}"

        zone: "{{ zone }}"

        network_interfaces:

        - network: "{{ networkid }}"

          subnetwork: "{{ subnetid }}"

          access_configs:

          - name: External NAT

            nat_ip: "{{ address }}"

            type: ONE_TO_ONE_NAT

        disks:

        - auto_delete: 'true'

          boot: 'true'

          source: "{{ disk }}" 

        tags: 

          items:

          - test-ssh-server

        auth_kind: serviceaccount

        service_account_file: "{{ credentials_file }}"

        project: "{{ project_id }}"

        metadata: '{ "startup-script" : "yum install -y httpd && systemctl start httpd" }'

        scopes:

          - https://www.googleapis.com/auth/compute

        state: present

      register: gce

This file used to delete what you have created,

C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\zippyops\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Pictures\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\g18.PNG

- name: delete instance(s)

  hosts: localhost

  gather_facts: no
  connection: local

  vars:
       machine_type: n1-standard-1 # default
       image: projects/centos-cloud/global/images/centos-7-v20190515
       service_account_email: < service_account_email >
       credentials_file: < json file path in local machine >
       project_id: < project_id >
       region: us-central1
       zone: us-central1-a


  tasks:
- name: delete instances
   gcp_compute_instance:
       name: test
       machine_type: "{{ machine_type }}"
       zone: "{{ zone }}"
       auth_kind: serviceaccount
       service_account_file: "{{ credentials_file }}"
       project: "{{ project_id }}"
       scopes:
            - https://www.googleapis.com/auth/compute
        state: absent
   register: gce

- name: delete a address
  gcp_compute_address:
      name: address-instance
      region: "{{ region }}"
      project: "{{ project_id }}"
      auth_kind: serviceaccount
      scopes:
           - https://www.googleapis.com/auth/compute
       service_account_file: "{{ credentials_file }}"
       state: absent
   register: address

- name: delete a firewall
   gcp_compute_firewall:
       name: firewall
        project: "{{ project_id }}"
        auth_kind: serviceaccount
        scopes:
             - https://www.googleapis.com/auth/compute
        service_account_file: "{{ credentials_file }}"
        state: absent

- name: existing network
gcp_compute_network:
name: network
auto_create_subnetworks: 'true'
project: "{{ project_id }}"
auth_kind: serviceaccount
scopes:
- https://www.googleapis.com/auth/compute
service_account_file: "{{ credentials_file }}"
register: networkid

- name: delete a subnetwork
   gcp_compute_subnetwork:
       name: ansiblenet
       region: "{{ region }}"
       network: "{{ networkid }}"
       ip_cidr_range: 172.16.0.0/16
       project: "{{ project_id }}"
       auth_kind: serviceaccount
       service_account_file: "{{ credentials_file }}
       scopes:
            - https://www.googleapis.com/auth/compute
       state: absent
  register: subnetid

- name: delete a network
  gcp_compute_network:
      name: network
      auto_create_subnetworks: 'true'
      project: "{{ project_id }}"
      auth_kind: serviceaccount
      scopes:
          - https://www.googleapis.com/auth/compute
       service_account_file: "{{ credentials_file }}"
       state: absent
  register: networkid

output


C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\zippyops\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Pictures\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\g1.PNG

After it the successfully finished, it will create the instance in the GCP with the web application and wait for 10 to 15 mins and you can check in the browser.

C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\zippyops\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Pictures\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\g2.PNG 

If you want to delete what you have created using this command,

C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\zippyops\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\Pictures\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\g3.PNG



Recent Comments

No comments

Leave a Comment