4 个你应该尝试的 Ansible Playbook

使用这些 Ansible Playbook,在复杂的 IT 环境中简化和加强自动化流程。
299 位读者喜欢这篇文章。
How Kubernetes became the solution for migrating legacy applications

Opensource.com

在复杂的 IT 环境中,即使是最小的任务也可能看起来遥遥无期。庞大的系统难以开发、部署和维护。业务需求只会增加复杂性,而 IT 团队则在管理、可用性和成本方面苦苦挣扎。

如何在满足当今业务需求的同时应对这种复杂性?毫无疑问,Ansible 可以改进您当前的流程,迁移应用程序以获得更好的优化,并为整个组织的 DevOps 实践提供一种通用语言。

更重要的是,您可以通过 Ansible Playbook 声明配置,它们可以编排任何手动有序流程的步骤,即使不同的步骤必须在特定顺序的机器组之间来回跳转。它们可以同步或异步启动任务。

虽然您可能会运行主 /usr/bin/ansible program 来执行临时任务,但 Playbook 更可能保存在源代码控制中,并用于推送您的配置或确保远程系统的配置符合规范。由于 Ansible Playbook 是配置、部署和编排语言,它们可以描述您希望远程系统执行的策略或通用 IT 流程中的一组步骤。

这里有四个你应该尝试的 Ansible Playbook,它们可以进一步自定义和配置您的自动化工作方式。

管理 Kubernetes 对象

当您对 Kubernetes 对象执行 CRUD 操作时,Ansible Playbook 使您能够通过 OpenShift Python 客户端快速轻松地访问 Kubernetes API 的全部功能。以下 Playbook 代码片段展示了如何创建特定的 Kubernetes 命名空间和服务对象

- name: Create a k8s namespace
  k8s:
    name: mynamespace
    api_version: v1
    kind: Namespace
    state: present

- name: Create a Service object from an inline definition
  k8s:
    state: present
    definition:
      apiVersion: v1
      kind: Service
      metadata:
        name: web
        namespace: mynamespace
        labels:
          app: galaxy
          service: web
      spec:
        selector:
          app: galaxy
          service: web
        ports:
        - protocol: TCP
          targetPort: 8000
          name: port-8000-tcp
          port: 8000

- name: Create a Service object by reading the definition from a file
  k8s:
    state: present
    src: /mynamespace/service.yml

# Passing the object definition from a file
- name: Create a Deployment by reading the definition from a local file
  k8s:
    state: present
    src: /mynamespace/deployment.yml

缓解 Meltdown 和 Spectre 等严重安全问题

一月份的第一个星期,宣布了两个漏洞:Meltdown 和 Spectre。两者都涉及到几乎所有计算设备的核心硬件:处理器。这里有一篇关于这两个漏洞的深入评测。虽然 Meltdown 和 Spectre 尚未完全缓解,但以下 Playbook 代码片段展示了如何轻松部署 Windows 的补丁

- name: Patch Windows systems against Meltdown and Spectre
  hosts: "{{ target_hosts | default('all') }}"

  vars:
    reboot_after_update: no
    registry_keys:
      - path: HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management
        name: FeatureSettingsOverride
        data: 0
        type: dword

      - path: HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management
        name: FeatureSettingsOverrideMask
        data: 3
        type: dword

      # https://support.microsoft.com/en-us/help/4072699
      - path: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\QualityCompat
        name: cadca5fe-87d3-4b96-b7fb-a231484277cc
        type: dword
        data: '0x00000000'

  tasks:
    - name: Install security updates
      win_updates:
        category_names:
          - SecurityUpdates
      notify: reboot windows system

    - name: Enable kernel protections
      win_regedit:
        path: "{{ item.path }}"
        name: "{{ item.name }}"
        data: "{{ item.data }}"
        type: "{{ item.type }}"
      with_items: "{{ registry_keys }}"

  handlers:
    - name: reboot windows system
      win_reboot:
        shutdown_timeout: 3600
        reboot_timeout: 3600
      when: reboot_after_update

您还可以找到用于 Linux 的其他 Playbook。

将 CI/CD 流程与 Jenkins 集成

Jenkins 是一个用于实现 CI/CD 的知名工具。Shell 脚本通常用于在管道流程中配置环境或部署应用程序。虽然这可能有效,但从长远来看,维护和重用脚本很麻烦。以下 Playbook 代码片段展示了如何在使用 Jenkins Pipeline 的持续集成/持续交付 (CI/CD) 流程中配置基础设施。

---
- name: Deploy Jenkins CI
hosts: jenkins_server
remote_user: vagrant
become: yes

roles:
  - geerlingguy.repo-epel
  - geerlingguy.jenkins
  - geerlingguy.git
  - tecris.maven
  - geerlingguy.ansible

- name: Deploy Nexus Server
hosts: nexus_server
remote_user: vagrant
become: yes

roles:
  - geerlingguy.java
  - savoirfairelinux.nexus3-oss

- name: Deploy Sonar Server
hosts: sonar_server
remote_user: vagrant
become: yes

roles:
  - wtanaka.unzip
  - zanini.sonar

- name: On Premises CentOS
hosts: app_server
remote_user: vagrant
become: yes

roles:
  - jenkins-keys-config

使用 Istio 启动服务网格

借助云平台,开发人员必须使用微服务来构建可移植性。与此同时,运维人员正在管理极其庞大的混合云和多云部署。使用 Istio 的服务网格使您可以通过专用基础设施(例如 Envoy Sidecar 容器)连接、保护、控制和观察服务,而不是开发人员。以下 Playbook 代码片段展示了如何在本地计算机上安装 Istio

---

# Whether the cluster is an Openshift (ocp) or upstream Kubernetes (k8s) cluster
cluster_flavour: ocp

istio:
  # Install istio with or without istio-auth module
  auth: false

  # A set of add-ons to install, for example kiali
  addon: []

  # The names of the samples that should be installed as well.
  # The available samples are in the istio_simple_samples variable
  # In addition to the values in istio_simple_samples, 'bookinfo' can also be specified
  samples: []

  # Whether or not to open apps in the browser
  open_apps: false

  # Whether to delete resources that might exist from previous Istio installations
  delete_resources: false

结论

您可以在 ansible-examples 存储库中找到完整的 Playbook 集,其中演示了许多这些技术。我建议您在继续阅读时在另一个选项卡中查看这些内容。

希望这些技巧和 Ansible Playbook 代码片段为您提供了一些有趣的方式来使用和扩展您的自动化之旅。

标签
danieloh
技术营销,开发者倡导者,CNCF 大使,公开演讲者,已出版作者,Quarkus,红帽运行时

6 条评论

非常有用!!

您所说的“使用 Istio 的服务网格使您可以通过专用基础设施(例如 Envoy Sidecar 容器)连接、保护、控制和观察服务,而不是开发人员”是什么意思?
听起来您好像在说您想要“...观察服务而不是开发人员”,这对我来说毫无意义。

在使用 Istio 服务网格之前,开发人员必须在 Spring Boot、Netflix OSS 等之上实现客户端负载均衡、服务发现、服务注册表、追踪等非功能性能力。在使用 Istio 之后,Envoy Sidecar 容器(也称为专用基础设施)将解决这些能力,这意味着开发人员现在专注于业务逻辑的实现。希望这能让您理解这段话。

回复 来自 rgwertg (未验证)

感谢分享!

Daniel,这些代码片段太简洁了,我认为单独来看对任何人都没有用。您真的是说上面 Istio 示例 Ansible 代码本身就可以完成 Istio 的完整安装吗?如何安装? 谢谢,如果我误解了,请提前道歉。

Creative Commons License本作品根据知识共享署名-相同方式共享 4.0 国际许可协议获得许可。
© 2025 open-source.net.cn. All rights reserved.