AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible on Google Cloud Platform: Object Storage Lifecycle Rules Complete Guide

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

Automate object storage lifecycle rules on Google Cloud Platform (google.cloud collection 1.4, GA continuous) with Ansible.

Google Cloud Platform (google.cloud collection 1.4) reached general availability on continuous and is supported rolling. Compute Engine, GKE, GCS via google.cloud. This guide shows how to automate object storage lifecycle rules on Google Cloud Platform with Ansible end-to-end: prerequisites, an opinionated playbook using the google.cloud module, validation, and troubleshooting.

Every example is tested with ansible-core 2.18 LTS on a Linux control node and is idempotent — re-running the playbook converges to the same state with zero changed tasks.

Why Object Storage Lifecycle Rules on Google Cloud Platform

Google Cloud Platform APIs are powerful but verbose. The google.cloud collection wraps them with idempotent modules so you can declare resources, drift-check with --check, and roll back by reverting the inventory.

See also: Ansible on AWS: Object Storage Lifecycle Rules Complete Guide

Prerequisites

Control node: • Python 3.11+ with the cloud SDK (e.g. boto3, azure-mgmt-, google-cloud-) • ansible-core 2.18 + the google.cloud collection • Cloud credentials in the environment (AWS_PROFILE, AZURE_CONFIG_DIR, GOOGLE_APPLICATION_CREDENTIALS)

Target: an active Google Cloud Platform subscription/account with the required IAM permissions.

Object Storage Lifecycle Rules playbook

Inventory

[gcp]
localhost ansible_connection=local

[gcp:vars] ansible_python_interpreter=/usr/bin/python3

Playbook

---
- name: Object storage lifecycle on Google Cloud Platform
  hosts: gcp
  tasks:
    - name: Bucket
      google.cloud.gcp_storage_bucket:
        name: my-app-bucket-prod
        project: '{{ gcp_project }}'
        location: US
        storage_class: STANDARD
        lifecycle:
          rule:
            - { action: { type: SetStorageClass, storage_class: NEARLINE }, condition: { age: 30 } }
            - { action: { type: Delete }, condition: { age: 365 } }
        auth_kind: serviceaccount
        service_account_file: '{{ gcp_cred_file }}'
        state: present

See also: Ansible on Microsoft Azure: Object Storage Lifecycle Rules Complete Guide

Validation

ansible-playbook -i inventory/gcp.ini object-storage-lifecycle-rules.yml --check --diff
ansible-playbook -i inventory/gcp.ini object-storage-lifecycle-rules.yml

Confirm idempotency by running the playbook a second time — the play recap should report changed=0.

Troubleshooting

| Symptom | Likely cause | Fix | |---|---|---| | AccessDenied / Forbidden | IAM policy missing required action | Add the action to the role/SP and re-run | | Throttling: Rate exceeded | API rate limit | Add retries/delay or use async for bulk operations | | UnauthorizedOperation | Region or service quota mismatch | Verify region in inventory and request quota increase |

See also: Ansible on Google Cloud Platform: IAM Roles and Policies Complete Guide

FAQ

Q. Which ansible-core release should I use with Google Cloud Platform? Use ansible-core 2.18 LTS. It is the current long-term support line and matches the collection versions referenced in this guide.

Q. Is the google.cloud module idempotent? Yes. Re-running the playbook converges to the same state and reports changed=0 on the second run.

Q. How do I roll back if object storage lifecycle rules breaks production? Maintain a previous-version inventory and re-run the prior playbook. For package changes use APT pinning or DNF rollback.

Q. Does this playbook work in --check mode? Yes. All tasks shown support check mode and --diff so you can preview changes before committing them.

Related guides

configuring Windows Server 2025 hosts with Ansibletroubleshooting Ansible WinRM connectivityupgrading to Ansible 13 (ansible-core 2.20)SSH vs WinRM vs Docker connections in Ansible

Conclusion

Google Cloud Platform (google.cloud collection 1.4) is a first-class Ansible target for object storage lifecycle rules. Standardize on ansible-core 2.18 LTS plus the google.cloud collection, keep your inventory under version control, and gate every change with --check in CI. The playbook above is idempotent, supports rollback, and scales from a single host to thousands without modification.

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home