Ansible 剧本快速入门指南

运维 系统运维
我们已经写了两篇关于 Ansible 的文章,这是第三篇。如果你是 Ansible 新手,我建议你阅读下面这两篇文章,它会教你一些 Ansible 的基础以及它是什么。如果你已经阅读过了,那么在阅读本文时你才不会感到突兀。

[[384413]]

我们已经写了两篇关于 Ansible 的文章,这是第三篇。

如果你是 Ansible 新手,我建议你阅读下面这两篇文章,它会教你一些 Ansible 的基础以及它是什么。

如果你已经阅读过了,那么在阅读本文时你才不会感到突兀。

什么是 Ansible 剧本?

剧本playbook比点对点命令模式更强大,而且完全不同。

它使用了 /usr/bin/ansible-playbook 二进制文件,并且提供丰富的特性使得复杂的任务变得更容易。

如果你想经常运行一个任务,剧本是非常有用的。此外,如果你想在服务器组上执行多个任务,它也是非常有用的。

剧本是由 YAML 语言编写。YAML 代表一种标记语言,它比其它常见的数据格式(如 XML 或 JSON)更容易读写。

下面这张 Ansible 剧本流程图将告诉你它的详细结构。

 

理解 Ansible 剧本的术语

  • 控制节点Control node:Ansible 安装的机器,它负责管理客户端节点。
  • 受控节点Managed node:控制节点管理的主机列表。
  • 剧本playbook:一个剧本文件包含一组自动化任务。
  • 主机清单Inventory:这个文件包含有关管理的服务器的信息。
  • 任务Task:每个剧本都有大量的任务。任务在指定机器上依次执行(一个主机或多个主机)。
  • 模块Module: 模块是一个代码单元,用于从客户端节点收集信息。
  • 角色Role:角色是根据已知文件结构自动加载一些变量文件、任务和处理程序的方法。
  • 动作Play:每个剧本含有大量的动作,一个动作从头到尾执行一个特定的自动化。
  • 处理程序Handler: 它可以帮助你减少在剧本中的重启任务。处理程序任务列表实际上与常规任务没有什么不同,更改由通知程序通知。如果处理程序没有收到任何通知,它将不起作用。

基本的剧本是怎样的?

下面是一个剧本的模板:

  1. --- [YAML 文件应该以三个破折号开头]
  2. - name: [脚本描述]
  3. hosts: group [添加主机或主机组]
  4. become: true [如果你想以 root 身份运行任务,则标记它]
  5. tasks: [你想在任务下执行什么动作]
  6. - name: [输入模块选项]
  7. module: [输入要执行的模块]
  8. module_options-1: value [输入模块选项]
  9. module_options-2: value
  10. .
  11. module_options-N: value

如何理解 Ansible 的输出

Ansible 剧本的输出有四种颜色,下面是具体含义:

  • 绿色ok 代表成功,关联的任务数据已经存在,并且已经根据需要进行了配置。
  • 黄色changed 指定的数据已经根据任务的需要更新或修改。
  • 红色FAILED 如果在执行任务时出现任何问题,它将返回一个失败消息,它可能是任何东西,你需要相应地修复它。
  • 白色:表示有多个参数。

为此,创建一个剧本目录,将它们都放在同一个地方。

  1. $ sudo mkdir /etc/ansible/playbooks

剧本-1:在 RHEL 系统上安装 Apache Web 服务器

这个示例剧本允许你在指定的目标机器上安装 Apache Web 服务器:

  1. $ sudo nano /etc/ansible/playbooks/apache.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install and Configure Apache Web server"
  7. tasks:
  8. - name: "Install Apache Web Server"
  9. yum:
  10. name: httpd
  11. state: latest
  12. - name: "Ensure Apache Web Server is Running"
  13. service:
  14. name: httpd
  15. state: started
  1. $ ansible-playbook apache1.yml

 

如何理解 Ansible 中剧本的执行

使用以下命令来查看语法错误。如果没有发现错误,它只显示剧本文件名。如果它检测到任何错误,你将得到一个如下所示的错误,但内容可能根据你的输入文件而有所不同。

  1. $ ansible-playbook apache1.yml --syntax-check
  2.  
  3. ERROR! Syntax Error while loading YAML.
  4. found a tab character that violate indentation
  5. The error appears to be in '/etc/ansible/playbooks/apache1.yml': line 10, column 1, but may
  6. be elsewhere in the file depending on the exact syntax problem.
  7. The offending line appears to be:
  8. state: latest
  9. ^ here
  10. There appears to be a tab character at the start of the line.
  11.  
  12. YAML does not use tabs for formatting. Tabs should be replaced with spaces.
  13. For example:
  14. - name: update tooling
  15. vars:
  16. version: 1.2.3
  17. # ^--- there is a tab there.
  18. Should be written as:
  19. - name: update tooling
  20. vars:
  21. version: 1.2.3
  22. # ^--- all spaces here.

或者,你可以使用这个 URL YAML Lint 在线检查 Ansible 剧本内容。

执行以下命令进行“演练”。当你运行带有 --check 选项的剧本时,它不会对远程机器进行任何修改。相反,它会告诉你它将要做什么改变但不是真的执行。

  1. $ ansible-playbook apache.yml --check
  2.  
  3. PLAY [Install and Configure Apache Webserver] ********************************************************************
  4.  
  5. TASK [Gathering Facts] *******************************************************************************************
  6. ok: [node2.2g.lab]
  7. ok: [node1.2g.lab]
  8.  
  9. TASK [Install Apache Web Server] *********************************************************************************
  10. changed: [node2.2g.lab]
  11. changed: [node1.2g.lab]
  12.  
  13. TASK [Ensure Apache Web Server is Running] ***********************************************************************
  14. changed: [node1.2g.lab]
  15. changed: [node2.2g.lab]
  16.  
  17. PLAY RECAP *******************************************************************************************************
  18. node1.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  19. node2.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

如果你想要知道 ansible 剧本实现的详细信息,使用 -vv 选项,它会展示如何收集这些信息。

  1. $ ansible-playbook apache.yml --check -vv
  2.  
  3. ansible-playbook 2.9.2
  4. config file = /etc/ansible/ansible.cfg
  5. configured module search path = ['/home/daygeek/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  6. ansible python module location = /usr/lib/python3.8/site-packages/ansible
  7. executable location = /usr/bin/ansible-playbook
  8. python version = 3.8.1 (default, Jan 8 2020, 23:09:20) [GCC 9.2.0]
  9. Using /etc/ansible/ansible.cfg as config file
  10.  
  11. PLAYBOOK: apache.yml *****************************************************************************************************
  12. 1 plays in apache.yml
  13.  
  14. PLAY [Install and Configure Apache Webserver] ****************************************************************************
  15.  
  16. TASK [Gathering Facts] ***************************************************************************************************
  17. task path: /etc/ansible/playbooks/apache.yml:2
  18. ok: [node2.2g.lab]
  19. ok: [node1.2g.lab]
  20. META: ran handlers
  21.  
  22. TASK [Install Apache Web Server] *****************************************************************************************
  23. task path: /etc/ansible/playbooks/apache.yml:6
  24. changed: [node2.2g.lab] => {"changed": true, "msg": "Check mode: No changes made, but would have if not in check mod
  25. e", "rc": 0, "results": ["Installed: httpd"]}
  26. changed: [node1.2g.lab] => {"changed": true, "changes": {"installed": ["httpd"], "updated": []}, "msg": "", "obsolet
  27. es": {"urw-fonts": {"dist": "noarch", "repo": "@anaconda", "version": "2.4-16.el7"}}, "rc": 0, "results": []}
  28.  
  29. TASK [Ensure Apache Web Server is Running] *******************************************************************************
  30. task path: /etc/ansible/playbooks/apache.yml:10
  31. changed: [node1.2g.lab] => {"changed": true, "msg": "Service httpd not found on host, assuming it will exist on full run"}
  32. changed: [node2.2g.lab] => {"changed": true, "msg": "Service httpd not found on host, assuming it will exist on full run"}
  33. META: ran handlers
  34. META: ran handlers
  35.  
  36. PLAY RECAP ***************************************************************************************************************
  37. node1.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  38. node2.2g.lab : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

剧本-2:在 Ubuntu 系统上安装 Apache Web 服务器

这个示例剧本允许你在指定的目标节点上安装 Apache Web 服务器。

  1. $ sudo nano /etc/ansible/playbooks/apache-ubuntu.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install and Configure Apache Web Server"
  7. tasks:
  8. - name: "Install Apache Web Server"
  9. yum:
  10. name: apache2
  11. state: latest
  12.  
  13. - name: "Start the Apache Web Server"
  14. service:
  15. name: apaceh2
  16. state: started
  17.  
  18. - name: "Enable mod_rewrite module"
  19. apache2_module:
  20. name: rewrite
  21. state: present
  22.  
  23. notify:
  24. - start apache
  25.  
  26. handlers:
  27. - name: "Ensure Apache Web Server is Running"
  28. service:
  29. name: apache2
  30. state: restarted
  31. enabled: yes

剧本-3:在 Red Hat 系统上安装软件包列表

这个示例剧本允许你在指定的目标节点上安装软件包。

方法-1:

  1. $ sudo nano /etc/ansible/playbooks/packages-redhat.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install a List of Packages on Red Hat Based System"
  7. tasks:
  8. - name: "Installing a list of packages"
  9. yum:
  10. name:
  11. - curl
  12. - httpd
  13. - nano
  14. - htop

方法-2:

  1. $ sudo nano /etc/ansible/playbooks/packages-redhat-1.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install a List of Packages on Red Hat Based System"
  7. tasks:
  8. - name: "Installing a list of packages"
  9. yum: name={{ item }} state=latest
  10. with_items:
  11. - curl
  12. - httpd
  13. - nano
  14. - htop

方法-3:使用数组变量

  1. $ sudo nano /etc/ansible/playbooks/packages-redhat-2.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install a List of Packages on Red Hat Based System"
  7. vars:
  8. packages: [ 'curl', 'git', 'htop' ]
  9. tasks:
  10. - name: Install a list of packages
  11. yum: name={{ item }} state=latest
  12. with_items: "{{ packages }}"

剧本-4:在 Linux 系统上安装更新

这个示例剧本允许你在基于 Red Hat 或 Debian 的 Linux 系统上安装更新。

  1. $ sudo nano /etc/ansible/playbooks/security-update.yml
  2.  
  3. ---
  4. - hosts: web
  5. become: yes
  6. name: "Install Security Update"
  7. tasks:
  8. - name: "Installing Security Update on Red Hat Based System"
  9. yum: name=* update_cache=yes security=yes state=latest
  10. when: ansible_facts['distribution'] == "CentOS"
  11.  
  12. - name: "Installing Security Update on Ubuntu Based System"
  13. apt: upgrade=dist update_cache=yes
  14. when: ansible_facts['distribution'] == "Ubuntu"

 

 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2021-03-01 13:00:21

Ansible系统运维

2021-02-22 18:50:03

Ansible系统运维

2011-03-08 16:50:35

2019-11-13 15:44:17

Kafka架构数据

2020-11-25 19:05:50

云计算SaaS公有云

2021-06-29 08:00:00

Ansible开发工具

2021-01-24 16:00:22

Ansible系统运维

2010-12-23 13:45:23

Office 2010批量激活

2020-11-13 05:49:09

物联网城域网IOT

2015-08-17 14:13:52

Ansible轻量自动化部署工具

2024-01-10 17:24:00

2023-12-19 09:36:35

PostgreSQL数据库开源

2020-05-11 09:54:33

JavaScript开发技术

2022-10-28 18:36:18

2011-03-08 09:22:37

2019-03-08 11:29:05

开源自动化工具Ansible

2011-05-18 15:15:44

MySQL

2010-08-03 15:19:08

FlexBuilder

2021-03-26 10:31:19

人工智能AIOps

2015-10-29 15:36:19

Redis入门
点赞
收藏

51CTO技术栈公众号