Ansible 常用模块学习

2019-01-07  本文已影响0人  打不shi的流云

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

#!/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

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的内部执行或执行状态:

尴尬的是,目前没发现能够仅影响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

上一篇 下一篇

猜你喜欢

热点阅读