OCI – Ansible Operation Example Background
In a cloud computing environment, automation is key to improving efficiency. Ansible is a widely used automation tool that helps us easily manage and operate resources on Oracle Cloud Infrastructure (OCI). This article will demonstrate how to use Ansible to operate OCI, including installing Ansible and the Ansible Collection, as well as performing shutdown operations on Oracle OCI virtual machines (VMs).
1. Install Ansible
First, we need to install Ansible on the Oracle Linux system. The installation command is as follows:
yum -y install ansible
2. Install Ansible Collection
2.1 Install via YUM
Depending on the version of Oracle Linux, use different commands to install oci-ansible-collection
.
Oracle Linux 7:
yum install -y oci-ansible-collection --enablerepo ol7_developer --enablerepo ol7_developer_EPEL
Oracle Linux 8:
yum install -y oci-ansible-collection --enablerepo ol8_developer --enablerepo ol8_developer_EPEL
2.2 Test if Installation was Successful
After installing the Ansible Collection, you can verify the installation with the following command.
Oracle Linux 7:
# Oracle Linux 7 verification command
ansible-3 localhost -m oracle.oci.oci_object_storage_namespace_facts
Oracle Linux 8:
# Oracle Linux 8 verification command
ansible localhost -m oracle.oci.oci_object_storage_namespace_facts
If the installation is successful, you will see fact data related to OCI object storage.
3. Oracle OCI VM Shutdown Operation Example
3.1 Example Explanation
This example will demonstrate how to filter OCI virtual machines (VMs) that are tagged with env=dev
and are currently running, extract their instance IDs, and perform shutdown operations.
In this example, we obtain the instance list based on the following conditions:
-
Only retrieve running instances.
-
The instance’s custom tag is
{"env": "dev"}
.
3.2 Example Demo
The following is the Ansible Playbook to achieve this operation:
- name: List instances
collections:
- oracle.oci
connection: local
hosts: localhost
vars:
compartment_id: ocid1.compartment.oc1..aaqaq
tasks:
- name: List instances
oci_compute_instance_facts:
compartment_id: "{{ compartment_id }}" ## Get the list of instance IDs that meet the conditions
lifecycle_state: "RUNNING" ## Only select running instances
register: result
- name: Append the result
set_fact:
str_list: "{{ str_list|default([]) + [item.id]}}" ## Add instance ID to the list of instances to be shut down
with_items: "{{ result.instances }}"
when:
- "'env' in item.freeform_tags" ## Check if the tag 'env' exists
- "item.freeform_tags.env == 'dev'" ## Only filter instances with env as dev
- name: Perform action stop instance
oci_compute_instance_actions:
compartment_id: "{{ compartment_id }}" ## Specify Compartment ID
instance_id: "{{ item }}" ## Instance ID
action: "stop" ## Perform shutdown operation
with_items: "{{ str_list }}"
register: stop_info
- name: Dump result
debug: var=stop_info ## Output the result of the shutdown operation
Configuration Explanation:
-
oci_compute_instance_facts: This task lists all running instances from the specified compartment.
-
set_fact: Used to generate a list of IDs of instances to be shut down, with the filtering condition being the tag
env=dev
. -
oci_compute_instance_actions: Performs shutdown operations on each qualifying instance.
-
debug: Outputs the task result, helping you confirm whether the shutdown operation was successful.
4. Reference Documentation
Here are some useful reference links where you can find more information about Ansible controlling Oracle OCI:
-
Install Ansible Collection via YUM
-
Details on Ansible controlling Oracle OCI instances
-
View details on Oracle OCI instances with Ansible