ansible笔记(三)--模块讲解

ansible 常用命令

ansible-doc
ansible-playbook
ansible-vault
ansible-console
ansible-galaxy
ansible-pull

ansible-doc,显示模块帮助

  ansible-doc [options] [module...]
  -a        显示所有模块文档
  -l,--list     列出可用模块
  -s,--snippet    显示指定模块的playbook片段

示例:

  ansible-doc -l    列出所有模块
  ansible-doc ping     查看指定模块帮助用法
  ansible-doc -s ping  查看指定模块帮助用法

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

  • 相同点:

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

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

2. 文件操作模块

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

 

原文地址:https://www.cnblogs.com/lizhewei/p/11685449.html