ansible运行模块的两种方式
ansible westos -m shell -a 'ls /mnt'
vim test.yml
- hosts: all
tasks:
- shell: ls /mnt
ansible-playbook test.yml

如何查看模块帮助
ansible-doc -l
ansible-doc shell
ansible-doc shell -s


ansible命令的常用执行参数
ansible 清单 - m 模块 - a 模块参数
| --version | 显示版本 |
| -m module | 指定模块,默认为command模块 |
| -v |
详细过程
-
vv
-
vvv
更详细过程
|
| --list | 显示主机列表,也可以用--list-hosts |
| -k | 提示输入ssh连接密码,默认key认证 |
| -C | 预执行检测 |
| -T | 执行命令的超时时间,默认10s |
|
-
u
|
指定远程执行的用户
|
|
-
b
|
执行
sudo
切换身份操作
|
| -become-user=USERNAME | 指定sudo的用户 |
|
-
K
|
提示输入
sudo
密码
|
ansible all -m ping
ansible all -m ping -v
ansible westos -m shell -a 'whoami' -u westos -k -b --become-user=westos
以上配置参数都可以再配置文件。ansible.cfg中输入
command模块的使用方法
| chdir | 执行命令前先进入到指定目录 |
| cmd | 运行命令指定 |
| creates | 如果文件存在将不运行 |
| removes | 如果文件存在在将运行 |
| free_form | 在远程主机中执行的命令,此参数不需要加 |
ansible all -m command -a 'chdir=/mnt pwd'
ansible all -m command -a 'chdir=/mnt pwd creates=/mnt'
Linux中的很多通配符在command模块中不支持

shell模块的使用方法
| chdir | 执行命令前先进入到指定目录 |
| cmd | 运行命令指定 |
| creates | 如果文件存在将不运行 |
| removes | 如果文件存在在将运行 |
| free_form | 在远程主机中执行的命令,此参数不需要加 |
| executable | 指定执行环境,默认为sh |
ansible all -m shell -a " ps ax | grep $$ " -k
ansible all -m shell -a "executable=sh ps ax | grep $$ " -k
ansible all -m shell -a "executable=bash ps ax | grep $$ " -k

scrip模块使用方法
vim ~/.ansible / westos.sh!/ bin / bashecho $HOSTNAMEansible all -m script -a "~/.ansible/westos.sh"

copy模块的使用
| src | 源文件 |
| dest | 目的地文件 |
| owner | 指定目的地文件所有人 |
| group | 指定目的地文件所有组 |
| mode | 指定目的地文件权限 |
| backup=yes | 当受控主机中存在文件时备份原文件 |
| content | 指定文本内容直接在受控主机中生成文件 |
ansible all -m copy -a "src=westos.sh dest=/mnt/westos.sh owner=xxx mode=707 backup=yes"ansible all -m shell -a "ls -l /mnt/westos.sh"

fetch模块的使用方法
| src | 受控主机的源文件 |
| dest | 本机目录 |
| flat | 基本名称功能 |
ansible all -m fetch -a "src=/mnt/westos.sh dest=/tmp "ansible 192.168.91.89 -m fetch -a "src=/mnt/westos.sh dest=/tmp/westosname.sh flat=yes"


file模块的使用方法
| path | 指定文件名称 |
| state | 指定操作状态 |
| touch |
建立
|
| absent |
删除
|
| directory |
递归
|
| link |
建立软链接
|
| hard | 建立硬链接 |
| mode |
设定权限
|
| owner | 设定文件用户 |
| group | 设定文件组 |
| src | 源文件 |
| dest | 目标文件 |
| recurse=yes | 递归更改 |
ansible all -m file -a 'src=/mnt/file1 dest=/mnt/filehard state=hard'ansible all -m file -a 'path=/mnt/file2 state=touch owner=devops group=xxx mode=777'


压缩及解压模块
| path | 打包目录名称 |
| dest | 声称打包文件名称 |
| format | 打包格式 |
| owner | 指定文件所属人 |
| mode | 指定文件权限 |
ansible all -m archive -a 'path=/etc dest=/opt/etc.tar.gz format=gz owner=xxx mode=700'
| copy | 默认为yes 从ansible主机复制文件到受控主机 设定为no 从受控主机中寻找src源文件 |
| remote_src | 功能同copy且相反 设定为yes 表示包在受控主机 设定为no表示包在ansible主机 |
| src | 包路径,可以使ansible主机也可以使受控主机 |
| dest | 受控主机目录 |
| mode | 加压后文件权限 <copy=yes> |
ansible all -m unarchive -a "src=/opt/etc.tar.gz dest=/mnt copy=no"

hostname模块
| name | 指定主机名称 |
ansible 192.168.91.89 -m hostname -a 'name=AAAA.westos.com'
定时任务设定模块
| minute | 分钟 |
| hour | 小时 |
| day | 天 |
| month | 月 |
| weekday | 周 |
| name | 任务名称 |
| job | 任务脚本或命令 |
| disabled | yes 禁用计划任务 no 启动计划任务 |
| state | |
| absent | 删除计划任务 |
ansible all -m cron -a "job='echo hello' name=test disabled=yes"

软件仓库配置模块
| name | 指定仓库名称 |
| baseurl | 指定源路径 |
| description | 指定仓库描述 |
| file | 指定仓库文件名称 |
| enabled | 仓库是否启用 |
| gpgcheck | 仓库是否检测gpgkey |
| state | 默认值present 建立 absent 为删除 |
ansible all -m yum_repository -a 'name=AppStream file=westos baseurl=http://192.168.91.88/var/www/html/westos/AppStream gpgcheck=no description="AppStream" enabled=yes state=present'

利用dnf模块管理软件
| name | 指定包 |
| state | 指定动作 |
| present | 安装 |
| latest | 更新 |
| absent | 删除 |
| list | 列出指定信息 |
| httpd | |
| installed | |
| all | |
| available | |
| disable_gpg_check | 禁用gpgkey检测 |
| enablerepo | 指定安装包来源 |
| disablerepo | 禁用安装包来源 |
ansible all -m dnf -a 'name=httpd state=present enablerepo=AppStream'

service服务管理模块
| name | 指定服务名称 |
| state | 指定对服务的动作 |
|
started
| |
|
stoped
| |
|
restarted
| |
| reloaded | |
| enabled | 设定服务开机是否启动
#yes
开启启动
#no
开机不启动
|
ansible all - m service - a "name = httpd state = started enabled = yes"

火墙控制模块firewalld
| zone | 火墙的域 |
| service | 服务名称 |
| permanent | 永久生效 |
| state | |
| enabled | 允许 |
| disabled | 拒绝 |
| immediate | 立即生效 |
ansible all -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes'

利用user模块管理用户
| name | 必须参数,用于指定要操作的用户名称 |
| group | 指定用户所在的基本组。 |
| gourps | 指定用户所在的附加组。 |
| append | 指定添加附加组默认值为no |
| shell | 指定用户的默认 shell |
| uid | 指定用户的 uid 号 |
| comment | 指定用户的注释信息 |
| state | 用于指定用户是否存在于远程主机 |
| present |
建立
|
| absent |
删除
|
| remove | 当删除用户是删除用户家目录,默认值为no |
| password | 此参数用于指定用户的密码。但密码为明文 可以用openssl password -6 '密码'生成加密字符 |
| generate_ssh_key | 生成sshkey |
ansible all -m user -a 'name=linux group=72 groups=1000 shell=/bin/sh uid=6666 comment="linux user" state=present'

利用group模块管理组
| name | 用于指定要操作的组名称 |
| state | 用于指定组的状态 |
|
absent
| 删除 |
|
present
| 建立 |
| gid | 用于指定组的gid。 |
ansible all -m group -a 'name=linux gid=2000 state=present'

文件关键行替换
| path | 指定要操作的文件 |
| line | 指定文本内容。 "|+" 表示格式化输入 |
| regexp | 使用正则表达式匹配对应的行当替换文本时
##
如果有多行文本都能被匹配
##
则只有最后面被匹配到的那行文本才会被替换
##
当删除文本时
,
如果有多行文本都能被匹配
##
这么这些行都会被删除
。
|
| state | 当想要删除对应的文本时需要将state参数的值设置为absent state的默认值为present |
| backrefs | 当内容无匹配规则时不对文件做任何更改,默认值为no 向后引用regexp变量信息 |
| insertafter | 借助insertafter参数可以将文本插入到“指定的行”之后 insertafter参数的值可以设置为EOF或者正则表达式 |
| insertbefore | 借助insertbefore参数可以将文本插入到“指定的行”之前 insertbefore参数的值可以设置为BOF或者正则表达式 |
| backup | 是否在修改文件之前对文件进行备份 |
| create | 当要操作的文件并不存在时,是否创建对应的文件 |
ansible all -m lineinfile -a 'path=/mnt/westos line="hello westos\nhello westos" create=yes'ansible all -m lineinfile -a 'path=/mnt/westos line="hello linux" create=yes'ansible all -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes'



替换文件中的关键字
| path | 指定要操作的文件 |
| regexp | 指定一个正则表达式 文件中与正则匹配的字符串将会被替换 |
| replace | 指定最终要替换成的字符串 |
| backup | 是否在修改文件之前对文件进行备份,最好设置为yes |
ansible all -m replace -a 'path=/mnt/westos regexp="hello" replace="westos_hello" backup=yes'
利用setup采集被控主机的变量信息
| filter | 用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息 |
ansible all - m setup - kansible all - m setup - a "filter = 'ansible_all_ipv4_addresses' " - k
利用debug显示指定信息
|
msg
:
| 调试输出的消息 |
| var: |
将某个任务执行的输出作为变量传递给debug
模块
debug
会直接将其打印输出
|
|
verbosity
:
|
debug
的级别
(
默认是
0
级
,
全部显示
)
|
ansible all -m debug -a 'var="westos"' -e westos="linnnnn"
ansible all -m debug -a 'msg="hello hello"'




















