1.ansible实现管理的方式
Ad-Hoc ##利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook ##ansible脚本,主要用于大型项目场景,需要前期的规划
2.Ad-Hoc执行方式中如何获得帮助
ansible-doc ##显示模块帮助的指令
#格式
ansible-doc [参数] [模块…]
#常用参数
-l ##列出可用模块
-s ##显示指定模块的playbook片段
ansible-doc shell 对指定模块使用使用方法进行查看
ansible-doc -s shell 显示模块简要用法
3.ansible命令运行方式及常用参数
#格式:
ansible 清单 -m 模块 -a 模块参数
#常用参数
#–version ##显示版本
#-m module ##指定模块,默认为command模块
#-v ##详细过程 -vv -vvv更详细过程
#–list ##显示主机列表,也可以用–list-hosts
#-k ##提示输入ssh连接密码,默认key认证
#-C ##预执行检测
#-T ##执行命令的超时时间,默认10s
#-u ##指定远程执行的用户
#-b ##执行sudo切换身份操作
#–become ## 在远程主机中执行命令时会用sudo调用
#-become-user=USERNAME ##指定sudo的用户
#-K ##执行ansible时询问sudo执行密码
4.ansible的基本颜色代表含义
绿色 ##执行成功但为对远程主机做任何改变
黄色 ##执行成功并对远程主机做改变
红色 ##执行失败
5.ansible中的常用模块##
(1)command
#功能: 在远程主机执行命令,此模块为默认模块
ansible-doc -s command 显示command模块的简要用法
#常用参数
chdir ##执行命令前先进入到指定目录
cmd ##运行命令指定
creates ##如果文件存在将不运行
removes ##如果文件存在在将运行
free_form ##在远程主机中执行的命令,此参数不需要加
#实例
ansible all -m command -a “useradd lee”
ansible all -m command -a “userdel -r lee”
建立lee用户失败,因为两台主机已经存在
删除一个成功一个失败,因为ansible主机正在控制用户为lee,所以不能删除
ansible all -m command -a "chdir=/mnt touch file1 file2" #chair提前切换环境
removes文件存在就运行
ansible westos -m command -a 'removes=/mnt/file1 chdir=/mnt rm -fr file1 file2'
如果/mnt/file1存在,切换环境到/mnt下删除file1 file2
creates文件存在不运行,即不存在就运行
ansible westos -m command -a 'creates=/mnt/file1 chdir=/mnt touch file1 file2'
如果/mnt/file1不存在,执行切换环境到/mnt 创建file1 file2 文件
##注意##
#Linux中的很多通配符在command模块中不支持
(2)shell
ansible -doc -s shell 显示shell模块简要参数
#功能:
和command功能类似,指令中可以使用通配符
#常用参数
chdir ##执行命令前先进入到指定目录
cmd ##运行命令指定
creates ##如果文件存在将不运行
removes ##如果文件存在在将运行
free_form ##在远程主机中执行的命令,此参数不需要加
executable ##指定执行命令的shell环境,默认为sh
#实例
ansible westos -m shell -a "executable=sh ps ax | grep $$ " $$为当前进程id
executable指定shell环境为sh,并查看当前进程id
(3)script
#功能:
在ansible主机中写好的脚本在受控主机中执行
#实例
vim /home/lee/ansible/westos.sh
#!/bin/bash
hostname
ansible westos -m script -a “/home/lee/ansible/westos.sh”
(4)copy
#功能
从ansible主机复制文件到受控主机
#常用参数
src ##源文件
dest ##目的地文件
owner