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 等严重安全问题

在 1 月的第一周,宣布了两个漏洞: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 国际许可协议进行许可。
© . All rights reserved.