AWS的Ansible Automation Platform Controller 4.1.0入门指南

本文是《AP Communications 2021年 Advent Calendar》的第25篇文章。

首先 ,

2021年12月2日,Ansible Automation Platform 2.1发布。
相应地,Ansible Automation Platform Controller也升级到了4.1.0版本。

还有许多其他的更新,但其中最引人注目的是新增了”自动化网格”功能,这似乎是一个重要的变化。

Ansible控制器与Ansible执行节点被分离,它们像”网状结构”一样连接,从而实现了进一步自动化扩展到全球的可能性。
全球范围内拥有IT资源的企业将特别值得关注。

希望本篇文章能对 Ansible 自动化平台 2.1 中负责自动化控制器功能的 Ansible 自动化平台控制器 4.1.0 的理解有所帮助。

■起初,什么是自动化平台控制器?

让我们整理一下包括Automation Platform Controller在内的整体外观吧。

首先,Ansible Automation Platform 是一个广义上的自动化平台,用于整合各个组件。这次要介绍的Ansible Automation Platform Controller 就是其中的一个工具。

image.png

基本上,您可以將其想像成登錄到Ansible自動化平台控制器並組織性地管理自動化的一種控制塔。之前可能稱之為Ansible「Tower」,因為它像一座控制「塔」。

介绍红帽Ansible Automation平台2.1- 引用自《画像》
https://www.ansible.com/blog/introducing-red-hat-ansible-automation-platform-2.1

■现场实操

现在,我们来做一下以 Ansible Automation Platform Controller 4.1.0 为基础的入门操作。我们将执行一个任务模板,用于从 NW 设备获取信息。

为了让Ansible初学者能够开始学习,我们将执行playbook本身简化为一个简单的示例。
我们主要关注从”环境准备”到”执行作业模板”的整个流程,尽可能地将整体过程可视化。

image.png

■操作实践的全部步骤

以下是本次动手实践的全部步骤。
让我们按照顺序进行第1至第7步。
预计完成时间为2到3小时。

① 准备执行对象的网络设备。
② 创建 Automation Platform Controller 4.1.0 的实例(要求 RHEL8.4 或更高版本,m5.large 及以上规格)。
③ 在第②步创建的实例中安装 Ansible Automation Platform 2.1。
④ 使用 Ansible-builder 构建容器执行环境后,创建 playbook。
⑤ 登录 Automation Platform Controller 4.1.0,进行各种设置以执行作业模板。
⑥ 执行作业模板。
⑦ 确认成功获取了网络设备的信息。

准备要执行的网络设备。

首先,准备执行的网络设备。
使用亚马逊云上的虚拟设备。

在iOS系列的路由器中,任何一种都可以,但在这里我们决定使用”Cisco Cloud Services Router(CSR)1000V – BYOL for Maximum Performance”。

image.png

选择合适的t2.medium实例类型并启动。
请确保安全组的入站规则至少允许SSH连接。

用Teraterm(或其他)工具登录到创建的Cisco Cloud Services Router(CSR)1000V,在Automation Platform Controller 4.1.0上设置用于连接的用户名和密码。

Router#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
Router(config)#username ansible password passw0rd

在这里,用户设置了ansible作为用户名和passw0rd作为密码。

网络设备已准备就绪,可以开始使用了。

建立一个适用于自动化平台控制器 4.1.0 的实例(要求使用 RHEL8.4 或更高版本,并选择 m5.large 或更大的尺寸)。

首先,我们将使用RHEL8.4或更高版本的AMI来创建实例,并构建自动化平台控制器4.1.0。

image.png

根据官方文件要求,实例的类型必须选择m5.large以上,因此我将遵守并选择m5.large。

image.png

在安全组的入站规则中,请最低限度允许SSH和HTTPS。

创建并启动实例。

在之前的步骤中,在第②步所创建的实例中安装Ansible Automation Platform 2.1。

首先,获取Ansible Automation Platform 2.1的安装程序。
访问 https://access.redhat.com/downloads/content/480。

我决定使用捆绑版安装程序。(安装程序包含依赖的rpm文件,非常方便。)点击Ansible自动化平台设置捆绑包的「立即下载」按钮。

image.png

请通过SCP等方式将已下载到本地PC上的ansible-automation-platform-setup-bundle-2.1.0-1.tar.gz发送到远程实例的/home/ec2-user目录下进行部署。

确认/home/ec2-user目录下存在ansible-automation-platform-setup-bundle-2.1.0-1.tar.gz。

[ec2-user]$ pwd
/home/ec2-user
[ec2-user]$ ls
ansible-automation-platform-setup-bundle-2.1.0-1.tar.gz 

使用tar进行解压缩。

[ec2-user]$ tar xvzf ansible-automation-platform-setup-bundle-2.1.0-1.tar.gz
[ec2-user]$ ls
ansible-automation-platform-setup-bundle-2.1.0-1  ansible-automation-platform-setup-bundle-2.1.0-1.tar.gz

展开后,将生成ansible-automation-platform-setup-bundle-2.1.0-1目录,然后进入该目录并使用ls命令来确认其子目录下的文件。

[ec2-user]$ cd ansible-automation-platform-setup-bundle-2.1.0-1
[ec2-user]$ ls
bundle  collections  group_vars  images  inventory  licenses  README.md  setup.sh

请用vi编辑器修改inventory文件。

[ec2-user]$ vi inventory

当启动vi编辑器时,将会出现以下这样的界面。

# Automation Controller Nodes
# There are two valid node_types that can be assigned for this group.
# A node_type=control implies that the node will only be able to run
# project and inventory updates, but not regular jobs.
# A node_type=hybrid will have the ability to run everything.
# If you do not define the node_type, it defaults to hybrid.
#
# control.example node_type=control
# hybrid.example  node_type=hybrid
# hybrid2.example <- this will default to hybrid
[automationcontroller]
localhost ansible_connection=local

(中略)

[all:vars]
admin_password='passw0rd' 

pg_host=''
pg_port=''

pg_database='awx'
pg_username='awx'
pg_password='passw0rd'
pg_sslmode='prefer'  # set to 'verify-full' for client-side enforced SSL

# Execution Environment Configuration
# Credentials for container registry to pull execution environment images from,
# registry_username and registry_password are required for registry.redhat.io
registry_url='registry.redhat.io'
registry_username='アカウント名'
registry_password='パスワード'

(後略)

通过在此inventory文件中记录automationmesh的配置、automationhub的设置以及EE的存储库设置等,可以实现各种系统配置。

由于本次使用的是自动化控制器,因此只使用当前已登录的单个节点。

管理密码=’passw0rd’
pg密码=’passw0rd’
注册表用户名=’アカウント名’#Red Hat帐户
注册表密码=’パスワード’#Red Hat帐户密码

只需对这4个地方进行编辑即可。保存更新并以sudo权限运行setup.sh。

[ec2-user]$ sudo ./setup.sh
(略)

PLAY RECAP *********************************************************************
localhost                  : ok=272  changed=138  unreachable=0    failed=0    skipped=147  rescued=0    ignored=6   

The setup process completed successfully.
Setup log saved to /var/log/tower/setup-2021-xx-xx-xx:xx:xx.log.
[ec2-user]$ 

请稍等一会儿,最终将failed设置为0并成功完成Ansible自动化平台控制器 4.1.0的设置。

image.png

在这里,需要提供订阅信息,您需输入Red Hat账户的资料,然后点击获取订阅。

如果您没有订阅,可以使用免费的评估版许可证(60天的Red Hat Ansible Automation Platform产品试用版,支持100个受管节点https://www.redhat.com/ja/technologies/management/ansible/try-it)。请将其与您的账户关联后,再回到本步骤。

image.png

在订阅注册后,同意2个用户和洞察分析,并发送3个最终用户许可协议,“Ansible Automation Platform Controller 4.1.0”的仪表板将显示。

image.png

使用这个,Ansible自动化平台控制器4.1.0的设置已完成。

在使用Ansible-builder构建容器执行环境之后,创建playbook。

从这里开始,我们将使用CLI来建立执行环境等等。
虽然已经建立了一个称为Ansible Automation Platform Controller 4.1.0的“盒子”,但其内部(如执行环境和playbook等)还不存在,所以我们要创建它。

首先,使用ec2-user通过SSH登录到之前创建的 RHEL8.4 实例(标号为 ②)。

现在,你不需要通过yum或pip来安装各种库或包。

实际上,在安装Ansible Automation Platform 2.1时,

ansible-core、ansible-runner和podman等许多软件包和库已经默认安装。

我们试着确认一下Ansible的版本和pip包。


[ec2-user]$ ansible --version
ansible [core 2.12.0]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/var/lib/awx/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.8/site-packages/ansible
  ansible collection location = /var/lib/awx/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.8.6 (default, Jan 22 2021, 11:41:28) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
  jinja version = 2.10.3
  libyaml = True
[ec2-user]$ pip3 freeze
ansible-core==2.12.0
ansible-runner==2.1.1
asn1crypto==1.2.0
Babel==2.7.0
cffi==1.13.2
click==7.1.2
cryptography==2.8
docutils==0.14
idna==2.8
Jinja2==2.10.3
lockfile==0.11.0
MarkupSafe==1.1.1
pexpect==4.6.0
ply==3.11
ptyprocess==0.5.2
pycparser==2.19
python-daemon==2.1.2
python-dateutil==2.6.1
pytz==2019.3
PyYAML==5.4.1
receptorctl==1.1.1
resolvelib==0.5.4
six==1.12.0

[ec2-user]$ podman --version
podman version 3.3.1

可以确认 ansible-core==2.12.0、ansible-runner==2.1.1 已安装,并且 python 版本为 3.8.6,podman 版本为 3.3.1。

在本次验证中所需的仅有一项是安装ansible-builder,因此我们将安装ansible-builder。

此外,Podman已默认安装,但Docker并不是标准安装的,如果您确实需要使用Docker,请额外进行安装。

[ec2-user]$ sudo pip3 install ansible-builder
Successfully installed Parsley-1.3 ansible-builder-1.0.1 bindep-2.10.1 distro-1.6.0 packaging-21.3 pbr-5.8.0 pyparsing-3.0.6 requirements-parser-0.2.0

我成功安装了 Ansible Builder。现在,我已经准备好完成本次验证所需的所有软件包了。

现在,这里很重要的一点是,接下来我们要切换到awx用户并继续进行工作。
Ansible自动化平台控制器4.1.0是为了让awx用户能够访问本地的Ansible环境。

这是因为,例如,如果在root或ec2-user中创建本地的执行环境容器,awx用户将无法访问位于本地的该执行环境容器。

由于这次任务是在本地完成的,所以我将使用awx用户进行操作。

[ec2-user]$ sudo su - awx
[awx]$

我成功地转到awx用户,并将执行以下操作。

[awx]$ pwd
/var/lib/awx   #作業ディレクトリは/var/lib/awx
[awx]$ ls
job_status  projects  public  rsyslog  uwsgi.stats  venv 
[awx]$ 
[awx]$ mkdir builder   #/var/lib/awx配下に「builder」ディレクトリを作成
[awx]$ cd builder
[awx]$ vi requirements.yml   #/var/lib/awx/builder/配下にrequirements.ymlを作成

创建 requirements.yml 文件。
在该文件中定义希望安装在容器运行环境中的 Ansible 集合。
这次我们想要安装 cisco.ios,所以写成以下这样。

---
collections:
  - name: cisco.ios

接下来,我们将创建Ansible的配置文件ansible.cfg。

[awx]$ vi ansible.cfg
[defaults]
host_key_checking = False

最后,我们将整理requirements.yml、ansible.cfg等文件,并创建execution-environment.yml来搭建容器执行环境。

[awx]$ vi execution-environment.yml
---
version: 1

build_arg_defaults:
  EE_BASE_IMAGE: 'quay.io/ansible/ansible-runner:stable-2.12-devel' #ベースイメージを定義

dependencies:
  galaxy: requirements.yml

additional_build_steps: # ビルド手順の前(prepend)または後(appendに追加コマンドを指定できる。今回は特に必要ないので、サンプルと同じ。適当。
  prepend: |
    RUN whoami
    RUN cat /etc/os-release
  append:
    - RUN echo This is a post-install command!
    - RUN ls -la /etc

当您完成了这个步骤,就准备好构建容器执行环境了。
使用ansible-builder进行构建。

[awx]$ ansible-builder build
Running command:
  podman build -f context/Containerfile -t ansible-execution-env:latest context
Complete! The build context can be found at: /var/lib/awx/builder/context

[awx]$ podman images
REPOSITORY                       TAG                IMAGE ID      CREATED             SIZE
localhost/ansible-execution-env  latest             36f9480d7fbb  About a minute ago  820 MB
<none>                           <none>             a6b09c838ade  2 minutes ago       836 MB
<none>                           <none>             69eabd503cc3  3 minutes ago       777 MB
quay.io/ansible/ansible-runner   stable-2.12-devel  cf3335b25a2a  8 hours ago         763 MB
quay.io/ansible/ansible-builder  latest             0ca8b4e5d250  8 hours ago         655 MB

等待一会儿直到构建完成。
显示“完成!”,并创建了一个名为localhost/ansible-execution-env:latest的容器执行环境映像。

这里其实只是关于内部操作的一个小插曲。基于这个 localhost/ansible-execution-env:latest 镜像,每次使用 ansible-runner 执行 playbook,都会创建一个执行环境容器,当 playbook 执行完毕后,容器会自动删除。然后,再次执行 playbook,会再次根据 localhost/ansible-execution-env:latest 镜像创建执行环境容器……如此循环进行下去。

接下来,我们将部署执行的playbook。

在/var/lib/awx/projects/下创建一个名为ios的目录,并将playbook放在其中。

请注意,如果在/var/lib/awx/projects/目录下直接放置playbook,则可能无法被识别。

[awx]$ cd projects/
[awx]$ mkdir ios
[awx]$ cd ios
[awx]$ vi show_version.yml

我会创建一个playbook来收集网络设备的版本,并进行标准输出。

---
- hosts: ios
  gather_facts: no

  tasks:
    - name: show test
      ios_command:
        commands:
          - show version 
      register: result

    - name: debug test
      debug:
        msg: "{{ result }}" 

现在,CLI的所有准备工作都已经完成。

登陆 Automation Platform Controller 4.1.0,并进行各种设置以执行作业模板。

image.png

在创建新的执行环境时,请按照以下方式输入和选择:
在名称栏中输入任意的执行环境名称。
在镜像栏中注册步骤4中创建的localhost/ansible-execution-env:latest容器镜像。
输入和选择完成后,点击“保存”。

image.png
image.png
image.png
image.png
image.png

进行所属库存的组设置。

image.png
image.png
image.png
image.png
image.png
image.png
image.png

现在,一切准备就绪。
只需执行作业模板即可。

执行作业模板。

image.png

确认网络设备信息的获取成功。

image.png

■总结

在本文中,我们介绍了安装Ansible Automation Platform 2.1并构建Ansible Automation Platform Controller 4.1.0的步骤,以及执行用于获取网络设备信息的作业模板的流程。

本次只是单节点结构,Ansible Automation Platform Controller 4.1.0 还可以采用更复杂的节点结构。

我想将下一个主题设定为“自动化网格”,并希望将分散多节点结构的验证整理成一篇文章。

谢谢您的阅读。
祝您度过一个愉快的圣诞节。

■ 参考如下

Red Hat Ansible Automation Platform 2.1的产品文档
https://access.redhat.com/documentation/zh-cn/red_hat_ansible_automation_platform/2.1

红帽Ansible自动化平台2.1安装指南
https://access.redhat.com/documentation/en-us/red_hat_ansible_automation_platform/2.1/html/red_hat_ansible_automation_platform_installation_guide/index

自动化控制器用户指南v4.1.0
https://docs.ansible.com/automation-controller/latest/html/userguide/index.html

介绍红帽Ansible自动化平台2.1版本
https://www.ansible.com/blog/introducing-red-hat-ansible-automation-platform-2.1

bannerAds