Example Ansible Playbooks

This topic provides a catalog of sample Ansible playbooks for Oracle Cloud Infrastructure (OCI) that illustrate how to carry out common infrastructure provisioning and configuration tasks using our Ansible collection.

These samples and solutions are organized in sections associated with OCI services. You can find a brief description of each playbook along with links to each sample on the Oracle GitHub repository. Be sure to review the Readme.md file that is included in each playbook's root directory for additional instructions.

See Getting Started to begin using our Ansible collection.

Samples

Block Volume

Attaching a block volume to a Compute instance

This sample playbook shows how to attach a block volume to a Compute instance using the iSCSI volume attachment type, and then connect it to the Compute instance using iscsiadm. The sample shows how to do the following:

  • Generate a temporary, host-specific SSH key pair.
  • Specify the public key from the key pair for connecting to the instance, and then launch the instance.
  • Create a new Block Volume for the instance, attach the volume to the instance, and specify iSCSI as the volume attachment type.
  • Connect to and then mount the volume from the Compute instance by executing iscsiadm commands over SSH using an Ansible module.

Go to the sample on Oracle GitHub.

Compute

Launching an Always Free Compute instance

This sample shows how to launch and access an Always Free Compute instance from the internet using SSH using OCI Ansible collections.

This sample shows how to do the following:

  • Generate a temporary, host-specific SSH key pair.
  • Specify the public key from that key-pair to connect to the instance during instance launch.
  • Connect to the newly launched instance using SSH.

Go to the sample for Always Free on Oracle GitHub.

Launching a Compute instance using App Catalog

This sample shows how a public Compute instance can be launched using app_catalog and accessed from the internet using SSH with OCI Ansible collections.

This sample shows how to do the following:

  • Generate a temporary, host-specific SSH key pair.
  • Specify the public key from that key-pair to connect to the instance during instance launch.
  • Fetch app_catalog and its versions and create a subscription for it.
  • Launch the instance using app_catalog and connect to it using SSH.

Go to the sample on Oracle GitHub.

Creating an instance pool

This sample shows how to manage your Compute instances using resources such as instance configurations and instance pools that are provided using OCI Ansible collections. Instance pools help you create and provision multiple Compute instances within the same region based on a single instance configuration.

This sample shows how to do the following:

  • Generate a temporary, host-specific SSH key pair.
  • Specify the public key from that key-pair to connect to the instance during instance launch.
  • Create an instance configuration that defines settings for creating a Compute instance as part of the instance pool. The configuration provides details such as base image, shape, and metadata.
  • Use the instance pool to launch Compute instances based on the instance configuration.
  • Connect to one of the Compute instances using SSH.

Go to the sample on Oracle GitHub.

Creating instance Console connections and capturing Console history

This sample shows how a serial and VNC console connection can be created for a Compute instance, and how the serial console data can be captured and fetched from a Compute instance using OCI Ansible collections. For more information about Console connections, see Troubleshooting Instances Using Instance Console Connections.

This sample shows how to do the following:

  • Generate a temporary SSH key pair for the serial Console connection.
  • Create an instance Console connection for a Compute instance.
  • Capture serial Console data for a Compute instance, and then save the data to a local machine so you can troubleshoot and debug issues.

Go to the sample on Oracle GitHub.

Launching a Compute instance

This sample shows how to launch and access a public Compute instance from the internet using SSH with OCI Ansible collections.

This sample shows how to do the following:

  • Generate a temporary, host-specific SSH key pair.
  • Specify the public key from the key pair for connecting to the instance, and then launch the instance.
  • Connect to the newly launched instance using SSH.

Go to the sample on Oracle GitHub.

Enabling internet access from a Compute instance using the OCI NAT Gateway

This sample shows how you can use OCI Ansible collections to enable internet access from Compute instances in a private subnet using a NAT Gateway in a public subnet. For more information about NAT gateways, see NAT Gateway and Access Resources on the Public Internet Through an Oracle Cloud Infrastructure NAT Gateway.

This sample shows how to do the following:

  • Set up the VCN, the NAT gateway, the internet gateway, the public and private subnets, and the necessary security lists and route rules.
  • Provision a bastion instance in the public subnet and a private instance in the private subnet.

Once set up, the private instance will have outbound Internet access through the NAT gateway, and will be accessible using SSH from the bastion instance.

Go to the sample on Oracle GitHub.

Enabling internet access from a Compute instance using an OCI NAT Instance

This sample shows how you can use OCI Ansible collections to enable internet access from Compute instances in a private subnet using a NAT instance in a public subnet as discussed here and in the whitepaper here.

Note

An NAT gateway is available as a reliable and highly available solution in the OCI Networking service. Please refer to the sample for more details.

This sample shows how to do the following:

  • Set up the topology described in the whitepaper by creating the VCN, the internet gateway, the public and private subnets, and the necessary security lists and route rules. A NAT instance is provisioned in the public subnet and a private instance is provisioned in the private subnet.
  • After the setup, the private instance has outbound internet access through the NAT instance in the public subnet.

Go to the sample on Oracle GitHub.

Accessing Object Storage from a private instance using a service gateway

This sample playbook shows how you can use OCI Ansible collections to enable private access to an Object Storage from a Compute instance using a service gateway. For more information about service gateways, see Access to Oracle Services: Service Gateway. To read a blog post discussing how to connect Compute instances using the service gateway, see Connect Private Instances with Oracle Services Through an Oracle Cloud Infrastructure Service Gateway.

This sample shows how to do the following:

  • Set up a user, group, and the policies required for managing buckets.
  • Create and upload the required API keys to the user.
  • Set up the VCN, the NAT gateway, the internet gateway, the public and private subnets, as well as the required security lists and route tables. A bastion instance is provisioned in the public subnet, and a private instance is provisioned in the private subnet.
  • Provision a Compute instance in the private subnet,
  • Install the OCI command line interface (CLI) and configure the CLI using the cloud init script.
  • Disable the NAT gateway to restrict public access to the private instance.
  • Create a bucket from the private instance using the OCI CLI, then verify that the bucket is created.

Following this setup, the private instance has private access to Object Storage.

Go to the sample on Oracle GitHub.

Container Engine for Kubernetes

Creating a cluster using Container Engine for Kubernetes

This sample creates a cluster with Oracle Cloud Infrastructure Container Engine for Kubernetes (OKE) using OCI Ansible collections.

This sample shows how to do the following:

  • Create and configure a VCN and related resources required for setting up an OKE cluster.
  • Create a cluster.
  • Create a node pool.
  • Download the kubeconfig file for the cluster.

Go to the sample on Oracle GitHub.

Database

Creating an Always Free Autonomous Database

This sample shows how you can use the OCI Ansible collection to create an Always Free Autonomous Database with Autonomous Transaction Processing and manage its lifecycle. See Overview of Autonomous Databases for more information.

This sample shows how to do the following:

  • Set up an Autonomous Database with Autonomous Transaction Processing.
  • List all of the Autonomous Transaction Processing instances available in a compartment, filtered by display name.
  • Get the "facts" for a specified database.
  • Stop and start an Autonomous Database.
  • Delete an Autonomous Database.

Go to the sample on Oracle GitHub.

Setting up an Autonomous Database

This sample shows how you can use the OCI Ansible collection to create an Autonomous Database with Autonomous Transaction Processing and manage its lifecycle. Refer to the Overview of Autonomous Databases for more information.

This sample shows how to do the following:

  • Set up an Autonomous Database with Autonomous Transaction Processing.
  • List all of the Autonomous Transaction Processing instances available in a compartment, filtered by display name.
  • Get the "facts" for a specified database.
  • Stop and start an Autonomous Database.
  • Delete an Autonomous Database.

Go to the sample on Oracle GitHub.

Creating a Bare Metal and Virtual Machine DB system

This sample shows how you can use the OCI Ansible collection to create Bare Metal and Virtual DB systems. For more information about OCI co-managed DB Systems, see Bare Metal and Virtual Machine DB Systems.

This sample shows how to do the following:

  • Set up a Virtual Machine DB System.
  • Get facts of a specific DB System and list available DB Homes.
  • List all the databases available in specified DB Home and get facts of specific database.
  • Collect DB Node's VNIC information of a specified DB system.
  • Extract Public and Private IPs of the DB Node from VNIC.
  • Create a backup from initial database.
  • Restore a database from latest backup.
  • Create a new database from backup.
  • Update database fields.

Go to the sample on Oracle GitHub.

File Storage

Creating and mounting a file system

This sample shows how you can use the OCI Ansible collection to create and access a File Storage file system through compute instances.

This sample shows how to do the following:

  • Generate all network related dependencies (e.g. VCN, subnets) and security lists with the configuration required by File Storage.
  • Generate the certificates required by instances.
  • Create File Storage components such as mount target, file system, export, and snapshot.
  • Mount the file system through a Compute instance and access the contents through another Compute instance.

Go to the sample on Oracle GitHub.

Exporting multiple file systems and mount targets

This sample shows how you can use the OCI Ansible collection to export one file system using two different export paths on two different mount targets. It also demonstrates how a single mount target can export paths from two different file systems.

This sample shows how to do the following:

  • Generate all network related dependencies (e.g. VCN, subnets) and security lists with the configuration required by File Storage.
  • Generate the certificates required by instances.
  • Create File Storage components such as mount target, file system, export, and snapshot.
  • Export one file system to two different mount targets.
  • Export paths from a single mount target to two different file systems.
  • Mount the file system through a compute instance.

Go to the sample on Oracle GitHub.

Identity

Adding a user and group

This sample shows how you can use the OCI Ansible collection to perform basic Oracle Cloud Infrastructure Identity and Access Management (IAM) tasks. The sample assumes the default user configured in the OCI configuration is in the Administrator group or has the required access for managing users, groups, policies.

This sample shows how to do the following:

  • Create a new group.
  • Create a policy.
  • Create a user then add it to the group and policy.
  • Create user password.
  • Generate SSH keys and assign them to the user.

Go to the sample on Oracle GitHub.

Load Balancing

Creating a load balancer

This sample shows how you can use the OCI Ansible collection to create a public load balancer.

This sample shows how to do the following:

  • Generate all network-related dependencies, like a VCN and subnets.
  • Generate the certificates required by the load balancer.
  • Create a public load balancer.

Go to the sample on Oracle GitHub.

Networking

Provisioning a VCN with private subnets

This sample shows how you can use the OCI Ansible collection to provision a virtual cloud network (VCN) with two private subnets in different availability domains and an IPSec VPN. The IPSec VPN uses a dynamic routing gateway  (DRG), customer-premises equipment  (CPE), and an IPSec connection. The provisioned resources are illustrated in this networking scenario.

This sample shows how to provision the following resources:

  • A VCN
  • Two private subnets
  • A dynamic routing gateway
  • Customer-premises equipment
  • An IPSec connection between DRG & CPE

Finally, it retrieves IPSec connection configuration information and status.

Go to the sample on Oracle GitHub.

Object Storage

Getting a namespace

This sample playbook shows how to use the OCI Ansible collection to get the tenancy namespace in Object Storage.

Go to the sample on Oracle GitHub.

Listing objects and buckets

This sample playbook shows how to use the OCI Ansible collection to list all Object Storage objects from all buckets in a namespace.

Go to the sample on Oracle GitHub.

Deleting objects

This sample playbook shows how to use the OCI Ansible collection to delete objects created within a specified range of days from all buckets in a namespace. You can modify the sample so that it deletes objects older than a specified number of days, which helps you prune old or unwanted objects stored in the Object Storage service.

Go to the sample on Oracle GitHub.

Solutions

MuShop

MuShop is a showcase of several Oracle Cloud Infrastructure (OCI) services in a unified reference application. The sample application implements an e-commerce platform built as a set of microservices. The accompanying content can be used to get started with cloud native application development on OCI.

MuShop can be deployed in different ways to explore OCI based on your subscription. OCI offers Always Free tier with resources that can be used indefinitely.

This project is an example of how you can build OCI infrastructure using the OCI Ansible collection.

Go to the solution on Oracle GitHub.