使用Ansible创建GCP存储桶

时间:2020-09-27 16:41:50

标签: google-cloud-platform ansible google-cloud-storage

我正在尝试ansible来处理GCP resources。最初,我尝试使用GCP storage bucket创建一个ansible。为此,我使用以下命令在ansible中安装了GCP CLoud Shell。还创建了一个service account并生成了基于JSON的密钥文件。

   sudo apt-get install ansible

还使用以下命令安装了gcloud模块

   sudo pip3 install google.cloud

现在我正尝试使用下面的Storage bucket文件创建一个YAML

- name: create a bucket
  tasks:
  - name: Storage bucket
    google.cloud.gcp_storage_bucket:
      name: ansible-storage-module
      project: test_project
      auth_kind: serviceaccount
      service_account_file: "/tmp/auth.json"
      state: present

但是当我运行ansible-playbook

时,它失败并出现以下错误
  ansible-playbook ansible.yaml

 ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

 The error appears to have been in '/home/project/ansible/ansible.yaml': line 4, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

违规行似乎是:

- name: Storage bucket
  ^ here

1 个答案:

答案 0 :(得分:2)

我通过使用Ansible安装google-authpip来解决此问题。

 pip install ansible
 pip install requests google-auth

playbook进行了一些修改以使其正常工作

 - name: create a bucket
   hosts: localhost
   tasks:
   - name: Storage bucket
     google.cloud.gcp_storage_bucket:
       name: ansible-storage-module
       project: adminproject-272208
       auth_kind: serviceaccount
       service_account_file: "/home/project/ansible/project.json"
       state: present

gsutil显示存储桶已创建

     $ gsutil ls
     gs://ansible-storage-module/