在复杂的 IT 环境中,即使是最小的任务也似乎需要很长时间。 庞大的系统难以开发、部署和维护。 业务需求只会增加复杂性,而 IT 团队则在管理、可用性和成本方面苦苦挣扎。
您如何应对这种复杂性,同时满足当今的业务需求?毫无疑问,Ansible 可以改进您当前的流程,迁移应用程序以进行更好的优化,并为组织内的 DevOps 实践提供单一语言。
更重要的是,您可以通过 Ansible Playbook 声明配置,但它们会编排任何手动有序流程的步骤,即使不同的步骤必须以特定的顺序在机器集之间来回跳转。 它们可以同步或异步启动任务。
虽然您可以运行主要的 /usr/bin/ansible 程序
来执行临时任务,但 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 流水线在持续集成/持续交付 (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 集合,这些 Playbook 说明了许多这些技术。 我建议您在另一个选项卡中查看这些内容。
希望这些提示和 Ansible Playbook 代码段提供了一些有趣的方式来使用和扩展您的自动化之旅。
6 条评论