- Command 模块
功能:在远程主机执行命令,此为默认模块,可忽略-m选项
ansible nodes -m command -a 'ip a'
192.168.148.132 | CHANGED | rc=0 >>
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d9:f2:15 brd ff:ff:ff:ff:ff:ff
inet 192.168.148.132/24 brd 192.168.148.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fed9:f215/64 scope link
valid_lft forever preferred_lft forever
192.168.148.131 | CHANGED | rc=0 >>
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:87:cf:aa brd ff:ff:ff:ff:ff:ff
inet 192.168.148.131/24 brd 192.168.148.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe87:cfaa/64 scope link
valid_lft forever preferred_lft forever
- shell 模块
功能:和command相似,用shell执行命令
ansible nodes -m shell -a 'ip a > ipinfo.txt'
192.168.148.132 | CHANGED | rc=0 >>
192.168.148.131 | CHANGED | rc=0 >>
- copy 模块
功能:从ansible服务器主控端复制文件到远程主机
#如目标存在,默认覆盖,此处指定先备份 ansible all -m copy -a “src=/root/test1.sh dest=/tmp/test2.sh owner=wang mode=600 backup=yes”
#指定内容,直接生成目标文件ansible all -m copy -a “content='test content\n' dest=/tmp/test.txt”
#复制/etc/下的文件,不包括/etc/目录自身 ansible all -m copy -a “src=/etc/ dest=/backup”
ansible nodes -m copy -a "src=/root/test.sh dest=/root/shells.sh"
192.168.148.131 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": true,
"checksum": "a52f428871290a87f204e9ffdadfead193cb43b0",
"dest": "/root/shells.sh",
"gid": 0,
"group": "root",
"md5sum": "5256c4f74e339a13ad7efe9053a174c8",
"mode": "0644",
"owner": "root",
"size": 8,
"src": "/root/.ansible/tmp/ansible-tmp-1587196914.5798655-36196-34761269522110/source",
"state": "file",
"uid": 0
}
4.script 模块
在远程主机上运行ansible服务器上的脚本
ansible nodes -m script -a shellscript.sh
192.168.148.131 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.148.131 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.148.131 closed."
],
"stdout": "node1 /root/shellscript.sh\r\nnode1\r\n",
"stdout_lines": [
"node1 /root/shellscript.sh",
"node1"
]
}
5.fetch 模块
fetch 模块可以将远程服务器的文件复制到本地 ansible 主机中
ansible nodes -m fetch -a "src=/root/shells.sh dest=/root/newshells.sh"
192.168.148.132 | CHANGED => {
"changed": true,
"checksum": "47eeaf57134572c8ed5dc0c8f90b784248bd7e3a",
"dest": "/root/newshells.sh/192.168.148.132/root/shells.sh",
"md5sum": "299e3a687252f3020cef20d0da880d2e",
"remote_checksum": "47eeaf57134572c8ed5dc0c8f90b784248bd7e3a",
"remote_md5sum": null
}
- file 模块
设置文件属性
#创建空文件 ansible all -m file -a 'path=/data/test.txt state=touch'
ansible all -m file -a 'path=/data/test.txt state=absent'
ansible all -m file -a "path=/root/test.sh owner=wang mode=755“ #创建目录 ansible all -m file -a "path=/data/mysql state=directory owner=mysql group=mysql"
#创建软链接 ansible all -m file -a ‘src=/data/testfile dest=/data/testfile-link state=link’
ansible nodes -m file -a "path=/root/shells.sh mode=777"
192.168.148.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"path": "/root/shells.sh",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 65,
"state": "file",
"uid": 0
}
- unarchive模块
功能:解包解压缩
1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes
2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no
ansible all -m unarchive -a 'src=/data/foo.tgz dest=/var/lib/foo'
ansible all -m unarchive -a 'src=/tmp/foo.zip dest=/data copy=no mode=0777'
ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'
ansible all -m unarchive -a "src=/root/code.tar.gz copy=yes dest=/root"
192.168.148.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/root",
"extract_results": {
"cmd": [
"/usr/bin/gtar",
"--extract",
"-C",
"/root",
"-z",
"-f",
"/root/.ansible/tmp/ansible-tmp-1563364600.86-262215652491407/source"
],
"err": "",
"out": "",
"rc": 0
},
"gid": 0,
"group": "root",
"handler": "TgzArchive",
"mode": "0550",
"owner": "root",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 232,
"src": "/root/.ansible/tmp/ansible-tmp-1563364600.86-262215652491407/source",
"state": "directory",
"uid": 0
}
- Archive模块
功能:打包压缩
ansible all -m archive -a "path=/etc/yum.repos.d dest=/root/yum.tar.gz format=gz mode=0777"
192.168.148.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"archived": [
"/etc/yum.repos.d/CentOS-Base.repo",
"/etc/yum.repos.d/CentOS-CR.repo",
"/etc/yum.repos.d/CentOS-Debuginfo.repo",
"/etc/yum.repos.d/CentOS-Media.repo",
"/etc/yum.repos.d/CentOS-Sources.repo",
"/etc/yum.repos.d/CentOS-Vault.repo",
"/etc/yum.repos.d/CentOS-fasttrack.repo"
],
"arcroot": "/etc/",
"changed": true,
"dest": "/root/yum.tar.gz",
"expanded_exclude_paths": [],
"expanded_paths": [
"/etc/yum.repos.d"
],
"gid": 0,
"group": "root",
"missing": [],
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 2354,
"state": "file",
"uid": 0
}
- Hostname模块
功能:管理主机名
ansible nodes -m hostname -a "name=node01"
192.168.148.132 | CHANGED => {
"ansible_facts": {
"ansible_domain": "",
"ansible_fqdn": "node01",
"ansible_hostname": "node01",
"ansible_nodename": "node01",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "node01"
}
- Cron模块
功能:计划任务 支持时间:minute,hour,day,month,weekday
ansible nodes -m cron -a "minute=*/2 job='/usr/bin/date >> /root/time.txt' name=crontest"
192.168.148.132 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"crontest"
]
}
- Yum Apt模块
功能:管理软件包
ansible all -m yum -a ‘name=httpd state=present’ #安装
ansible all -m yum -a ‘name=httpd state=absent’ #删除
- Service模块
功能:管理服务
ansible all -m service -a 'name=httpd state=started enabled=yes'
ansible all -m service -a 'name=httpd state=stopped'
ansible all -m service -a 'name=httpd state=reloaded’
ansible all -m shell -a "sed -i 's/^Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf"
ansible all -m service -a 'name=httpd state=restarted'
13.User模块
功能:管理用户
ansible all -m user -a 'name=user1 comment=“test user” uid=2048 home=/app/user1 group=root‘
ansible all -m user -a 'name=nginx comment=nginx uid=88 group=nginx groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nginx non_unique=yes'
#删除用户及家目录等数据 ansible all -m user -a 'name=nginx state=absent remove=yes'
- Group模块
功能:管理组
#创建组 ansible weballs -m group -a 'name=nginx gid=88 system=yes'
#删除组 ansible weballs -m group -a 'name=nginx state=absent'
- Setup模块
功能:返回系统状态信息
ansible all -m setup
ansible all -m setup -a "filter=ansible_nodename"
ansible all -m setup -a "filter=ansible_hostname"
ansible all -m setup -a "filter=ansible_domain"
ansible all -m setup -a "filter=ansible_memtotal_mb"
ansible all -m setup -a "filter=ansible_memory_mb"
ansible all -m setup -a "filter=ansible_memfree_mb"
ansible all -m setup -a "filter=ansible_os_family"
ansible all -m setup -a "filter=ansible_distribution_major_version"
ansible all -m setup -a "filter=ansible_distribution_version"
ansible all -m setup -a "filter=ansible_processor_vcpus"
ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
ansible all -m setup -a "filter=ansible_architecture