All Products
Search
Document Center

CloudOps Orchestration Service:Use Ansible to create and execute OOS templates

Last Updated:Sep 02, 2024

Ansible is an open source configuration and management tool that you can use to automate task execution and application deployment to implement the IT infrastructure.

What is OOS? allows you to use templates to manage Alibaba Cloud services to implement automated O&M. Before you proceed, make sure that you are familiar with the basic features and usage notes of OOS. This topic describes how to use Ansible to create OOS O&M templates, and how to use Ansible to execute the created templates to manage Alibaba Cloud services.

Overview

This topic separately describes Ansible playbooks for template creation and template execution. This helps you learn how to declare configurations in YAML format. You can refer to the provided example to run the Ansible playbook to create and execute an OOS template.

Ansible contains four modules: ali_oos_template, ali_oos_template_info, ali_oos_execution, and ali_oos_execution_info. You can use the four modules in the following scenarios.

Module

Scenario

ali_oos_template

Create a template.

Update a template.

Delete a template.

ali_oos_template_info

Query a template.

ali_oos_execution

Execute a template.

Cancel the execution of a template.

Delete the execution of a template.

Approve actions.

ali_oos_execution_info

Query the execution of a template.

Prerequisites

Ansible is installed. You can perform the following steps to install Ansible.

1. Run the following command to install Ansible:

sudo yum install ansible

For more information, see Installing Ansible.

2. Run the following command to view the version of the installed Ansible.

ansible -version

3. Run the following command to install the Ansible modules for Alibaba Cloud.

sudo pip install ansible_alicloud

4. Optional. If the version of the Ansible modules for Alibaba Cloud does not meet your business requirements, you can run the following command to update the version of the Ansible modules.

sudo pip install footmark 
sudo pip install ansible_alicloud

5. Run the following command to configure an AccessKey pair to access Alibaba Cloud resources.

export ALICLOUD_ACCESS_KEY="your_accesskey"
export ALICLOUD_SECRET_KEY="your_accesskey_secret"

For more information, see Obtain an AccessKey pair.

OOS playbook

1. Create a template.

- name: Create oos template
  ali_oos_template:
    alicloud_region: '{{ alicloud_region }}'
    template_name: '{{ template_name }}'
    content: '{{ content }}'
  register: create_template

2. Query a template.

- name: Get oos template
  ali_oos_template_info:
    alicloud_region: '{{ alicloud_region }}'
    name_prefix: '{{ name_prefix }}'
  register: get_template

3. Delete a template.

- name: Delete oos template
  ali_oos_template:
    state: absent
    alicloud_region: '{{ alicloud_region }}'
    template_name: '{{ template_name }}'

4. Update a template.

- name: Update oos template
  ali_oos_template:
    content: '{{ content }}'
    alicloud_region: '{{ alicloud_region }}'
    template_name: '{{ template_name }}'

5. Execute a template.

- name: Start a execution
  ali_oos_execution:
    alicloud_region: '{{ alicloud_region }}'
    template_name: '{{ template_name }}'
    safety_check: Skip
    parameters:
      Status: '{{ status }}'
  register: start_execution

6. Cancel the execution of a template.

- name: Cancel a execution
  ali_oos_execution:
    state: cancel
    alicloud_region: '{{ alicloud_region }}'
    execution_id: '{{ execution_id }}'
  register: cancel_execution

7. Query the execution of a template.

- name: Get executions
  ali_oos_execution_info:
    alicloud_region: '{{ alicloud_region }}'
    name_prefix: '{{ name_prefix }}'
  register: get_executions

8. Delete the execution of a template.

- name: Delete a execution
  ali_oos_execution:
    state: absent
    alicloud_region: '{{ alicloud_region }}'
    execution_id: '{{ execution_id }}'
  register: delete_execution

9. Approve actions.

- name: Notify a execution
  ali_oos_execution:
    state: notify
    notify_type: Approve
    alicloud_region: '{{ alicloud_region }}'
    execution_id: '{{ execution_id }}'
  register: notify_execution
Important

If you need other parameters, refer to the sample parameters that are provided.

Run the Ansible playbook to manage OOS operations

The following section provides an example of running the Ansible playbook to manage OOS operations. Perform the following steps to create a template and execute the created template. You can replace the parameters based on your business requirements.

1. Create a file named alicloud_describe_instances.yml and open it in edit mode.

vi alicloud_describe_instances.yml

2. In edit mode, copy the following playbook code to the alicloud_describe_instances.yml file:

---
- name: test create template and execute template
  hosts: localhost
  remote_user: root
  tasks:
    - name: Create oos template
      ali_oos_template:
        alicloud_region: 'cn-hangzhou'
        template_name: 'test-ansible-template'
        content: '{"Description": "Example template, describe instances in some status", "FormatVersion": "OOS-2019-06-01", "Parameters": {"Status": {"Description": "(Required) Running or Stopped", "Type": "String"}}, "Tasks": [{"Name": "describeInstances", "Action": "ACS::ExecuteAPI", "Description": {"zh-cn": "desc", "en": "desc-en"}, "Properties": {"Service": "ECS", "API": "DescribeInstances", "Parameters": {"Status": "\{\{ Status \}\}"}}, "Outputs": {"InstanceIds": {"Type": "List", "ValueSelector": "Instances.Instance[].InstanceId"}}}], "Outputs": {"InstanceIds": {"Type": "List", "Value": "\{\{ describeInstances.InstanceIds \}\}"}}}'
      register: create_template
    - name: Describe instances by status
      ali_oos_execution:
        alicloud_region: 'cn-hangzhou'
        template_name: 'test-ansible-template'
        safety_check: Skip
        description: test execution from ansible.
        parameters:
          Status: 'Running'
      register: start_execution

3. Save the file and exit the edit mode.

4. Run the Ansible playbook to create and execute a template.

ansible-playbook alicloud_describe_instances.yml

5. View the execution result.

5.1. View the execution result of the template to check whether the template is successfully executed.

image

5.2. If the execution is successful in the result, you can log on to the OOS console to check whether the task that is executed by using Ansible takes effect.