Ansible自动化运维(三)----Ansible模式与命令
Ansible实现批量化主机管理的模式,主要有两种,
利用ansible的纯命令行实现的批量管理,ad-hoc模式-------好比简单的shell命令管理;
利用ansible的playbook剧本来实现批量管理,playbook剧本模式-----好比复杂的shell脚本管理。
ad-hoc模式
Ansible的ad-hoc模式是ansible的命令行形式,也就是处理一些临时的,简单的任务,可以直接使用ansible的命令行来操作,
- 比如
- 临时批量查看被管理机器的内存情况,cpu负载情况,网络情况
- 比如临时分发配置文件等等
playbook模式
Ansible的playbook模式是针对比较具体,且比较大的任务,那么你就得实现写好剧本,应用场景,
- 一键部署rsync备份服务器
- 一键部署lnmp环境
Ansible的ad-hoc命令行解析
Ansible-01 | 管理机器-----老板 |
Ansible-02 | 被管理机器-----员工 |
# 让被管理机器返回主机名
[root@Ansible-01 .ssh]# ansible shenfu -m command -a "hostname"
123.57.235.127 | CHANGED | rc=0 >>
Ansible-02
# ad-hoc命令解释
ansible ---- 自带提供的命令操作
shenfu ----/etc/ansible/hosts文件中定义的主机组,还可以写主机ip地址,以及通配符*
-m command ansible的指定模块的参数,以及指定了command模块
-a 指定给command模块什么参数,如 hostname, uname -r
ansible-doc命令
# 列出所有的ansible支持的模块
[root@Ansible-01 .ssh]# ansible-doc -l
# 查看某个模块的具体用法参数
[root@Ansible-01 .ssh]# ansible-doc -s command
- name: Execute commands on targets
command:
argv: # Passes the command as a list rather than a string. Use `argv' to avoid quoting values that would otherwise be interpreted incorrectly (for
# example "user name"). Only the string (free form) or the list (argv) form can be provided, not both. One or
# the other must be provided.
chdir: # Change into this directory before running the command.
cmd: # The command to run.
creates: # A filename or (since 2.0) glob pattern. If a matching file already exists, this step *will not* be run. This is checked before `removes' is
# checked.
free_form: # The command module takes a free form string as a command to run. There is no actual parameter named 'free form'.
removes: # A filename or (since 2.0) glob pattern. If a matching file exists, this step *will* be run. This is checked after `creates' is checked.
stdin: # Set the stdin of the command directly to the specified value.
stdin_add_newline: # If set to `yes', append a newline to stdin data.
strip_empty_ends: # Strip empty lines from the end of stdout/stderr in result.
warn: # (deprecated) Enable or disable task warnings. This feature is deprecated and will be removed in 2.14. As of version 2.11, this option is now
# disabled by default
----根据于超老师视频讲解进行笔记,做了相对更改,如有侵权请联系作者删除。