使用 ansible-doc [模块名字]
查看模块帮助信息也可以访问官方文档查看
官方模块文档
command 执行命令(默认)
command模块执行的命令不能有管道,但shell模块可以
[root@Ansible ~]# ansible hosts -m command -a "hostnamectl"
localhost | CHANGED | rc=0 >>
Static hostname: Ansible
Icon name: computer-vm
Chassis: vm
Machine ID: 611eb9146733482c813dee0118e98c2e
Boot ID: 842cefc3079845d0948229dd97ca0c56
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-862.3.2.el7.x86_64
Architecture: x86-64
nfs1 | CHANGED | rc=0 >>
Static hostname: Client2
Icon name: computer-vm
Chassis: vm
Machine ID: 611eb9146733482c813dee0118e98c2e
Boot ID: 3dd31b653cf6496aba7d59e0705889e0
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-862.3.2.el7.x86_64
Architecture: x86-64
web1 | CHANGED | rc=0 >>
Static hostname: Client1
Icon name: computer-vm
Chassis: vm
Machine ID: 611eb9146733482c813dee0118e98c2e
Boot ID: f2d91614d04a4ae0b8ef94136738b70a
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-862.3.2.el7.x86_64
Architecture: x86-64
shell 执行命令
执行任何 shell 命令,可以使用管道,但对一些复杂的命令太不支持,比如
awk
[root@Ansible ~]# ansible hosts -m shell -a "ifconfig eth0|grep 'inet'"
localhost | CHANGED | rc=0 >>
inet 192.168.1.1 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::862b:ec1d:da56:b3f5 prefixlen 64 scopeid 0x20<link>
web1 | CHANGED | rc=0 >>
inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::fe0a:ef25:6dce:b81a prefixlen 64 scopeid 0x20<link>
nfs1 | CHANGED | rc=0 >>
inet 192.168.1.3 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::fe0a:ef25:6dce:b81a prefixlen 64 scopeid 0x20<link>
inet6 fe80::1a2a:995:6f5a:ffe8 prefixlen 64 scopeid 0x20<link>
## 不太支持awk
[root@Ansible ~]# ansible hosts -m shell -a "df -h|awk '//$/{print $5}'"
localhost | CHANGED | rc=0 >>
/dev/mapper/centos-root 17G 3.0G 14G 18% /
web1 | CHANGED | rc=0 >>
/dev/mapper/centos-root 17G 3.1G 14G 18% /
nfs1 | CHANGED | rc=0 >>
/dev/mapper/centos-root 17G 3.1G 14G 18% /
get_url 联网下载
- url ---文件在网络上的具体位置
- dest ---下载到被控端的哪个目录下
- checksum ---校验(md5 sha256)
## 添加阿里云的base仓库
[root@Ansible ~]# ansible web -m get_url -a "url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo"
web1 | CHANGED => {
"changed": true,
"checksum_dest": null,
"checksum_src": "a149656624ecdf9c5549bf419925b1d8adddefb6",
"dest": "/etc/yum.repos.d/CentOS-Base.repo",
"gid": 0,
"group": "root",
"md5sum": "13151789a512213f1695a5b427b1a9ab",
"mode": "0644",
"msg": "OK (2523 bytes)",
"owner": "root",
"size": 2523,
"src": "/root/.ansible/tmp/ansible-tmp-1557748542.57-71249069620018/tmpFjRxR8",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "http://mirrors.aliyun.com/repo/Centos-7.repo"
}
## 添加阿里云的epel仓库
[root@Ansible ~]# ansible web -m get_url -a "url=http://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel.repo"
web1 | CHANGED => {
"changed": true,
"checksum_dest": null,
"checksum_src": "2feedd589b72617f03d75c4b8a6e328cc1aad918",
"dest": "/etc/yum.repos.d/epel.repo",
"gid": 0,
"group": "root",
"md5sum": "bddf35db56cf6be9190fdabeae71c801",
"mode": "0644",
"msg": "OK (664 bytes)",
"owner": "root",
"size": 664,
"src": "/root/.ansible/tmp/ansible-tmp-1557748737.47-206113667370211/tmpEBlLWk",
"state": "file",
"status_code": 200,
"uid": 0,
"url": "http://mirrors.aliyun.com/repo/epel-7.repo"
}
## 生成yum缓存
[root@Ansible ~]# ansible web -m command -a "yum makecache"
yum 安装软件模块
- name ---软件包名称
- state ---指定使用yum的方法
- installed,present ---安装软件包
- removed,absent ---移除软件包
- latest ---安装最新软件包
## 安装httpd服务(显示有点乱,主要是所有换行都显示
)
[root@Ansible ~]# ansible hosts -m yum -a "name=httpd state=installed"
nfs1 | CHANGED => {
"ansible_facts": {
"pkg_mgr": "yum"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.6-89.el7.centos updates 2.7 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 2.7 M
Installed size: 9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : httpd-2.4.6-89.el7.centos.x86_64 1/1
Verifying : httpd-2.4.6-89.el7.centos.x86_64 1/1
Installed:
httpd.x86_64 0:2.4.6-89.el7.centos
Complete!
"
]
}
web1 | CHANGED => {
"ansible_facts": {
"pkg_mgr": "yum"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.6-89.el7.centos updates 2.7 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 2.7 M
Installed size: 9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : httpd-2.4.6-89.el7.centos.x86_64 1/1
Verifying : httpd-2.4.6-89.el7.centos.x86_64 1/1
Installed:
httpd.x86_64 0:2.4.6-89.el7.centos
Complete!
"
]
}
localhost | CHANGED => {
"ansible_facts": {
"pkg_mgr": "yum"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.6-89.el7.centos updates 2.7 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 2.7 M
Installed size: 9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : httpd-2.4.6-89.el7.centos.x86_64 1/1
Verifying : httpd-2.4.6-89.el7.centos.x86_64 1/1
Installed:
httpd.x86_64 0:2.4.6-89.el7.centos
Complete!
"
]
}
## 卸载httpd服务(显示有点乱,主要是所有换行都显示
)
[root@Ansible ~]# ansible hosts -m yum -a "name=httpd state=removed"
nfs1 | CHANGED => {
"ansible_facts": {
"pkg_mgr": "yum"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-89.el7.centos 将被 删除
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在删除:
httpd x86_64 2.4.6-89.el7.centos @updates 9.4 M
事务概要
================================================================================
移除 1 软件包
安装大小:9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在删除 : httpd-2.4.6-89.el7.centos.x86_64 1/1
验证中 : httpd-2.4.6-89.el7.centos.x86_64 1/1
删除:
httpd.x86_64 0:2.4.6-89.el7.centos
完毕!
"
]
}
web1 | CHANGED => {
"ansible_facts": {
"pkg_mgr": "yum"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-89.el7.centos 将被 删除
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在删除:
httpd x86_64 2.4.6-89.el7.centos @updates 9.4 M
事务概要
================================================================================
移除 1 软件包
安装大小:9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在删除 : httpd-2.4.6-89.el7.centos.x86_64 1/1
验证中 : httpd-2.4.6-89.el7.centos.x86_64 1/1
删除:
httpd.x86_64 0:2.4.6-89.el7.centos
完毕!
"
]
}
localhost | CHANGED => {
"ansible_facts": {
"pkg_mgr": "yum"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-89.el7.centos 将被 删除
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在删除:
httpd x86_64 2.4.6-89.el7.centos @updates 9.4 M
事务概要
================================================================================
移除 1 软件包
安装大小:9.4 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在删除 : httpd-2.4.6-89.el7.centos.x86_64 1/1
验证中 : httpd-2.4.6-89.el7.centos.x86_64 1/1
删除:
httpd.x86_64 0:2.4.6-89.el7.centos
完毕!
"
]
}
copy 配置模块
- src --- 推送数据的源文件信息
- dest --- 推送数据的目标路径
- backup --- 对原来的文件,会放到
/var/lib/docker/overlay2/
目录下 - content --- 直接批量在被管理端文件中添加内容
- group --- 将本地文件推送到远端,指定文件属组信息
- owner --- 将本地文件推送到远端,指定文件属主信息
- mode --- 将本地文件推送到远端,指定文件权限信息
## 把/etc/hosts文件推送到所有主机/tmp/test.txt
[root@Ansible ~]# ansible hosts -m copy -a "src=/etc/hosts dest=/tmp/test.txt owner=www group=www mode=644"
localhost | FAILED! => {
"changed": false,
"checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
"gid": 0,
"group": "root",
"mode": "0644",
"msg": "chown failed: failed to look up user www",
"owner": "root",
"path": "/tmp/test.txt",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 158,
"state": "file",
"uid": 0
}
web1 | FAILED! => {
"changed": false,
"checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
"gid": 0,
"group": "root",
"mode": "0644",
"msg": "chown failed: failed to look up user www",
"owner": "root",
"path": "/tmp/test.txt",
"size": 158,
"state": "file",
"uid": 0
}
nfs1 | FAILED! => {
"changed": false,
"checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
"gid": 0,
"group": "root",
"mode": "0644",
"msg": "chown failed: failed to look up user www",
"owner": "root",
"path": "/tmp/test.txt",
"size": 158,
"state": "file",
"uid": 0
}
## 被覆盖的文件会备份到/var/lib/docker/overlay2/
[root@Ansible ~]# [root@Ansible ~]# ansible hosts -m copy -a "src=/etc/hosts dest=/etc/hosts backup=yes"
-bash: [root@Ansible: 未找到命令
[root@Ansible ~]# ansible hosts -m copy -a "src=/etc/hosts dest=/etc/hosts backup=yes"
localhost | SUCCESS => {
"changed": false,
"checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
"dest": "/etc/hosts",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/etc/hosts",
"secontext": "system_u:object_r:net_conf_t:s0",
"size": 158,
"state": "file",
"uid": 0
}
web1 | CHANGED => {
"backup_file": "/etc/hosts.4087.2019-05-12@20:24:56~",
"changed": true,
"checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
"dest": "/etc/hosts",
"gid": 0,
"group": "root",
"md5sum": "54fb6627dbaa37721048e4549db3224d",
"mode": "0644",
"owner": "root",
"size": 158,
"src": "/root/.ansible/tmp/ansible-tmp-1557663893.54-81828959416663/source",
"state": "file",
"uid": 0
}
nfs1 | CHANGED => {
"backup_file": "/etc/hosts.3795.2019-05-12@20:24:56~",
"changed": true,
"checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
"dest": "/etc/hosts",
"gid": 0,
"group": "root",
"md5sum": "54fb6627dbaa37721048e4549db3224d",
"mode": "0644",
"owner": "root",
"size": 158,
"src": "/root/.ansible/tmp/ansible-tmp-1557663893.44-149096126702721/source",
"state": "file",
"uid": 0
}
## 直接向远端文件内写入数据信息,并且会覆盖远端文件内原有数据信息
[root@Ansible ~]# ansible hosts -m copy -a "content='###songguoyou' dest=/etc/hosts"
localhost | CHANGED => {
"changed": true,
"checksum": "4c42e5babe87ef18dc6edb66c4ce4c03dd8908e7",
"dest": "/etc/hosts",
"gid": 0,
"group": "root",
"md5sum": "12c74c48e83ac492c160f647d64a91e5",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:net_conf_t:s0",
"size": 13,
"src": "/root/.ansible/tmp/ansible-tmp-1557664064.75-189921694361228/source",
"state": "file",
"uid": 0
}
web1 | CHANGED => {
"changed": true,
"checksum": "4c42e5babe87ef18dc6edb66c4ce4c03dd8908e7",
"dest": "/etc/hosts",
"gid": 0,
"group": "root",
"md5sum": "12c74c48e83ac492c160f647d64a91e5",
"mode": "0644",
"owner": "root",
"size": 13,
"src": "/root/.ansible/tmp/ansible-tmp-1557664064.74-223362295723281/source",
"state": "file",
"uid": 0
}
nfs1 | CHANGED => {
"changed": true,
"checksum": "4c42e5babe87ef18dc6edb66c4ce4c03dd8908e7",
"dest": "/etc/hosts",
"gid": 0,
"group": "root",
"md5sum": "12c74c48e83ac492c160f647d64a91e5",
"mode": "0644",
"owner": "root",
"size": 13,
"src": "/root/.ansible/tmp/ansible-tmp-1557664064.79-185916316363979/source",
"state": "file",
"uid": 0
}