Ansible 运行临时命令详解

使用 ansible 命令运行临时命令

ansible <host-pattern> -m <module> [-a '<module arguments>'] [-i <inventory>]
参数说明
  1. host-pattern:指定应在其上运行临时命令的受管主机。它可以是清单中的特定受管主机或主机组。例如,可以使用 --list-hosts 选项结合主机组来显示通过特定主机模式匹配的主机列表。
  2. -m 选项:指定在目标主机上运行的模块的名称。模块是为了实现特定任务而执行的小程序。
  3. -a 选项:传递给模块的参数,可以带引号来传递这些参数的列表。
  4. -i 选项:指定其他清单位置,取代当前 Ansible 配置文件中的默认位置。
示例

一个最简单的临时命令是使用 ping 模块。此模块不执行 ICMP ping,而是检查能否在受管主机上运行基于 Python 的模块。

ansible all -m ping
servera.lab.example.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

Ansible 模块详解

常用 Ansible 模块分类

  1. 文件模块

    • copy:将本地文件复制到受管主机。
    • file:设置文件的权限和其他属性。
    • lineinfile:确保特定行是否存在于文件中。
    • synchronize:使用 rsync 同步内容。
  2. 软件包模块

    • package:使用操作系统的包管理器自动检测包管理器。
    • yum:使用 YUM 软件包管理器管理软件包。
    • apt:使用 APT 软件包管理器管理软件包。
    • dnf:使用 DNF 软件包管理器管理软件包。
    • gem:管理 Ruby gem。
    • pip:从 PyPI 管理 Python 软件包。
  3. 系统模块

    • firewalld:使用 firewalld 管理任意端口和服务。
    • reboot:重新启动计算机。
    • service:管理服务。
    • user:添加、删除和管理用户帐户。
  4. 网络工具模块

    • get_url:通过 HTTP、HTTPS 或 FTP 下载文件。
    • nmcli:管理网络。
    • uri:与 Web 服务交互。

常用模块示例

文件模块

blockinfile 模块

插入、更新或删除由标记包围的多行文本块。

- name: Insert/Update/Delete text block
  blockinfile:
    path: /etc/httpd/conf/httpd.conf
    block: |
      <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
      </Directory>
copy 模块

将文件从本地或远程主机复制到目标主机。

- name: Copy file with owner and permissions
  copy:
    src: /srv/myfile
    dest: /etc/myfile
    owner: foo
    group: foo
    mode: '0644'
fetch 模块

从远程主机获取文件并将其存储在本地主机上。

- name: Fetch file from remote host
  fetch:
    src: /tmp/file.conf
    dest: /local/path/file.conf
    flat: yes
file 模块

设置文件的权限、所有权和其他属性。

- name: Set permissions on /etc/foo.conf
  file:
    path: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'
lineinfile 模块

确保特定行是否存在于文件中,如果不存在则插入该行。

- name: Ensure a line in file
  lineinfile:
    path: /etc/hosts
    line: '127.0.0.1 localhost'
synchronize 模块

使用 rsync 命令同步文件和目录。

- name: Synchronize directories
  synchronize:
    src: /src/dir/
    dest: /dest/dir/
    delete: yes
    recursive: yes

软件包模块

yum 模块

使用 YUM 管理软件包。

- name: Ensure latest version of a package is installed
  yum:
    name: httpd
    state: latest
apt 模块

使用 APT 管理软件包。

- name: Ensure a package is installed
  apt:
    name: apache2
    state: present
dnf 模块

使用 DNF 管理软件包。

- name: Ensure a package is installed using DNF
  dnf:
    name: nginx
    state: present
gem 模块

管理 Ruby gem。

- name: Install a gem
  gem:
    name: bundler
    state: present
pip 模块

从 PyPI 管理 Python 软件包。

- name: Install a Python package
  pip:
    name: django
    state: present

系统模块

firewalld 模块

管理 firewalld 服务。

- name: Add a service to the public zone
  firewalld:
    service: http
    zone: public
    permanent: yes
    state: enabled
reboot 模块

重启计算机。

- name: Reboot the machine
  reboot:
    msg: "Reboot initiated by Ansible"
    connect_timeout: 5
    reboot_timeout: 600
service 模块

管理系统服务。

- name: Ensure a service is running
  service:
    name: httpd
    state: started
user 模块

管理用户帐户。

- name: Ensure a user is present
  user:
    name: jdoe
    state: present
    groups: wheel
    append: yes

网络工具模块

get_url 模块

通过 HTTP、HTTPS 或 FTP 下载文件。

- name: Download a file
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/file.conf
nmcli 模块

管理网络连接。

- name: Ensure a connection profile is present
  nmcli:
    conn_name: eth0
    ifname: eth0
    type: ethernet
    ip4: 192.168.1.100/24
    gw4: 192.168.1.1
    dns4: 8.8.8.8
uri 模块

与 Web 服务交互。

- name: Interact with a web service
  uri:
    url: http://example.com/api/
    method: POST
    body: '{ "key": "value" }'
    body_format: json
    headers:
      Content-Type: "application/json"

Ansible 模块示例

  1. 使用 command 模块运行简单命令

    ansible web -m command -a 'date'
  2. 使用 service 模块管理服务

    启动 httpd 服务,并将其设置为随机器启动:

    ansible web -m service -a 'enabled=true name=httpd state=started'
  3. 使用 shell 模块运行复杂命令

    用到管道复杂命令功能时建议用 shell

    ansible web -m shell -a 'echo 123 | passwd --stdin user1'
  4. 使用 yum 模块安装软件包

    安装软件包:

    ansible web -m yum -a 'name=vsftpd state=present'
  5. 使用 file 模块设置文件属性

    修改文件所属的用户、组、权限:

    ansible web -m file -a 'owner=test group=test mode=644 path=/tmp/1.file'
  6. 使用 copy 模块进行文件复制

    利用 copy 模块进行文件的生成和拷贝:

    ansible web -m copy -a 'content=12121212 dest=/tmp/1 owner=root mode=660'
    ansible web -m copy -a 'src=/tmp/1 dest=/tmp/
  7. remote_src=yes owner=root mode=660'

  8. 使用 user 模块管理用户

    创建新用户:

    ansible web -m user -a 'name="user1"'
    ansible web -m command -a 'id user1'

    可以通过状态 state=presentabsent 来进行用户的创建和删除。

  9. 使用 cron 模块建立周期性任务

    ansible web -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"'

详细举例

lineinfile 模块

  1. 确保指定的一行文本存在于文件中,如果指定的文本本来就存在于文件中,则不做任何操作,如果不存在,默认在文件的末尾插入这行文本。
  2. 如果不止一行能够匹配正则,那么只有最后一个匹配正则的行才会替换,被匹配行会被替换成 line 参数指定的内容。

例如,受管机器上有个文件 /tmp/1.file,内容如下:

123
124
456
789

增加一行内容:

ansible web -m lineinfile -a 'path=/tmp/1.file line="0000"'

按正则来匹配查找:

ansible web -m lineinfile -a 'path=/tmp/1.file regexp="^12" line="#12"'

删除指定行:

ansible web -m lineinfile -a 'path=/tmp/1.file regexp="^000" state=absent'

synchronize 模块

使用 rsync 进行文件同步:

  • 将主控机上的 /tmp/1 文件传输到(push 推)web 组机器下的 /tmp 目录,默认是 push:

    ansible web -m synchronize -a 'src=/tmp/1 dest=/tmp'
  • 拉的操作,这个时候,src 是在 web 组的机器上,dest 是在主控机上:

    ansible web -m synchronize -a 'mode=pull src=/tmp/1 dest=/usr/local/src'
  • 将本地 /tmp 目录内 .txt 结尾的数据无差异且保持属性的同步到 /mnt 目录无差异化:

    ansible web -m synchronize -a 'src=/tmp dest=/mnt archive=yes delete=yes rsync_opts=--exclude=*.txt'

本文由mdnice多平台发布


逼格高的汤圆
10 声望2 粉丝