ansible all -m command -a "useradd lee" -u root -k
ansible all -m command -a "userdel -r lee" -u root -k
ansible all -m command -a "chdir=/etc cat passwd " -u root -k
ansible all -m command -a "chdir=/etc creates=/etc/passwd cat passwd " -u root -k
ansible all -m command -a "chdir=/etc removes=/etc/passwd cat passwd " -u root -k
##注意##
#Linux中的很多通配符在command模块中不支持
二、shell
#功能:
#和command功能类似
常用参数
| chdir | 执行命令前先进入到指定目录 |
| cmd | 运行命令指定 |
| creates | 如果文件存在将不运行 |
| removes | 如果文件存在在将运行 |
| free_form | 在远程主机中执行的命令,此参数不需要加 |
| executable | 指定执行环境,默认为sh |
#实例
ansible all -m shell -a "executable=/bin/bash ps ax | grep $$ " -k
三、script
#功能:
在ansible主机中写好的脚本在受控主机中执行
#实例
vim /mnt/westos.sh
#!/bin/bash
echo $HOSTNAME
ansible all -m script -a "/mnt/westos.sh" -k
四、copy
#功能
从ansible主机复制文件到受控主机
常用参数
| src | 源文件 |
| dest | 目的地文件 |
| owner | 指定目的地文件所有人 |
| group | 指定目的地文件所有组 |
| mode | 指定目的地文件权限 |
| backup=yes | 当受控主机中存在文件时备份原文件 |
| content | 指定文本内容直接在受控主机中生成文件 |
#实例
ansible all -m copy -a "src=/root/westos dest=/mnt/westos owner=lee mode=777 backup=yes"
ansible all -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/westosfile1 owner=lee mode=600"
五、fetch
#功能
从受控主机把文件复制到ansible主机,但不支持目录
常用参数
| src | 受控主机的源文件 |
| dest | 本机目录 |
| flat | 基本名称功能(加了之后复制过去的文件不再层层目录,只有文件) |
#实例
ansible all -m fetch -a "src=/etc/hostname dest=/mnt" -k
ansible all -m fetch -a "src=/etc/hostname dest=/mnt"
六、file
#功能
设置文件的属性
常用参数
| path | 指定文件名称 |
| state | 指定操作状态 |
| touch | 建立 |
| absent | 删除 |
| directory | 递归 |
| link | 建立链接 |
| hard | |
| mode | 设定权限 |
| owner | 设定文件用户 |
| group | 设定文件组 |
| src | 源文件 |
| dest | 目标文件 |
| recurse=yes | 递归更改 |
#实例
ansible all -m file -a 'path=/mnt/test.sh state=touch'
ansible all -m file -a 'path=/mnt/test.sh state=absent'
ansible all -m file -a 'path=/mnt/westos state=directory'
ansible all -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes'
ansible all -m file -a 'src=/etc/passwd path=/mnt/passwd state=link'
ansible all -m file -a 'src=/mnt/file path=/mnt/westos1 state=hard'
ansible all -m file -a 'path=/mnt/file state=touch owner=lee group=westos mode=777'
七、unarchive
#功能
解压缩
常用参数
| copy | 默认为yes 从ansible主机复制文件到受控主机 |
| | 设定为no 从受控主机中寻找src源文件 |
| remote_src | 功能同copy且相反 |
| | 设定为yes 表示包在受控主机 |
| | 设定为no表示包在ansible主机 |
| src | 包路径,可以使ansible主机也可以使受控主机 |
| dest | 受控主机目录 |
| mode | 加压后文件权限 |
#实例
ansible all -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt owner=lee' -k
ansible all -m unarchive -a "src=/opt/etc.tar.gz dest=/mnt copy=no"
ansible list1 -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt remote_src=yes' ##包在受控主机 no的时候在ansible 主机
八、archive
#作用
压缩
常用参数
| path | 打包目录名称 |
| dest | 生成打包文件名称 |
| format | 打包格式 |
| owner | 指定文件所属人 |
| mode | 指定文件权限 |
#实例
ansible all -m archive -a 'path=/etc dest=/opt/etc.tar.gz format=gz owner=lee mode=700' -k
九、hostname
#作用
管理主机名称
#常用参数
name ##指定主机名称
#实例
ansbile 172.25.1.10 -m hostname -a 'name=westos.westos.com'
十、cron
#作用
计划任务
常用参数
| minute | 分钟 |
| hour | 小时 |
| day | 天 |
| month | 月 |
| weekday | 周 |
| name | 任务名称 |
| job | 任务脚本或命令 |
| disabled | yes 禁用计划任务 |
| | no 启动计划任务 |
| state | absent 删除计划任务 |
#实例
ansible list1 -m cron -a "job='echo hello' name=test hour=5 user=westos"
ansible list1 -m cron -a "job='echo hello' name=test disabled=yes"
ansible list1 -m cron -a "job='echo hello' name=test state=absent"
十一、yum_repository
#作用
配置系统软件仓库源文件
常用参数
| name | 指定仓库名称 |
| baseurl | 指定源路径 |
| description | 指定仓库描述 |
| file | 指定仓库文件名称 |
| enabled | 仓库是否启用 |
| gpgcheck | 仓库是否检测gpgkey |
| state | 默认值present 建立 absent 为删除 |
#实例
ansible list1 -m yum_repository -a "file=linux name=AppStream description=AppSteam baseurl=http://172.25.1.1/linux/AppStream enabled=yes gpgcheck=no"
ansible list1 -m yum_repository -a "file=linux name=BaseOS description=BaseOS baseurl=http://172.25.1.1/linux/BaseOS enabled=yes gpgcheck=no"
ansible all -m yum_repository -a "name=AppStream file=westos_test state=absent" -k
十二、dnf
#作用
管理系统中的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=latest"
ansible all -m dnf -a 'name="httpd,mariadb-server" state=present'
ansible all -m dnf -a 'name=httpd state=absent'
ansible all -m dnf -a 'name=httpd state=absent autoremove=no'
ansible all -m dnf -a 'name=httpd state=present enablerepo=AppStream'
ansible all -m dnf -a 'name="*" state=latest'
ansible all -m dnf -a 'name=http://172.25.1.1/software/wps-office-xxx.rpm state=present'
ansible all -m dnf -a 'name="@ Virtualization Tools" state=present'
十三、service
#作用
管理系统服务状态
常用参数
| name | 指定服务名称 |
| state | 指定对服务的动作 started stoped reloaded |
| enabled | 设定服务开机是否启动 yes开启启动 no开机不启动 |
#实例
ansible all -m service -a "name=httpd state=started enabled=yes" -k
ansible all -m service -a "name=httpd state=restarted enabled=yes" -k
十四、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=lee'
ansible all -m user -a 'name=lee state=absent'
ansible all -m user -a 'name=lee remove=yes state=absent'
ansible all -m user -a 'name=lee group=888'
ansible all -m user -a 'name=lee group=888 groups="user1,user2"'
ansible all -m user -a 'name=lee groups="user3"'
ansible all -m user -a 'name=lee groups="user1,user2" append=yes'
openssl passwd -6 'westos'
ansible all -m user -a 'name=lee password="$6$F4OBwqoXAigDV.dn$I2OgEPB3kfyl8CPmdh3Y8vKDqewZKrVMIDPPIt8GKnhs/DW4gZHfxrZX5ziQN7rVjISX7l14KwDQHEd.uprlV/"'
这里注意外部用单引,否则会出现$被注释
ansible all -m user -a 'name=lee generate_ssh_key=yes''
十五、group
#作用
group 模块可以帮助我们管理远程主机上的组。
常用参数
| name | 用于指定要操作的组名称。 |
| state | 用于指定组的状态 present 建立 absent 删除 |
| gid | 用于指定组的gid |
#实例
ansible all -m group -a 'name=westoslee'
ansible all -m group -a 'name=westoslee state=absent'
ansible all -m group -a 'name=westoslee gid=8888'
测试: