I am trying my hands with ansible
to handle GCP resources
. Initially I am trying to create a GCP storage bucket
using ansible
. For this I installed ansible
in GCP CLoud Shell
using below command. Also created a service account
and generated JSON based key file.
sudo apt-get install ansible
Also installed gcloud module using below command
sudo pip3 install google.cloud
Now I am trying to create a Storage bucket
using below YAML
file
- 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
But it is failing with below error when I ran with 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.
The offending line appears to be:
- name: Storage bucket
^ here
I resolved this by installing both Ansible
and google-auth
using pip
.
pip install ansible
pip install requests google-auth
and little modification on playbook
to make it work
- 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
shows bucket was created
$ gsutil ls
gs://ansible-storage-module/
In case you are using a python virtual env make sure the ansible_python_interpreter
is been declared properly.
Here is a full example with the entire setup with a Python venv:
python3.9 -m venv ~/py39
source ~/py39/bin/activate
pip install ansible
pip install google-auth
Then create a proper ansible directory tree like:
plays/ansible.cfg
plays/inventory
plays/main.yml
Where plays/ansible.cfg
have:
[defaults]
inventory = inventory
interpreter_python = ~/py39/bin/python
The plays/inventory
have:
localhost ansible_connection=local
The plays/main.yml
:
---
- name: "Creating a Bucket on GCP"
hosts: localhost
tasks:
- name: "Creating the Bucket"
google.cloud.gcp_storage_bucket:
name: "The_bucket_name"
project: "The_project_name"
auth_kind: "serviceaccount"
service_account_file: "/your/service/account/file.json"
state: present
register: bucket
Finally run in:
cd plays
ansible-playbook main.yml
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.