主机连通测试
-bash-4.2 ansible web -m ping
172.17.0.4 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.17.0.3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
command模块
模块中常见的一些用法
chdir 在执行命令之前,先切换到该目录
executable 切换shell来执行命令,需要使用命令的绝对路径
free_form 要执行的Linux指令,一般使用Ansible的-a参数代替
creates 一个文件名,当这个文件存在,则该命令不执行,可以用来做判断
removes 后接一个文件名,如果这个文件不存在,则该命令不执行
用法实例:
-bash-4.2 ansible web -m command -a "ls /root"
172.17.0.3 | SUCCESS | rc=0 >>
anaconda-ks.cfg
172.17.0.4 | SUCCESS | rc=0 >>
anaconda-ks.cfg
-bash-4.2 ansible web -m command -a 'creates=/data/aaa.jpg ls'
如果/data/aaa.jpg存在,则不执行“ls”命令
192.168.37.122 | SUCCESS | rc=0 >>
skipped, since /data/aaa.jpg exists
-bash-4.2 ansible web -m command -a 'removes=/data/aaa.jpg cat /data/a'
如果/data/aaa.jpg存在,则执行“cat /data/a”命令
192.168.37.122 | SUCCESS | rc=0 >>
hello
3、shell 模块
shell模块可以在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等。
[root@server ~] ansible web -m shell -a 'cat /etc/passwd |grep "keer"'
192.168.37.122 | SUCCESS | rc=0 >>
keer:x:10001:1000:keer:/home/keer:/bin/sh
192.168.37.133 | SUCCESS | rc=0 >>
keer:x:10001:10001::/home/keer:/bin/sh
4、copy 模块
这个模块用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等。
src
被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于"rsync"
content 用于替换"src",可以直接指定文件的值
dest 必选项,将源文件复制到的远程主机的绝对路径
backup 当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息
directory_mode 递归设定目录的权限,默认为系统的权限(目录755,文件644)
force 当目标主机包含该文件,但内容不同时,设为yes,表示强制覆盖,为no ,表示目标主机的目标位置不存在该文件才进行复制,默认为yes
others 所有的file模块中的选项可以在这里使用
示例
-bash-4.2 ansible web -m copy -a 'src=/root/a.txt dest=/root/b.txt'
172.17.0.4 | SUCCESS => {
"changed": true,
"checksum": "0eed149356fa2881d6474da8c990cc9fff8d4c42",
"dest": "/root/b.txt",
"gid": 0,
"group": "root",
"md5sum": "e214bed936063693b516744eaf162424",
"mode": "0644",
"owner": "root",
"size": 20,
"src": "/root/.ansible/tmp/ansible-tmp-1570798203.02-252755618621282/source",
"state": "file",
"uid": 0
}
172.17.0.3 | SUCCESS => {
"changed": true,
"checksum": "0eed149356fa2881d6474da8c990cc9fff8d4c42",
"dest": "/root/b.txt",
"gid": 0,
"group": "root",
"md5sum": "e214bed936063693b516744eaf162424",
"mode": "0644",
"owner": "root",
"size": 20,
"src": "/root/.ansible/tmp/ansible-tmp-1570798203.01-155488878434273/source",
"state": "file",
"uid": 0
}
-bash-4.2 ansible web -m copy -a 'content="i am king\n" dest=/root/b.txt mode=666'
172.17.0.4 | SUCCESS => {
"changed": true,
"checksum": "05f14542dc99048e457349477ba0eb7a0a77acd4",
"dest": "/root/b.txt",
"gid": 0,
"group": "root",
"md5sum": "58444340dfc282702932b6c35546b50d",
"mode": "0666",
"owner": "root",
"size": 10,
"src": "/root/.ansible/tmp/ansible-tmp-1570798568.82-162397482267113/source",
"state": "file",
"uid": 0
}
-bash-4.2 ansible web -m shell -a "cat /root/b.txt"
172.17.0.4 | SUCCESS | rc=0 >>
i am king
// 追加覆写
5、file 模块
该模块主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等。
下面是一些常见的命令:
force
#需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group
#定义文件/目录的属组。后面可以加上mode
:定义文件/目录的权限
owner
#定义文件/目录的属主。后面必须跟上path
:定义文件/目录的路径
recurse
#递归设置文件的属性,只对目录有效,后面跟上
src
: # 被链接的源文件路径,只应用于state=link
的情况
dest
#被链接到的路径,只应用于state=link
的情况
state
#状态,有以下选项:
directory
:如果目录不存在,就创建目录
file
:即使文件不存在,也不会被创建
link
:创建软链接
hard
:创建硬链接
touch
:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent
:删除目录、文件或者取消链接文件
① 创建目录:
[root@server ~] ansible web -m file -a 'path=/data/app state=directory'
192.168.37.122 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/data/app",
"size": 6,
"state": "directory",
"uid": 0
}
192.168.37.133 | SUCCESS => {
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/data/app",
"size": 4096,
"state": "directory",
"uid": 0
}
② 创建链接文件
[root@server ~] ansible web -m file -a 'path=/data/bbb.jpg src=aaa.jpg state=link'
192.168.37.122 | SUCCESS => {
"changed": true,
"dest": "/data/bbb.jpg",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 7,
"src": "aaa.jpg",
"state": "link",
"uid": 0
}
192.168.37.133 | SUCCESS => {
"changed": true,
"dest": "/data/bbb.jpg",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 7,
"src": "aaa.jpg",
"state": "link",
"uid": 0
}
[root@server ~] ansible web -m file -a 'path=/root/dd.jpg src=/root/app1 state=hard'
172.17.0.3 | SUCCESS => {
"changed": true,
"dest": "/root/dd.jpg",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 5,
"src": "/root/app1",
"state": "hard",
"uid": 0
}
172.17.0.4 | SUCCESS => {
"changed": true,
"dest": "/root/dd.jpg",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 5,
"src": "/root/app1",
"state": "hard",
"uid": 0
}
③ 删除文件
[root@server ~] ansible web -m file -a 'path=/data/a state=absent'
192.168.37.122 | SUCCESS => {
"changed": true,
"path": "/data/a",
"state": "absent"
}
192.168.37.133 | SUCCESS => {
"changed": true,
"path": "/data/a",
"state": "absent"
}
6、fetch 模块
该模块用于从远程某主机获取(复制)文件到本地。
有两个选项:
dest
:用来存放文件的目录
src
:在 远程 拉取的文件,并且必须是一个file,不能是目录
[root@server ~] ansible web -m fetch -a 'src=/data/hello dest=/data'
192.168.37.122 | SUCCESS => {
"changed": true,
"checksum": "22596363b3de40b06f981fb85d82312e8c0ed511",
"dest": "/data/192.168.37.122/data/hello",
"md5sum": "6f5902ac237024bdd0c176cb93063dc4",
"remote_checksum": "22596363b3de40b06f981fb85d82312e8c0ed511",
"remote_md5sum": null
}
192.168.37.133 | SUCCESS => {
"changed": true,
"checksum": "22596363b3de40b06f981fb85d82312e8c0ed511",
"dest": "/data/192.168.37.133/data/hello",
"md5sum": "6f5902ac237024bdd0c176cb93063dc4",
"remote_checksum": "22596363b3de40b06f981fb85d82312e8c0ed511",
"remote_md5sum": null
}
7、cron 模块
该模块适用于管理cron
计划任务的。
其使用的语法跟我们的crontab
文件中的语法一致,同时,可以指定以下选项:
day=
# 日应该运行的工作( 1-31, , /2, )
hour=
# 小时 ( 0-23, , /2, )
minute=
# 分钟( 0-59, , /2, )
month=
# 月( 1-12, *, /2, )
weekday=
# 周 ( 0-6 for Sunday-Saturday,, )
job=
# 指明运行的命令是什么
name=
# 定时任务描述
reboot
# 任务在重启时运行,不建议使用,建议使用 special_time
special_time
# 特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
state
# 指定状态,present 表示添加定时任务,也是默认设置,absent 表示删除定时任务
user
# 以哪个用户的身份执行
① 添加计划任务
[root@server ~]# ansible web -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/sbin/ntpdate 172.17.0.1 &> /dev/null"'
192.168.37.122 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"ntp update every 5 min"
]
}
192.168.37.133 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"ntp update every 5 min"
]
}
然后执行删除操作:
[root@server ~]# ansible web -m cron -a 'name="df everyday" hour=15 job="df -lh >> /tmp/disk_total &> /dev/null" state=absent'
192.168.37.122 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"ntp update every 5 min"
]
}
192.168.37.133 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"ntp update every 5 min"
]
}
8、yum 模块
顾名思义,该模块主要用于软件的安装。
其选项如下:
name=
# 所安装的包的名称
state=
#present
--->安装,latest
--->安装最新的,absent
---> 卸载软件。
update_cache
# 强制更新yum的缓存
conf_file
# 指定远程yum安装时所依赖的配置文件(安装本地已有的包)。
disable_gpg_check
# 是否禁止GPG checking,只用于present
orlatest
。
disablerepo
# 临时禁止使用 yum 库。 只用于安装或更新时。
enablerepo
# 临时使用的 yum 库。只用于安装或更新时。
[root@server ~]# ansible web -m yum -a 'name=htop state=present'
192.168.37.122 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package htop.x86_64 0:2.0.2-1.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n htop x86_64 2.0.2-1.el7 epel 98 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 98 k\nInstalled size: 207 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : htop-2.0.2-1.el7.x86_64 1/1 \n Verifying : htop-2.0.2-1.el7.x86_64 1/1 \n\nInstalled:\n htop.x86_64 0:2.0.2-1.el7 \n\nComplete!\n"
]
}
9、service 模块
该模块用于服务程序的管理。
其主要选项如下:
arguments
# 命令行提供额外的参数
enabled
# 设置开机启动。
name=
# 服务名称
runlevel
# 开机启动的级别,一般不用指定。
sleep
# 在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)
state
# 有四种状态,分别为:started
--->启动服务,stopped
--->停止服务,restarted
--->重启服务,reloaded
--->重载配置
① 开启服务并设置自启动
[root@server ~]# ansible web -m service -a 'name=nginx state=started enabled=true'
192.168.37.122 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "nginx",
"state": "started",
……
}
192.168.37.133 | SUCCESS => {
"changed": true,
"enabled": true,
"name": "nginx",
"state": "started",
……
}
② 关闭服务
我们也可以通过该模块来关闭我们的服务:
[root@server ~]# ansible web -m service -a 'name=nginx state=stopped'
192.168.37.122 | SUCCESS => {
"changed": true,
"name": "nginx",
"state": "stopped",
……
}
192.168.37.133 | SUCCESS => {
"changed": true,
"name": "nginx",
"state": "stopped",
……
}
10、user 模块
该模块主要是用来管理用户账号。
其主要选项如下:
comment
# 用户的描述信息
createhome
# 是否创建家目录
force
# 在使用 state=absent 时, 行为与 userdel –force 一致.
group
# 指定基本组
groups
# 指定附加组,如果指定为(groups=)表示删除所有组
home
# 指定用户家目录
move_home
# 如果设置为home=时, 试图将用户主目录移动到指定的目录
name
# 指定用户名
non_unique
# 该选项允许改变非唯一的用户ID值
password
# 指定用户密码
remove
# 在使用 state=absent 时, 行为是与 userdel –remove 一致
shell
# 指定默认shell
state
# 设置帐号状态,不指定为创建,指定值为 absent 表示删除
system
# 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
uid
# 指定用户的uid
[root@server ~]# ansible web -m user -a 'name=keer uid=11111'
192.168.37.122 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 11111,
"home": "/home/keer",
"name": "keer",
"shell": "/bin/bash",
"state": "present",
"stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
"system": false,
"uid": 11111
}
② 删除用户
[root@server ~]# ansible web -m user -a 'name=keer state=absent'
192.168.37.122 | SUCCESS => {
"changed": true,
"force": false,
"name": "keer",
"remove": false,
"state": "absent"
}
192.168.37.133 | SUCCESS => {
"changed": true,
"force": false,
"name": "keer",
"remove": false,
"state": "absent"
}
11、group 模块
该模块主要用于添加或删除组。
常用的选项如下:
gid=
# 设置组的GID号
name=
# 指定组的名称
state=
# 指定组的状态,默认为创建,设置值为absent
为删除
system=
# 设置值为yes
,表示创建为系统组
① 创建组
[root@server ~]# ansible web -m group -a 'name=sanguo gid=12222'
192.168.37.122 | SUCCESS => {
"changed": true,
"gid": 12222,
"name": "sanguo",
"state": "present",
"system": false
}
192.168.37.133 | SUCCESS => {
"changed": true,
"gid": 12222,
"name": "sanguo",
"state": "present",
"system": false
}
12、script 模块
该模块用于将本机的脚本在被管理端的机器上运行。
该模块直接指定脚本的路径即可,我们通过例子来看一看到底如何使用的:
首先,我们写一个脚本,并给其加上执行权限:
[root@server ~]# vim /tmp/df.sh
#!/bin/bash
date >> /tmp/disk_total.log
df -lh >> /tmp/disk_total.log
[root@server ~]# chmod +x /tmp/df.sh
然后,我们直接运行命令来实现在被管理端执行该脚本:
[root@server ~]# ansible web -m script -a '/tmp/df.sh'
192.168.37.122 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.37.122 closed.\r\n",
"stdout": "",
"stdout_lines": []
}
192.168.37.133 | SUCCESS => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.37.133 closed.\r\n",
"stdout": "",
"stdout_lines": []
}
照例查看一下文件内容:
[root@server ~]# ansible web -m shell -a 'cat /tmp/disk_total.log'
192.168.37.122 | SUCCESS | rc=0 >>
Tue Dec 5 15:58:21 CST 2017
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 47G 4.4G 43G 10% /
devtmpfs 978M 0 978M 0% /dev
tmpfs 993M 84K 993M 1% /dev/shm
tmpfs 993M 9.1M 984M 1% /run
tmpfs 993M 0 993M 0% /sys/fs/cgroup
/dev/sda3 47G 33M 47G 1% /app
/dev/sda1 950M 153M 798M 17% /boot
tmpfs 199M 16K 199M 1% /run/user/42
tmpfs 199M 0 199M 0% /run/user/0
192.168.37.133 | SUCCESS | rc=0 >>
Tue Dec 5 15:58:21 CST 2017
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 46G 4.1G 40G 10% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 9.0M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda3 3.7G 15M 3.4G 1% /app
/dev/sda1 1.9G 141M 1.6G 9% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
可以看出已经执行成功了。
13、setup 模块
该模块主要用于收集信息,是通过调用facts组件来实现的。
facts组件是Ansible用于采集被管机器设备信息的一个功能,我们可以使用setup模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
facts就是变量,内建变量 。每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。
① 查看信息
我们可以直接用命令获取到变量的值,具体我们来看看例子:
[root@server ~]# ansible web -m setup -a 'filter="*mem*"' #查看内存
192.168.37.122 | SUCCESS => {
"ansible_facts": {
"ansible_memfree_mb": 1116,
"ansible_memory_mb": {
"nocache": {
"free": 1397,
"used": 587
},
"real": {
"free": 1116,
"total": 1984,
"used": 868
},
"swap": {
"cached": 0,
"free": 3813,
"total": 3813,
"used": 0
}
},
"ansible_memtotal_mb": 1984
},
"changed": false
}
192.168.37.133 | SUCCESS => {
"ansible_facts": {
"ansible_memfree_mb": 1203,
"ansible_memory_mb": {
"nocache": {
"free": 1470,
"used": 353
},
"real": {
"free": 1203,
"total": 1823,
"used": 620
},
"swap": {
"cached": 0,
"free": 3813,
"total": 3813,
"used": 0
}
},
"ansible_memtotal_mb": 1823
},
"changed": false
}
我们可以通过命令查看一下内存的大小以确认一下是否一致:
[root@server ~]# ansible web -m shell -a 'free -m'
192.168.37.122 | SUCCESS | rc=0 >>
total used free shared buff/cache available
Mem: 1984 404 1122 9 457 1346
Swap: 3813 0 3813
192.168.37.133 | SUCCESS | rc=0 >>
total used free shared buff/cache available
Mem: 1823 292 1207 9 323 1351
Swap: 3813 0 3813
可以看出信息是一致的。
② 保存信息
我们的setup模块还有一个很好用的功能就是可以保存我们所筛选的信息至我们的主机上,同时,文件名为我们被管制的主机的IP,这样方便我们知道是哪台机器出的问题。
我们可以看一看例子:
[root@server tmp]# ansible web -m setup -a 'filter="*mem*"' --tree /tmp/facts
192.168.37.122 | SUCCESS => {
"ansible_facts": {
"ansible_memfree_mb": 1115,
"ansible_memory_mb": {
"nocache": {
"free": 1396,
"used": 588
},
"real": {
"free": 1115,
"total": 1984,
"used": 869
},
"swap": {
"cached": 0,
"free": 3813,
"total": 3813,
"used": 0
}
},
"ansible_memtotal_mb": 1984
},
"changed": false
}
192.168.37.133 | SUCCESS => {
"ansible_facts": {
"ansible_memfree_mb": 1199,
"ansible_memory_mb": {
"nocache": {
"free": 1467,
"used": 356
},
"real": {
"free": 1199,
"total": 1823,
"used": 624
},
"swap": {
"cached": 0,
"free": 3813,
"total": 3813,
"used": 0
}
},
"ansible_memtotal_mb": 1823
},
"changed": false
}
然后我们可以去查看一下:
[root@server ~]# cd /tmp/facts/
[root@server facts]# ls
192.168.37.122 192.168.37.133
[root@server facts]# cat 192.168.37.122
{"ansible_facts": {"ansible_memfree_mb": 1115, "ansible_memory_mb": {"nocache": {"free": 1396, "used": 588}, "real": {"free": 1115, "total": 1984, "used": 869}, "swap": {"cached": 0, "free": 3813, "total": 3813, "used": 0}}, "ansible_memtotal_mb": 1984}, "changed": false}
网友评论