注意:-a 参数后的命令用单引号; 双引号有可能会出问题, 特别是在 user 模块
模块一: 测试目标主机是否在线, ping 模块
主机如果在线, 则回复 Pong
- [root@ansible ~]# ansible web -m ping
- 192.168.30.36 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
- 192.168.30.32 | SUCCESS => {
- "changed": false,
- "ping": "pong"
- }
模块二: command 模块和 shell 模块
用于在各被管理主机节点运行指定的命令
shell 与 command 的区别: command 模块是执行 ansible 自带模块执行, 如果要用> <| & ' '等管道, 需要使用 shell 模块
- [root@ansible tmp]# ansible web -m command -a 'uptime'
- 192.168.30.32 | SUCCESS | rc=0>>
- 21:10:43 up 138 days, 1:34, 1 user, load average: 0.00, 0.01, 0.05
- 192.168.30.36 | SUCCESS | rc=0>>
- 21:08:30 up 95 days, 3:02, 4 users, load average: 0.00, 0.00, 0.00
shell 模块:
- [root@ansible tmp]# ansible web -m shell -a 'cat /etc/fstab | grep ^UUID'
- 192.168.30.36 | SUCCESS | rc=0>>
- UUID=28b3513d-9e6f-42a8-ade0-7700573fed2a /boot ext4 defaults 1 2
- 192.168.30.32 | SUCCESS | rc=0>>
- UUID=de2e369a-c0e8-4188-89e0-db1f4aded764 /boot xfs defaults 0 0
相关选项如下:
creates: 一个文件名, 当文件名存在, 则该命令不执行
chdir: 在执行指令之前, 先切换到该目录
free_from: 要执行的 Linux 指令
removes: 一个文件名, 当文件名不存在, 则该命令不执行
executable: 切换 shell 来执行指令, 该执行路径必须是一个绝对路径
参考:<https://docs.ansible.com/ansible/latest/modules/shell_module.html>
模块三: 远程复制备份 --copy 模块
[root@ansible tmp]# ansible web -m copy -a 'src=/etc/hosts dest=/tmp/hosts'
相关选项:
backup: 在覆盖前, 将源文件备份, 备份文件包含时间信息, 有俩选项: yes|no
content: 用于替代 "src", 可以直接设定指定文件的值
dest: 必选项, 要将源文件复制到远程主机的绝对路径, 如果源文件是一个目录, 那么该路径必须是个目录
directory_mode: 递归设定目录的权限, 默认为系统默认权限
force: 如果目标主机包含该文件, 但内容不同, 如果设置为 yes, 则强制覆盖, 如果为 no, 则只有当目标主机的目标位置不存在该文件时, 才复制. 默认为 yes
others: 所有的 file 模块里的选项都可以在这里使用
src: 被复制到远程主机的本地文件, 可以是绝对路径, 也可以是相对路径. 如果路径是一个目录, 他将递归复制. 这种情况路径使用 "/" 来结尾, 则只复制目录里的内容, 如果没有使用 "/", 则包含目录在内的全部内容复制, 类似与 rsync.
owner: 指定属主
group: 指定属组
mode: 指定权限, 可以以数字指定比如 0644
参考:<https://docs.ansible.com/ansible/latest/modules/copy_module.html>
模块四: 对远程文件管理 --file 模块
修改文件权限:
[root@ansible ~]# ansible web -m file -a 'path=/srv/a.txt mode=600 owner=snow group=snow'
创建一个文件:
[root@ansible ~]# ansible web -m file -a 'dest=/srv/a.txt state=touch'
创建一个目录文件:
[root@ansible ~]# ansible web -m file -a 'path=/srv/haha mode=600 state=directory'
创建一个软链接:
[root@ansible ~]# ansible web -m file -a 'dest=/srv/a.lnk src=/srv/a.txt state=link'
删除文件:
[root@ansible ~]# ansible web -m file -a 'path=/srv/a.lnk state=absent'
相关选项:
force: 需要在两种情况下强制创建软链接, 一种源文件不存在, 但之后会建立的情况; 另一种是目标软链接存在, 需要先取消之前的链接, 再创建新链接, 有两个选项: yes|no
path: 必选项, 定义文件 / 目录的路径(alias:dest,name)
recurse: 递归设置文件的属性, 只对目录有效
src: 被链接的源文件路径, 只能用于 state=link 情况
dest: 目标文件路径, 只能用于 state-link 情况
state:
directory: 如果目录文件不存在, 就创建目录文件
*file: 即使文件不存在也会被创建
link: 创建软链接
hard: 创建硬链接
touch: 如果文件不存在, 则会创建一个新的文件, 如果文件或目录已存在, 则更新其最后修改时间
absent: 删除目录, 文件或者取消链接文件
参考:<https://docs.ansible.com/ansible/latest/modules/file_module.html>
fetch 模块: 从远程服务器拉取文件至本机, 只能 fetch 文件, 不能 fetch 目录, 如果拉目录, 先 tar/zip 再拉到本机即可.
使用参考:<https://docs.ansible.com/ansible/latest/modules/fetch_module.html>
模块五: 程序包管理工具 --yum 模块
安装程序包(可接版本号):
[root@ansible ~]# ansible web -m yum -a 'name=httpd state=present'
安装最新程序包:
[root@ansible ~]# ansible web -m yum -a 'name=httpd state=latest'
卸载程序包:
[root@ansible ~]# ansible web -m yum -a 'name=httpd state=absent'
相关选项:
- conf_file #设定远程 yum 安装时所依赖的配置文件. 如配置文件没有在默认的位置.
- disable_gpg_check #是否禁止 GPG checking, 只用于 `present'or `latest'.
- disablerepo #临时禁止使用 yum 库. 只用于安装或更新时.
- enablerepo #临时使用的 yum 库. 只用于安装或更新时.
- name= #所安装的包的名称
- state= #present 安装, latest 安装最新的, absent 卸载软件.
- update_cache #强制更新 yum 的缓存
参考:<https://docs.ansible.com/ansible/latest/modules/yum_module.html>
注意: 此模块只能在 Python2 上使用, 在 Python3 上需要使用 dnf 模块用于包管理
使用参考:<https://docs.ansible.com/ansible/latest/modules/dnf_module.html>
模块六: 服务程序管理 --Service 模块
启动服务:
[root@ansible ~]# ansible web -m service -a 'name=httpd state=started'
开机启动服务:
[root@ansible ~]# ansible web -m service -a 'name=httpd enabled=yes'
相关选项:
- arguments #命令行提供额外的参数
- enabled #设置开机启动.
- name= #服务名称
- runlevel #开机启动的级别, 一般不用指定.
- sleep #在重启服务的过程中, 是否等待. 如在服务关闭以后等待 2 秒再启动.
- state #started 启动服务, stopped 停止服务, restarted 重启服务, reloaded 重载配置
参考:<https://docs.ansible.com/ansible/latest/modules/service_module.html>
模块七: 管理计划任务 --cron 模块
[root@ansible ~]# ansible web -m cron -a 'name="sync time"minute=*/10 job="/usr/sbin/ntpdate ntp.aliyun.com"'
相关选项:
- backup= # 如果设置, 创建一个 crontab 备份
- cron_file= #如果指定, 使用这个文件 cron.d, 而不是单个用户 crontab
- day= # 日应该运行的工作( 1-31, *, */2, etc )
- hour= # 小时 ( 0-23, *, */2, etc )
- job= #指明运行的命令是什么
- minute= #分钟( 0-59, *, */2, etc )
- month= # 月( 1-12, *, */2, etc )
- name= #定时任务描述
- reboot # 任务在重启时运行, 不建议使用, 建议使用 special_time
- special_time # 特殊的时间范围, 参数: reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
- state #指定状态, prsent 表示添加定时任务, 也是默认设置, absent 表示删除定时任务
- user # 以哪个用户的身份执行
- weekday # 周 ( 0-6 for Sunday-Saturday, *, etc )
参考:<https://docs.ansible.com/ansible/latest/modules/cron_module.html>
模块八: 远程主机执行本地脚本 --script 模块
[root@ansible ~]# ansible web -m script -a '/tmp/a.sh'
相关选项:
creates: 一个文件名, 当文件名存在, 则该命令不执行
chdir: 在执行指令之前, 先切换到该目录
free_from: 要执行的 Linux 指令
removes: 一个文件名, 当文件名不存在, 则该命令不执行
executable: 切换 shell 来执行指令, 该执行路径必须是一个绝对路径
decrypt: 使用 Vault 控制源文件的自动解码, 选项 yes|no
参考:<https://docs.ansible.com/ansible/latest/modules/script_module.html>
模块九: 用户管理 --User 模块
创建加密密码:
- [root@ansible ~]# echo 123456 | openssl passwd -1 -stdin
- $1$wMroVeEW$2ImLQiNupnwDJ5nk86F2X0
创建新用户:
[root@ansible ~]# ansible web -m user -a 'name=wolf system=yes password=$1$wMroVeEW$2ImLQiNupnwDJ5nk86F2X0 state=present'
更新用户密码:
[root@ansible ~]# ansible web -m user -a 'name=wolf update_password=always password=$1$wMroVeEW$2ImLQiNupnwDJ5nk86F2X0'
删除用户:
[root@ansible ~]# ansible web -m user -a 'name=wolf remove=yes state=absent'
相关选项:
- comment # 用户的描述信息
- createhom # 是否创建家目录, yes, 默认项, 即创建家目录; no, 创建用户时不创建家目录.
- force # 在使用 `state=absent'是, 行为与 `userdel --force'一致.
- group # 指定基本组
- groups # 指定附加组, 如果指定为 ('groups=') 表示删除所有组
- home # 指定用户家目录
- login_class #可以设置用户的登录类 FreeBSD, OpenBSD and NetBSD 系统.
- move_home # 如果设置为 `home='时, 试图将用户主目录移动到指定的目录
- name= # 指定用户名
- non_unique # 该选项允许改变非唯一的用户 ID 值
- password # 指定用户密码, password 参数需要接受 md5 加密后的值
- remove # yes: 删除家目录, 需要指定此参数; no: 默认项, 删除用户时默认不删除用户家目录.
- shell # 指定默认 shell
- state #设置帐号状态, 默认值为 present 表示创建, 可不写, 指定值为 absent 表示删除
- system # 当创建一个用户, 设置这个用户是系统用户. 这个设置不能更改现有用户.
- uid #指定用户的 uid
- update_password # 更新用户密码, always: 新密码和旧密码不同时进行修改, on_create: 为新建用户指定密码
- expires #指明密码的过期时间
参考:<https://docs.ansible.com/ansible/latest/modules/user_module.html>
模块十: 收集远程主机的信息 --setup 模块
收集可用的 facts 用于内建变量. 每个主机的各种信息, cpu 颗数, 内存大小等. 会存在 facts 中的某个变量中. 调用后返回很多对应主机的信息, 在后面的操作中可以根据不同的信息来做不同的操作. 如 redhat 系列用 yum 安装, 而 debian 系列用 apt 来安装软件
[root@ansible ~]# ansible web -m setup
参考:<https://docs.ansible.com/ansible/latest/modules/setup_module.html>
来源: https://www.cnblogs.com/walkman-sky/p/9458682.html