Skip to content

1. ansible-doc命令

1.1 查看所有模块

bash
ansible-doc -l
ansible-doc -l

1.2 查看个例模块

bash
ansible-doc -s copy
ansible-doc -s copy

Ad-Hoc 执行方式,可以通过shell或者command模块来执行命令。一条条来执行

  • -m 模块名称
  • -a 模块参数
bash
ansible-doc command

# 查看某服务器的内存使用情况
ansible myserver -m command -a "free -m"

# 可简写, 因为 command 是默认模块
ansible myserver -a "free -m"

# 模块包括 command, script(在远程主机执行主控端的shell脚本), shell (执行远程主机的shell脚本文件)
ansible myserver -m command -a "cat /etc/os-release"

# 先切换到目录再执行
ansible myserver -m command -a "chdir=/etc cat os-release"

# 用 command 模块执行不成功, shell 模块可以
ansible cloud -m command -a "sudo rm -rf /var/log/mysql/*.gz"
ansible cloud -m shell -a "sudo rm -rf /var/log/mysql/*.gz"
ansible myserver -m script -a "/home/local.sh"
ansible myserver -m shell -a "/home/server.sh"

# 实际上shell模块执行命令的方式是在远程使用/bin/sh来执行的
ansible merch -m shell -a "touch demo.txt"
ansible-doc command

# 查看某服务器的内存使用情况
ansible myserver -m command -a "free -m"

# 可简写, 因为 command 是默认模块
ansible myserver -a "free -m"

# 模块包括 command, script(在远程主机执行主控端的shell脚本), shell (执行远程主机的shell脚本文件)
ansible myserver -m command -a "cat /etc/os-release"

# 先切换到目录再执行
ansible myserver -m command -a "chdir=/etc cat os-release"

# 用 command 模块执行不成功, shell 模块可以
ansible cloud -m command -a "sudo rm -rf /var/log/mysql/*.gz"
ansible cloud -m shell -a "sudo rm -rf /var/log/mysql/*.gz"
ansible myserver -m script -a "/home/local.sh"
ansible myserver -m shell -a "/home/server.sh"

# 实际上shell模块执行命令的方式是在远程使用/bin/sh来执行的
ansible merch -m shell -a "touch demo.txt"