目录
一、Ansible实现管理的方式
二、Ad-Hoc执行方式中如何获得帮助
三、Ansible命令运行方式及常用参数
四、Ansible的基本颜色代表信
五、Ansible中的常用模块
1、command模块
2、shell模块、script模块
3、copy模块、fetch模块
4、file模块
5、archive模块、unarchive模块
6、hostname模块、cron模块
一、Ansible实现管理的方式
Ad-Hoc 利用ansible命令直接完成管理,主要用于临时命令使用场景
 playbook ansible脚本,主要用于大型项目场景,需要前期的规划
二、Ad-Hoc执行方式中如何获得帮助
ansible-doc 显示模块帮助的指令
格式
ansible-doc [参数] [模块...]常用参数
-l   列出可用模块
-s   显示指定模块的playbook片段vim test.yml
cat test.yml
ansible-playbook test.ymlansible-doc
ansible-doc -l | wc -l
ansible-doc -l | grrep shell 
三、Ansible命令运行方式及常用参数
| 参数 | 功能 | 
|---|---|
| -i | 指定hosts文件路径,默认在 /etc/ansible/hosts | 
| -m | 指定使用的module名称,默认command模块 | 
| -a | 指定模块参数 | 
| -k(小写) | 提示输入ssh密码,并非基于ssh密钥认证 | 
| -K(大写) | 提示输入sudo密码 | 
| -b | 使用sudo执行命令 | 
| -become-user= | 指定sudo的用户 | 
| -f,-forks=NUM | NUM默认是整数5,指定fork开启同步进程的个数 | 
| -u | 指定远程主机的执行用户 | 
| -v | 详细模式,如果执行成功,输出详细结果, -vv -vvv更详细过程 | 
| -C | 预执行检测 | 
| -T | 执行命令的超时时间,默认10s | 
| --list | 显示主机列表,也可以用 --list-hosts | 
| --version | 显示版本 | 
ansible --version
ansible all -m shell -a 'whoami' -C
ansible all -m shell -a 'whoami' -u test
ansible all -m shell -a 'whoami' -utest
ansible all -m shell -a 'whoami'
ansible all -m shell -a 'whoami' -b -become-user=yyl
ansible all -m shell -a 'whoami' -b -become-user=yyl -K 
 
 
四、Ansible的基本颜色代表信
| 绿色 | 执行成功但未对远程主机做任何改变 | 
| 黄色 | 执行成功并对远程主机做改变 | 
| 红色 | 执行失败 | 
五、Ansible中的常用模块
1、command模块
在远程主机执行命令,此模块为默认模块
| 常用参数 | 功能 | 
|---|---|
| chdir | 执行命令前先进入到指定目录 | 
| cmd | 运行命令指定 | 
| creates | 如果文件存在将不运行 | 
| removes | 如果文件存在将运行 | 
ansible all -m command -a 'chdir=/mnt pwd'
ansible all -m command -a 'chdir=/mnt cmd=pwd'
ansible all -m command -a 'chdir=/mnt  creates=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt  removes=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt  removes=/mnt/file cat passwd'ansible all -m command -a 'chdir=/mnt touch file'
ansible all -m command -a 'chdir=/mnt  creates=/mnt/file pwd'
ansible all -m command -a 'chdir=/mnt  removes=/mnt/file pwd'
注意
 Linux中的很多通配符在command模块中不支持
ansible all -m command -a "chdir=/mnt touch file{1..10}"
 
 
2、shell模块、script模块
(1)shell模块
 shell模块与command模块类似
| 常用参数 | 功能 | 
|---|---|
| chdir | 执行命令前先进入到指定目录 | 
| cmd | 运行命令指定 | 
| creates | 如果文件存在将不运行 | 
| removes | 如果文件存在将运行 | 
| executable | 指定执行环境,默认为sh | 
ansible all -m shell -a 'chdir=/mnt/ touch file{1..3}'
ansible all -m shell -a 'chdir=/mnt/ ls -ld /mnt'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
ansible all -m shell -a 'ps ax | grep $$'
ansible all -m shell -a 'executable=/bin/bash ps ax | grep $$'
(2)script模块
 在ansible主机中写好的脚本在受控主机中执行
vim test.sh
cat test.sh
ansible all -m script -a "test.sh"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
3、copy模块、fetch模块
(1)copy模块
 从ansible主机复制文件到受控主机
| 参数 | 功能 | 
|---|---|
| src | 源文件 | 
| dest | 目的地文件 | 
| owner | 指定目的地文件所有人 | 
| group | 指定目的地文件所有组 | 
| mode | 指定目的地文件权限 | 
| backup=yes | 当受控主机中存在文件时备份原文件 | 
| content | 指定文本内容直接在受控主机中生成文件 | 
复制当前目录的test.sh到受控主机的/mnt下,文件所有人为yyl,权限为755 
ansible all -m copy -a 'src=test.sh dest=/mnt/ owner=yyl mode=755'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
在ansible主机上修改文件内容,开启备份,再次发送 先修改一下文件
ansible all -m copy -a 'src=test.sh dest=/mnt/ owner=yyl mode=755 backup=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'
 直接输入文件内容,在受控主机上生成文件
 直接输入文件内容,在受控主机上生成文件
ansible all -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/test.sh owner=yyl mode=755"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
(2)fetch模块
 从受控主机把文件复制到ansible主机,但不支持目录
| 参数 | 功能 | 
|---|---|
| src | 受控主机的源文件 | 
| dest | 本机目录 | 
| flat | 基本名称功能,单纯只要文件,不要路径的层层目录 | 
ansible all -m fetch -a "src=/mnt/test.sh dest=/home/ale/.ansible"
ansible all -m fetch -a "src=/mnt/test.sh dest=/home/ale/.ansible/ flat=yes"

4、file模块
设置文件的属性
| 参数 | 功能 | 
|---|---|
| path | 指定文件名称 | 
| state | 指定操作状态 touch:建立absent:删除directory:递归目录link:建立软链接hard:建立硬链接 | 
| mode | 设定权限 | 
| owner | 设定文件用户 | 
| group | 设定文件组 | 
| src | 源文件 | 
| dest | 目标文件 | 
| recurse=yes | 递归更改 | 
ansible all -m shell -a 'rm -fr /mnt/*'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
创建test.sh文件
ansible all -m file -a 'path=/mnt/test.sh state=touch'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
删除
ansible all -m file -a 'path=/mnt/test.sh state=absent'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
创建目录
ansible all -m file -a 'path=/mnt/westos state=directory'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
ansible all -m file -a 'path=/mnt/westos/file1 state=touch'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
递归更改目录权限
ansible all -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
软连接
ansible all -m file -a 'src=/mnt/file dest=/mnt/yyl state=link'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
硬链接
ansible all -m file -a 'src=/mnt/file dest=/mnt/yyl1 state=hard'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt ' 
5、archive模块、unarchive模块
(1)archive模块——压缩
| 参数 | 功能 | 
|---|---|
| path | 打包目录名称 | 
| dest | 声称打包文件名称 | 
| format | 打包格式 | 
| owner | 指定文件所属人 | 
| mode | 指定文件权限 | 
ansible all -m archive -a "path=/mnt dest=/mnt/mnt.tar.gz format=gz owner=yyl mode=700"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
 
 
(2)unarchive模块——解压缩
| copy | 默认为 yes,从ansible主机复制文件到受控主机设定为 no,从受控主机中寻找src源文件 | 
| remote_src | 功能同copy且相反 设定为 yes表示包在受控主机设定为 no表示包在ansible主机 | 
| src | 压缩文件的路径 | 
| dest | 受控主机目录 | 
| mode | 解压后文件的权限 <copy=yes> | 
tar zcf ansible.tar.gz  .
ls
ansible all -m unarchive -a 'src=./ansible.tar.gz dest=/mnt copy=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '
 
提前在vm1,2上压缩一个不被限制的gz包
ansible all -m unarchive -a 'src=/mnt/file.tar.gz  dest=/mnt copy=no'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

6、hostname模块、cron模块
(1)hostname模块
 管理主机名称
 常用参数:name:指定主机名称
ansible 192.168.67.112 -m hostname -a 'name=vm22'
ansible 192.168.67.112 -m shell -a 'hostname'
ansible 192.168.67.112 -m hostname -a 'name=vm1'
ansible 192.168.67.112 -m shell -a 'hostname' 
