您需要了解的 10 个 Ansible 模块

查看示例并学习使用 Ansible 自动化日常任务的最重要模块。
489 位读者喜欢这篇文章。
Text editor on a browser, in blue

Ansible 是一个开源的 IT 配置管理和自动化平台。它使用人类可读的 YAML 模板,以便用户可以对重复性任务进行编程以自动执行,而无需学习高级编程语言。

Ansible 是无代理的,这意味着它管理的节点不需要在其上安装任何软件。这消除了潜在的安全漏洞,并使整体管理更加顺畅。

Ansible 模块是可以 Ansible 剧本中使用的独立脚本。剧本由一个剧集组成,而一个剧集又由多个任务组成。如果您是 Ansible 的新手,这些概念可能看起来令人困惑,但是当您开始编写和使用更多剧本时,它们将变得熟悉。

 

有些模块经常用于自动化日常任务;这些是我们在本文中将要介绍的模块。

 

Ansible 有三个主要文件需要您考虑

  • 主机/清单文件:包含需要管理的节点的条目
  • Ansible.cfg 文件:默认情况下位于 /etc/ansible/ansible.cfg,它具有必要的权限提升选项和清单文件的位置
  • 主文件:一个剧本,其中包含在清单或主机文件中列出的主机上执行各种任务的模块

模块 1:软件包管理

大多数流行的软件包管理器(例如 DNF 和 APT)都有一个模块,使您能够在系统上安装任何软件包。功能完全取决于软件包管理器,但通常这些模块可以安装、升级、降级、删除和列出软件包。相关模块的名称很容易猜到。例如,DNF 模块是 dnf_module,旧的 YUM 模块(Python 2 兼容性需要)是 yum_module,而 APT 模块是 apt_module,Slackpkg 模块是 slackpkg_module,等等。

示例 1

- name: install the latest version of Apache and MariaDB
  dnf:
    name:
      - httpd
      - mariadb-server
    state: latest

这会安装 Apache Web 服务器和 MariaDB SQL 数据库。

示例 2

- name: Install a list of packages
  yum:
    name:
      - nginx
      - postgresql
      - postgresql-server
    state: present

这会安装软件包列表并帮助下载多个软件包。

[ 提升您的自动化专业知识。获取 Ansible 清单: 迁移到 Red Hat Ansible Automation Platform 2 的 5 个理由 ]

模块 2:服务

安装软件包后,您需要一个模块来启动它。service 模块使您能够启动、停止和重新加载已安装的软件包;这非常方便。

示例 1

- name: Start service foo, based on running process /usr/bin/foo
  service:
    name: foo
    pattern: /usr/bin/foo
    state: started

这将启动服务 foo

示例 2

- name: Restart network service for interface eth0
  service:
    name: network
    state: restarted
    args: eth0

这将重新启动接口 eth0 的网络服务。

模块 3:复制

copy 模块将文件从本地或远程计算机复制到远程计算机上的某个位置。

示例 1

- name: Copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
  copy:
    src: /mine/ntp.conf
    dest: /etc/ntp.conf
    owner: root
    group: root
    mode: '0644'
    backup: yes

示例 2

- name: Copy file with owner and permission, using symbolic representation
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u=rw,g=r,o=r

模块 4:调试

debug 模块在执行期间打印语句,可用于调试变量或表达式,而无需停止剧本。

示例 1

- name: Display all variables/facts known for a host
  debug:
    var: hostvars[inventory_hostname]
    verbosity: 4

这会显示清单文件中定义的主机的所有变量信息。

示例 2

- name: Write some content in a file /tmp/foo.txt
  copy:
    dest: /tmp/foo.txt
    content: |
      Good Morning!
      Awesome sunshine today.
    register: display_file_content
- name: Debug display_file_content
    debug:
      var: display_file_content
      verbosity: 2

这会注册 copy 模块输出的内容,并且仅当您将详细程度指定为 2 时才显示它。例如

ansible-playbook demo.yaml -vv

模块 5:文件

file 模块管理文件及其属性。

  • 它设置文件、符号链接或目录的属性。
  • 它还可以删除文件、符号链接或目录。

示例 1

- name: Change file ownership, group and permissions
  file:
    path: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'

这会创建一个名为 foo.conf 的文件,并将权限设置为 0644

示例 2

- name: Create a directory if it does not exist
  file:
    path: /etc/some_directory
    state: directory
    mode: '0755'

这会创建一个名为 some_directory 的目录,并将权限设置为 0755

模块 6:Lineinfile

lineinfile 模块管理文本文件中的行。

  • 它确保文件中存在特定行,或者使用反向引用的正则表达式替换现有行。
  • 当您只想更改文件中的单行时,它非常有用。

示例 1

- name: Ensure SELinux is set to enforcing mode
  lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=enforcing

这会将 SELINUX=enforcing 的值设置为启用强制模式。

示例 2

- name: Add a line to a file if the file does not exist, without passing regexp
  lineinfile:
    path: /etc/resolv.conf
    line: 192.168.1.99 foo.lab.net foo
    create: yes

这会在 resolv.conf 文件中为 IP 和主机名添加一个条目。

模块 7:Git

git 模块管理存储库的 git 检出,以部署文件或软件。

示例 1

# Example Create git archive from repo
- git:
    repo: https://github.com/ansible/ansible-examples.git
    dest: /src/ansible-examples
    archive: /tmp/ansible-examples.zip

示例 2

- git:
    repo: https://github.com/ansible/ansible-examples.git
    dest: /src/ansible-examples
    separate_git_dir: /src/ansible-examples.git

这将克隆一个带有单独 Git 目录的存储库。

模块 8:Cli_command

cli_command 模块,首次在 Ansible 2.7 中提供,提供了一种平台无关的方式,通过 network_cli 连接插件将基于文本的配置推送到网络设备。

示例 1

- name: commit with comment
  cli_config:
    config: set system host-name foo
    commit_comment: this is a test

这将设置交换机的主机名并使用提交消息退出。

示例 2

- name: configurable backup path
  cli_config:
    config: "{{ lookup('template', 'basic/config.j2') }}"
    backup: yes
    backup_options:
      filename: backup.cfg
      dir_path: /home/user

这会将配置备份到不同的目标文件。

模块 9:Archive

archive 模块创建一个或多个文件的压缩存档。默认情况下,它假定压缩源存在于目标上。

示例 1

- name: Compress directory /path/to/foo/ into /path/to/foo.tgz
  archive:
    path: /path/to/foo
    dest: /path/to/foo.tgz

示例 2

- name: Create a bz2 archive of multiple files, rooted at /path
  archive:
    path:
    - /path/to/foo
    - /path/wong/foo
    dest: /path/file.tar.bz2
    format: bz2

模块 10:Command

作为最基本但最有用的模块之一,command 模块接受命令名称,后跟以空格分隔的参数列表。

示例 1

- name: return motd to registered var
  command: cat /etc/motd
  register: mymotd

示例 2

- name: Change the working directory to somedir/ and run the command as db_owner if /path/to/database does not exist.
  command: /usr/bin/make_database.sh db_user db_name
  become: yes
  become_user: db_owner
  args:
    chdir: somedir/
    creates: /path/to/database

结论

Ansible 中有大量模块可用,但这十个模块是您可以用于自动化作业的最基本和最强大的模块。随着您的需求变化,您可以通过在命令行输入 ansible-doc <模块名称> 或参考 官方文档 来了解其他有用的模块。

接下来要阅读的内容
标签
Avatar
我作为 Red Hat 的解决方案工程师工作,我的日常工作涉及使用 OpenShift 和 Ansible。我对开源、云、安全和网络技术充满热情。

6 条评论

感谢您对这些 Ansible 模块的总结。这是一个很好的列表。

嗨!Ansible 上游贡献者在此,我是 yum 和 dnf 模块的维护者之一。

首先,写得真棒!

其次,从 Ansible 2.8 开始,您现在可以将 yum 模块用于 yum 或 dnf(有一个与其关联的操作插件来处理魔术)。只是想提一下,祝您自动化愉快!

为什么您要使用特定的软件包管理模块而不是 package 模块?package 模块适用于 95% 的所有用例。我还想说,我使用 template 模块比 file 模块更多。我也只会将 command 模块作为最后的手段。

+1 支持 package 模块。它在 APT 和 RPM 发行版之间不是那么互操作,但它可以更轻松地为 CentOS/Fedora/SUSE 主机编写相同的任务一次。

回复 作者:Ricky Latupeirissa (未验证)

感谢这篇入门文章。但是,我认为模块 6 的示例 2 应该将 /etc/resolv.conf 替换为 /etc/hosts

知识共享许可协议本作品根据知识共享署名-相同方式共享 4.0 国际许可协议获得许可。
© . All rights reserved.