运维编排场景系列---在ECS实例上运行Ansible-playbook,linux运维项目经验案例


场景介绍

Ansible是一个简单的自动化运维管理工具,基于Python语言实现,由Paramiko和PyYAML两个关键模块构建,可用于自动化部署应用、配置、编排task(持续交付、无宕机更新等)。本文介绍通过OOS在实例中运行指定OSS中的PlayBook。

模板介绍

  1. 模板内容:
FormatVersion: OOS-2019-06-01
Description: Use this template to run Ansible playbooks on ECS Instance.
Parameters:
  instanceId:
    Description: The ID of ECS Instance.
    Type: String
    MaxLength: 30
    MinLength: 1
  playbookurl:
    Type: String
    Description: "You must specify a URL where the playbook is stored.You can specify the URL in the following formats: http://example.com/playbook.yml"
    AllowedPattern: ^\s*$|^(http|https)://[^']*$
  extravars:
    Type: String
    Description: 'Additional variables to pass to Ansible at runtime. Enter a space separated list of key/value
      pairs. For example: color=red flavor=lime'
    Default: SSM=True
    AllowedPattern: ^$|^\w+\=\S+(\s\w+\=\S+)*$
  check:
    Type: String
    Description: 'Use the check parameter to perform a dry run of the Ansible execution.'
    AllowedValues:
    - 'True'
    - 'False'
    Default: 'False'
  OOSAssumeRole:
    Description: The RAM role to be assumed by OOS.
    Type: String
    Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
- Name: runCommand
  Action: ACS::ECS::RunCommand
  Properties:
    commandContent:
      Fn::Join:
        - ' '
        - - ansible --version;
          - if [ $? -ne 0 ]; then
          -   echo "Ansible is not installed. Please install Ansible and rerun the command" >&2;
          -   exit 1;
          - fi;
          - if [[ {{playbookurl}} == http* ]]; then
          -   wget {{playbookurl}} -O playbook.yml;
          -   if [ $? -ne 0 ]; then
          -     echo "There was a problem downloading the playbook. Make sure the URL is correct and that the playbook exists.">&2;
          -     exit 1;
          -   fi;
          - fi;
          - if  [[ {{check}} == True ]] ; then
          -     ansible-playbook --check -c local -e {{extravars}} playbook.yml;
          - else
          -     ansible-playbook -c local -e {{extravars}} playbook.yml;
          - fi;
    instanceId: '{{ instanceId }}'
    commandType: RunShellScript
  Outputs:
    commandOutput:
      Type: String
      ValueSelector: invocationOutput
Outputs:
  commandOutput:
    Type: String
    Value: '{{ runCommand.commandOutput }}'

2.模板参数:

  • instanceId:实例ID
  • playbookurl:playbook下载路径
  • extravars:运行剧本的参数
  • check:检测模式

3.剧本介绍:
  在oss中准备一个需要运行的剧本:
a1

  本文所用剧本内容如下:

---
- hosts: local
  connection: local
  become: yes
  become_user: root
  tasks:
   - name: Install Nginx
     yum:
       name: nginx
       state: installed
       update_cache: true

4.实例介绍:
准备一台已安装Ansible实例,Ansible的hosts配置如下:
a2

操作指引

  1. 首先登录到运维编排控制台 。
  2. 单击 自定义模版 ,单击 创建模版 。
  3. 单击 YAML 。


 将上面提供的YAML格式模版内容粘贴到编辑框内。
a3

  1. 单击 校验模版。 
  • 若模板内容已通过校验,继续下一步。
  • 若模板内容校验失败,请根据提示内容调整模版。
  1. 填写 模版名称。


  本文此处填写"test_ansible_run_play_book" 。
a4

  1. 单击 创建模版 。


      在自定义模版列表中可通过模版名称快速找到该模版。

  1. 单击 创建执行 。
  2. 风险确认模式 选择 客户了解风险,无需确认 。


1568259457317_9be339ca_b5d0_4b1d_8ced_41c5dd0480c6

  1. 单击 下一步,设置参数 。

1568259659815_be0b521f_a4b1_4953_86e5_31a7eac06fe0

  1. 单击 下一步,确认创建 ,单击 创建执行 。
  2. 执行成功后单击详情,查看执行结果。

1568259799961_7d2d2d9e_eb8b_4531_a8e0_eb73dce1097d

12.进入远程链接,管理终端,查看playbook运行结果:

系列文章

主题文章

和通数据库重磅发布云上自动化利器——运维编排OOS

最佳实践

玩转运维编排服务的权限:Assume Role+Pass Role

场景系列

运维编排场景系列----更新ECS镜像
运维编排场景系列-----给ECS实例自动打TAG
运维编排场景系列----从实例中拷贝文件到OSS
运维编排场景系列----给实例加到SLS机器组
运维编排场景系列----检测MFA功能状态
和通数据库运维编排新功能:一键批量克隆ECS
运维编排场景系列-----每日统计多Region实例的运行状态
运维编排场景系列-----如何使用jq
运维编排场景系列----分批到机器上运行命令

相关内容