使用Ansible Playbook在Google Cloud(gcp)中创建实例

时间:2020-04-28 21:34:50

标签: google-cloud-platform ansible

我正在使用以下代码

- name: create a instance
  gcp_compute_instance:
    name: test_object
    machine_type: n1-standard-1
    disks:
    - auto_delete: 'false'
      boot: 'true'
      source: "{{ disk }}"
    metadata:
      startup-script-url:
      cost-center:
    labels:
      environment: production                                                               
    network_interfaces:                                                                     
    - network: "{{ network }}"                                                              
      access_configs:                                                                       
      - name: External NAT                                                                  
        nat_ip: "{{ address }}"                                                             
        type: ONE_TO_ONE_NAT                                                                
    zone: us-central1-a                                                                     
    project: test-12y38912634812648                                                         
    auth_kind: serviceaccount                                                               
    service_account_file: "~/programming/gcloud/test-1283891264812-8h3981f3.json"      
    state: present                                                                          

然后我将文件另存为create2.yml

然后我运行Ansible-playbook create2.yml并收到以下错误消息

ERROR! 'gcp_compute_instance' is not a valid attribute for a Play

The error appears to be in '/Users/xxx/programming/gcloud-test/create2.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: create a instance
  ^ here

我遵循了文档。我在做什么错以及如何解决?

1 个答案:

答案 0 :(得分:1)

您还没有创建剧本,只是创建了一个文件,该文件的任务无法像您所发现的那样自行运行。

剧本是任务的集合。您应该从剧本文档开始:

Playbook Documentation

对于GCP,这是一个创建网络,外部IP,磁盘和VM的有效示例。

- name: 'Deploy gcp vm'
  hosts: localhost
  connection: local
  become: false
  gather_facts: no

  vars: 
    gcp_project: "671245944514"
    gcp_cred_kind: "serviceaccount"
    gcp_cred_file: "/tmp/test-project.json"
    gcp_region: "us-central1"
    gcp_zone: "us-central1-a"


  # Roles & Tasks
  tasks:

  - name: create a disk
    gcp_compute_disk:
      name: disk-instance
      size_gb: 50
      source_image: projects/ubuntu-os-cloud/global/images/family/ubuntu-2004-lts
      zone: "{{ gcp_zone }}"
      project: "{{ gcp_project }}"
      auth_kind: "{{ gcp_cred_kind }}"
      service_account_file: "{{ gcp_cred_file }}"
      state: present
    register: disk

  - name: create a network
    gcp_compute_network:
      name: network-instance
      project: "{{ gcp_project }}"
      auth_kind: "{{ gcp_cred_kind }}"
      service_account_file: "{{ gcp_cred_file }}"
      state: present
    register: network

  - name: create a address
    gcp_compute_address:
      name: address-instance
      region: "{{ gcp_region }}"
      project: "{{ gcp_project }}"
      auth_kind: "{{ gcp_cred_kind }}"
      service_account_file: "{{ gcp_cred_file }}"
      state: present
    register: address

  - name: create a instance
    gcp_compute_instance:
      name: vm-instance
      project: "{{ gcp_project }}"
      zone: "{{ gcp_zone }}"
      machine_type: n1-standard-1
      disks:
      - auto_delete: 'true'
        boot: 'true'
        source: "{{ disk }}"
      labels:
        environment: testing
      network_interfaces:
      - network: "{{ network }}"
        access_configs:
        - name: External NAT
          nat_ip: "{{ address }}"
          type: ONE_TO_ONE_NAT
      auth_kind: serviceaccount
      service_account_file: "{{ gcp_cred_file }}"
      state: present