美文网首页
Ansible 常用模块学习

Ansible 常用模块学习

作者: 打不shi的流云 | 来源:发表于2019-01-07 22:42 被阅读0次

1. 远端执行模块:command vs shell

  • 相同点:

    • 功能相似,都是在远端运行shell命令
    • 如果要在 Windows 环境运行,需要使用对应 win_commandwin_shell 模块
  • 不同点:

    • shell 将命令打包,通过 /bin/sh 的远程模式运行
    • command 解析命令参数,然后在远端执行,因此无法使用 管道("|") 定向符 (">" "<") 以及 ";" 和 "&"
  • 用例:使用 commandshell 执行同一语句 cat /home/root/testfile

#!/usr/local/bin/ansible-playbook
---
- hosts: all
  tasks:
    - name: 1.1 command
      command: cat /home/root/testfile
      register: cmd_out
      
    - name: 1.2 see command output
      debug: var=cmd_out

    - name: 2.1 shell
      shell: cat /home/root/testfile
      register: shell_out

    - name: 2.2 see shell output
      debug: var=shell_out

观察输出:

# command 输出
{
    'stderr_lines': [], 
    u'changed': True, 
    u'end': u'2018-12-24 00:06:51.393299', 
    'failed': False, 
    u'stdout': u'feng baobao is like immortal', 
    # ========== ↓ Attention ↓ ==========
    u'cmd': [u'cat', u'/home/root/testfile'], 
    # ========== ↑ Attention ↑ ==========
    u'rc': 0, 
    u'start': 
    u'2018-12-24 00:06:51.283211', u'stderr': 
    u'', 
    u'delta': 
    u'0:00:00.110088', 'stdout_lines': [u'feng baobao is like immortal']
}
# shell 输出
{
    'stderr_lines': [], 
    u'changed': True, 
    u'end': u'2018-12-24 00:06:58.119163', 
    'failed': False, 
    u'stdout': u'feng baobao is like immortal', 
    # ========== ↓ Attention ↓ ==========
    u'cmd': u'cat /home/root/testfile', 
    # ========== ↑ Attention ↑ ==========
    u'rc': 0, 
    u'start': u'2018-12-24 00:06:58.007352', 
    u'stderr': u'', 
    u'delta': u'0:00:00.111811', 'stdout_lines': [u'feng baobao is like immortal']}"

几无区别,除了 cmd 项中,command输出是一个list,每个元素为命令的split;而shell输出是将命令作为一个string传递给远端shell

  • 思考: 由此看来,shell 理当比 command 模块更强大,那么 command 模块单独存在的意义是什么?
  • 还有“同类”: script & raw
    • script: 将本地脚本传递到远端,然后在远端机器执行该脚本
    • raw:
      • 类似 command 但是可以使用管道(网上的说法,但貌似不全面)
      • 官方解释,该模块可以运行 "low-down and dirty SSH command",并不需要依赖python和ansible模块功能
      • 限制: 建议只在少数情况下使用,如对远端进行安装python操作
    • 此二者不需要远端安装 python

References:

2. 文件操作模块

模块 功能
file 文件/文件夹/链接 进行 创建/删除/修改权限 操作
copy 将 本地或远端文件 拷贝到 远端路径
template copy 升级版,可以对按 jinja2 规则的模板文件进行构建,并拷贝到远端目录中
assemble 将一个路径中的所有文件按照字符串顺序聚合起来

file 模块示例:

    tasks:
      - name: 创建
        file:
          path: /home/root/testfile
          state: touch # 创建文件需要用 touch, 创建文件夹用 directory, 删除用 absent
      
      - name: 链接
        file:
          src: /home/root/filetolink
          dest: /home/root/testlink
          state: link
    
      - name: 修改权限属性等
        file: 
          path: /home/root/testfile
          owner: foo
          group: foo
          mode: 0644

copy 模块示例:

    tasks:
      - name: 将本地 /srv/myfiles/foo.conf 拷贝到远端 /etc/foo.conf
        copy:
          src: /srv/myfiles/foo.conf
          dest: /etc/foo.conf
          owner: foo
          group: foo
          mode: 0644
      
      - name: 将远端 /srv/myfiles/foo.conf 拷贝到远端 /etc/foo.conf
        copy:
          src: /srv/myfiles/foo.conf
          dest: /etc/foo.conf
          owner: foo
          group: foo
          remote_src: yes
          mode: 0644

References:

文件拉取 fetch vs slurp

copytemplate 将本地文件传送到远端不同, fetch 命令从远端将文件拉取到本地。

slurp 模块用于拉取远端文件的 base64 码。
fetch 示例:

# Store file into /tmp/fetched/host.example.com/tmp/somefile
- fetch:
    src: /tmp/somefile
    dest: /tmp/fetched
    
# Specifying a destination path
- fetch:
    src: /tmp/uniquefile
    dest: /tmp/special/
    flat: yes # 不覆盖已有文件

References:

4. How to EXIT playbook: fail & meta: end_play

fail 模块
可用于标记任务失败,仅影响当前 inventory_hostname, 其他节点仍可继续进行后续步骤。

meta 模块
该模块可执行一些特殊命令,这些命令能影响playbook的内部执行或执行状态:

  • flush_handler:刷新 handler
  • refresh_inventory:某些动态加载 host 需要在此刷新
  • noop:无用
  • clear_facts:清除当前 inventory 收集的facts
  • clear_host_errors:清除 inventory 的 fail 状态
  • end_play:结束整个 playbook
  • reset_connection:重新设置链接 (从官方用例来看,可以更改远程登录的用户)

尴尬的是,目前没发现能够仅影响1台主机令其结束任务,其他主机依旧进行任务的方式。或许可以尝试 meta 模块的 refresh_inventory

5. 配置文件相关 ini_file vs with_ini

ini_file 模块

用于管理远端配置文件。可以 增、删、改 远端配置文件的配置项。

远端配置文件需满足一定格式,如

[section1]
option1=value1
option2=value2

[section2]
option1=value2
option2=value1

section 不可重复;同一 section 下的 option 亦不可重复。

模块使用方式:

- name: 修改远端 /etc/conf 文件,使其 [drinks] 下的 fav 值为 lemonade (如果不存在option或section,则加上这个配置项),且权限模式为 0600,并备份原始文件
  ini_file:
    path: /etc/conf
    section: drinks
    option: fav
    value: lemonade
    mode: 0600
    backup: yes

- name: 删除远端 /etc/anotherconf 文件中 [drinks] 下的 temperature 配置项
  ini_file:
    path: /etc/anotherconf
    section: drinks
    option: temperature
    state: absent

with_ini

其实是 lookup 的子模块,用于读取本地的文件。 with_ini 可以读取本地配置文件。示例如下:

- debug: msg="User in integration is {{ lookup('ini', 'user section=integration file=users.ini') }}"

- debug: msg="User in production  is {{ lookup('ini', 'user section=production  file=users.ini') }}"

- debug: msg="user.name is {{ lookup('ini', 'user.name type=properties file=user.properties') }}"

- debug:
    msg: "{{ item }}"
  with_ini:
    - value[1-2]
    - section: section1
    - file: "lookup.ini"
    - re: true

目前没发现读取远端配置文件的模块。解决方法都是 fetch 目标配置文件到本地来解析。

References:

AnsibleDoc-Lookup-ini

相关文章

网友评论

      本文标题:Ansible 常用模块学习

      本文链接:https://www.haomeiwen.com/subject/ghcmrqtx.html