Ansible是一个开源配置管理工具,可以使用它来自动化执行任务,部署应用来实现IT基础架构。
OOS是一个以模板的方式管理阿里云产品来实现自动化运维的一个服务。相信读此教程的大家已经了解OOS的基本功能与使用方法,本教程将指导您如何使用Ansible创建阿里云的OOS运维模版,以及如何通过Ansible执行创建的模板来管理阿里云产品。
教程概览
本教程将创建和执行模版拆分成了不不同的Ansible playbooks,方便您了解如何通过YAML格式声明配置。您可以参考提供简单的完整示例,运行Playbook来创建并执行一个OOS模版。
Ansible包含OOS的四个模块,你可以使用ali_oos_template、ali_oos_template_info、ali_oos_execution、ali_oos_execution_info四个模块来进行以下操作。
模块 | 使用场景 |
ali_oos_template | 创建模版 |
更新模版 | |
删除模版 | |
ali_oos_template_info | 获取模版 |
ali_oos_execution | 开始执行 |
取消执行 | |
删除执行 | |
审批动作 | |
ali_oos_execution_info | 获取执行 |
前提条件
在执行前需要安装Ansible,详细安装操作与部分配置请参考为您提供的官方文档安装和配置Ansible。
需要将以下内容提前配置到您执行Ansible playbooks的机器中。
export ALICLOUD_ACCESS_KEY="your_accesskey"
export ALICLOUD_SECRET_KEY="your_accesskey_secret"
OOS的PlayBook
1、创建模版
- name: Create oos template
ali_oos_template:
alicloud_region: '{{ alicloud_region }}'
template_name: '{{ template_name }}'
content: '{{ content }}'
register: create_template
2、获取模版
- name: Get oos template
ali_oos_template_info:
alicloud_region: '{{ alicloud_region }}'
name_prefix: '{{ name_prefix }}'
register: get_template
3、删除模版
- name: Delete oos template
ali_oos_template:
state: absent
alicloud_region: '{{ alicloud_region }}'
template_name: '{{ template_name }}'
4、更新模版
- name: Update oos template
ali_oos_template:
content: '{{ content }}'
alicloud_region: '{{ alicloud_region }}'
template_name: '{{ template_name }}'
5、执行模版
- 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、取消执行
- name: Cancel a execution
ali_oos_execution:
state: cancel
alicloud_region: '{{ alicloud_region }}'
execution_id: '{{ execution_id }}'
register: cancel_execution
7、获取执行
- name: Get executions
ali_oos_execution_info:
alicloud_region: '{{ alicloud_region }}'
name_prefix: '{{ name_prefix }}'
register: get_executions
8、删除执行
- name: Delete a execution
ali_oos_execution:
state: absent
alicloud_region: '{{ alicloud_region }}'
execution_id: '{{ execution_id }}'
register: delete_execution
9、审批动作
- name: Notify a execution
ali_oos_execution:
state: notify
notify_type: Approve
alicloud_region: '{{ alicloud_region }}'
execution_id: '{{ execution_id }}'
register: notify_execution
如果有其他的参数需求,参考提供的参数样式,根据OOS提供的简介来添加您的参数。
运行PlayBook操作OOS步骤
以下为提供的一个简单例子。请根据步骤完成以下操作来创建一个模版,以及执行创建的模版。并根据您的实际需求进行参数替换。
1、编写一个alicloud_describe_instances.yml
vi alicloud_describe_instances.yml
2、在编辑模式下将以下PlayBook复制进alicloud_describe_instances.yml中
---
- 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、保存后退出编辑模式
4、运行Ansible PlayBook创建模版并执行。
ansible-playbook alicloud_describe_instances.yml
5. 执行结果
5.1查看Ansible的执行结果,检测Ansible是否执行成功。
5.2 如果命令中的Ansible执行成功, 可以登录OOS控制台,查看使用Ansible执行的task是否实际生效。