ad-hoc命令能够让我们通过命令的方式快速的完成一些任务,类似于Linux下的shell命令,常用于一些简单的,临时性的需求。
SSH公钥认证
我们可以通过ask-pass选项手动输入密码进行远程认证,但在批量管理节点时,可以通过配置SSH公钥进行免密认证。
管理节点生成公钥并复制到托管节点
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  | 
[root@h-luhx-254 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:0FFbEBbtTesV0W5JaDwVQy3WO60BRw/TWoXj7zNLpnI root@h-luhx-253
The key's randomart image is:
+---[RSA 2048]----+
|        ..*=o.BXB|
|       . o oo=B*B|
|      . . ...BoB*|
|       .    . *+=|
|        S    . *.|
|              o .|
|               + |
|           . E+o.|
|            o. .+|
+----[SHA256]-----+
[root@h-luhx-254 ~]# scp .ssh/id_rsa.pub @10.255.210.10:/root/.ssh/authorized_keys 
root@10.255.210.9's password: 
id_rsa.pub             100%  397   199.8KB/s   00:00 
[root@h-luhx-254 ~]# ssh 10.255.210.10
Last login: Tue Feb 23 09:02:54 2021 from h-luhx-254.vanke.com
  | 
 
托管节点配置SSH公钥
1
2
3
4
  | 
[root@h-luhx-253 ~]# ssh-agent  bash
[root@h-luhx-253 ~]# ssh-add /root/.ssh/id_rsa
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
  | 
 
现有如下Inventory文件
1
2
3
4
  | 
[root@h-luhx-254 Inventory]# cat hosts 
[db]
10.255.210.10
10.255.210.15
  | 
 
测试简单的命令
1
2
3
  | 
[root@h-luhx-254 Inventory]# ansible db[0] -a "hostname" -i /data/ansible/Inventory/hosts 
10.255.210.10 | CHANGED | rc=0 >>
h-luhx-253
  | 
 
Patterns
Patterns是指我们如何指定执行任务的托管节点,也就是与哪些主机进行交互。
下列patterns等于Inventory中的所有主机
当然,也可以直接指定对应主机的IP或主机名
1
2
  | 
h-luhx-253
10.255.210.10
  | 
 
指定Inventory中的一个或多个groups时,多个groups之间通过冒号分割
两个组之间的交集可以通过&符号,两个组的差集可以通过!符号
在高级语法中,我们也可以通过下标指定groups中的一个或多个主机
patterns支持正则表达式,只需要在开头用~符号声明
1
  | 
~(web|db).*\.example\.com
  | 
 
模块
ansible包含很多模块,默认是command(命令模块),我们可以通过-m选项指定不同模块,支持的模块可以通过ansible-doc -l查看,下面介绍一些常见的模块
1、Service模块
| 选项 | 
说明 | 
| arguments | 
提供一些参数 | 
| enabled | 
是否开机启动 | 
| name | 
服务名 | 
| pattern | 
定义一个模式,如果利用status获取服务状态无效时,则通过ps命令查看进程状态 | 
| runlevel | 
运行级别 | 
| sleep | 
restart时在stop和start休眠一定时间 | 
| state | 
对服务进行操作,包含stated、stopped、restarted、reloaded | 
 
下面通过service模块重启mysql服务并设置开机启动,
1
  | 
[root@h-luhx-254 Inventory]# ansible db[0] -m service -a "name=mysql state=restarted enabled=yes" -i /data/ansible/Inventory/hosts
  | 
 
2、file模块
| 选项 | 
说明 | 
| force | 
当源文件不存在但之后会创建时强制创建软链接或目标软链接已经存在时取消之前的链接并新建软连接 | 
| group | 
定义文件或目录的属组 | 
| mode | 
定义文件或目录的权限 | 
| owner | 
定义文件或目录的属主 | 
| path | 
定义文件或目录的路径 | 
| recurse | 
针对目录递归设置文件属性 | 
| src | 
链接的源文件路径,只适用于state=link | 
| dest | 
链接的目标文件路径,只适用于state=link | 
| state | 
directory表示目录不存在时创建目录,file表示文件不存在时也不会被创建,link表示创建软链接,hard表示创建硬链接,touch表示文件不存在时则创建新文件,absent表示删除文件或目录或取消链接文件 | 
 
创建目录
1
  | 
[root@h-luhx-254 ~]# ansible db[0] -m file -a "path=/data/ansible state=directory" -i /data/ansible/Inventory/hosts
  | 
 
删除文件或目录
1
  | 
[root@h-luhx-254 ~]# ansible db[0] -m file -a "path=/data/ansible state=absent" -i /data/ansible/Inventory/hosts
  | 
 
创建软链接
1
  | 
[root@h-luhx-254 ~]# ansible db[0] -m file -a "src=/tmp/docker-ce.repo dest=/root/docker-ce.repo state=link" -i /data/ansible/Inventory/hosts
  | 
 
3、copy模块
| 选项 | 
说明 | 
| backup | 
在覆盖前创建文件备份 | 
| src | 
复制的源文件路径,如果路径时一个目录,则递归进行复制,如果路径使用/结尾则只复制目录内容,否则连同目录全部复制 | 
| dest | 
复制的目标文件路径,如果源文件是一个目录,则目标也必须是一个目录 | 
| content | 
类似src | 
| directory_mode | 
设置目录权限,默认为系统默认权限 | 
| force | 
如果目标文件存在,但内容不同可设置yes强制覆盖,为no则表示文件不存在才会覆盖 | 
| others | 
file模块的选项也可以在此使用 | 
| validate | 
复制开始前进行验证,验证的文件需要通过携带%s传入 | 
 
复制文件
1
  | 
[root@h-luhx-254 ~]# ansible db[0] -m copy -a "src=/root/docker-ce.repo dest=/root/docker-ce.repo backup=yes" -i /data/ansible/Inventory/hosts
  | 
 
复制文件并设置权限
1
  | 
[root@h-luhx-254 ~]# ansible db[0] -m copy -a "src=/root/docker-ce.repo dest=/root/docker-ce.repo owner=root mode=644" -i /data/ansible/Inventory/hosts
  | 
 
文件检查
1
  | 
[root@h-luhx-254 ~]# ansible db[0] -m copy -a "src=/root/docker-ce.repo dest=/root/docker-ce.repo validate='vi %s'" -i /data/ansible/Inventory/hosts
  | 
 
4、setup模块
setup模块用于收集远程主机的信息,例如IP,时间、网卡、cpu等信息
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  | 
[root@h-luhx-254 ~]# ansible db[0] -m setup -a "filter=ansible_memory_mb" -i /data/ansible/Inventory/hosts
10.255.210.10 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 4673,
                "used": 3291
            },
            "real": {
                "free": 760,
                "total": 7964,
                "used": 7204
            },
            "swap": {
                "cached": 0,
                "free": 0,
                "total": 0,
                "used": 0
            }
        },
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
  | 
 
5、user模块
| 选项 | 
说明 | 
| name | 
用户名 | 
| password | 
用户密码,密码需要加密 | 
| state | 
用户状态,默认为present,表示添加用户,删除用户为absent | 
| update_password | 
修改用户密码,always表示新密码和旧密码不同时修改,on_create表示为新建用户指定密码 | 
| createhome | 
创建用户家目录,默认为yes | 
| remove | 
删除用户时,是否删除家目录 | 
 
创建用户
1
2
3
4
5
6
  | 
###得到Abcd123#加密后的数据
[root@h-luhx-254 ~]# python -c 'import crypt; print crypt.crypt("Abcd123#", "salt")'
sab0KRaX0F2gc
###利用加密密码创建用户
[root@h-luhx-254 ~]# ansible db[0] -m user -a "name=test password=sab0KRaX0F2gc" -i /data/ansible/Inventory/hosts
  | 
 
删除用户
1
  | 
[root@h-luhx-254 ~]# ansible db[0] -m user -a "name=test remove=yes state=absent" -i /data/ansible/Inventory/hosts
  | 
 
更新密码
1
2
3
4
5
6
  | 
###得到Abcd123加密后的数据
[root@h-luhx-254 ~]# python -c 'import crypt; print crypt.crypt("Abcd123", "salt")'
saCVI17SNGkXI
###利用加密数据更新密码
[root@h-luhx-254 ~]# ansible db[0] -m user -a "name=test update_password=always password=saCVI17SNGkXI" -i /data/ansible/Inventory/hosts
  | 
 
6、unarchive模块
| 选项 | 
说明 | 
| copy | 
是否先将文件复制到远程主机 | 
| creates | 
指定文件,当文件存在时,不进行解压 | 
| src | 
copy为yes时,需要指定源文件路径 | 
| dest | 
远程解压路径 | 
| group | 
解压后的文件属组 | 
| owner | 
解压文件的属主 | 
| list_files | 
yes则列出压缩包内的文件清单 | 
| mode | 
解压文件的权限 | 
 
1
  | 
[root@h-luhx-254 ~]# ansible db[0] -m unarchive -a "src=/root/docker-ce.tar.gz dest=/root/docker copy=yes" -i /data/ansible/Inventory/hosts
  | 
 
7、cron模块
| 选项 | 
说明 | 
| backup | 
修改cron前是否进行备份 | 
| cron_file | 
指定文件替换远程主机cron.d下的任务计划文件 | 
| day | 
日 | 
| hour | 
小时 | 
| minute | 
分钟 | 
| month | 
月 | 
| weekday | 
周 | 
| job | 
要执行的任务 | 
| name | 
任务描述 | 
| special_time | 
指定执行时间,包含reboot、yearly、annually、monthly、weekly、daily、hourly | 
| state | 
创建计划(present)还是删除计划(absent) | 
| user | 
执行计划任务的用户,默认为root | 
 
设置定时任务
1
  | 
[root@h-luhx-254 ~]#ansible db[0] -m cron -a 'name="test job" job="/root/job.sh" minute=*/1' -i /data/ansible/Inventory/hosts
  | 
 
删除定时任务
1
  | 
[root@h-luhx-254 ~]#ansible db[0] -m cron -a 'name="test job" state=absent' -i /data/ansible/Inventory/hosts
  | 
 
8、yum模块
| 选项 | 
说明 | 
| config-file | 
yum配置文件 | 
| disable_gpg_check | 
关闭gpg检查 | 
| disablerepo | 
禁用默认源 | 
| enablerepo | 
启用某个源 | 
| name | 
软件包的名称,也可以是url或本地rpm包 | 
| state | 
present、absent、latest | 
 
安装最新版的合httpd
1
  | 
[root@h-luhx-254 ~]#ansible db[0] -m yum -a "name=httpd state=latest" -i /data/ansible/Inventory/hosts
  | 
 
9、get_url模块
| 选项 | 
说明 | 
| sha256sum | 
下载完成后对文件进行sha2556sum检查 | 
| timeout | 
超时时间,默认为10秒 | 
| url | 
下载路径 | 
| url_user/url_password | 
访问用户和密码 | 
| use_proxy | 
使用代理 | 
 
1
  | 
[root@h-luhx-254 ~]#ansible db[0] -m get_url -a 'url="https://www.python.org/ftp/python/3.9.0/python-3.9.0-amd64.exe" dest=/media/python-3.9.0-amd64.exe mode=0755 timeout=30'
  | 
 
10、lineinfile模块
| 选项 | 
说明 | 
| path | 
需要修改文件的绝对路径 | 
| regexp | 
正则匹配要修改的内容 | 
| line | 
要修改的内容 | 
| state | 
present和absent | 
| backrefs | 
no表示未匹配到新增行,匹配到则替换行;yes表示未匹配到则不修改,匹配到则替换行 | 
| backup | 
是否备份原文件 | 
| insertafter | 
匹配到行在其后面新增行 | 
| insertbefore | 
匹配到行在其前面新增行 |