ansible 常用模块

Command 模块
功能:在远程主机执行命令,此为默认模块,可忽略-m选项
注意:此命令不支持 $VARNAME < > | ; & 等,用shell模块实现

Shell 模块
功能:和command相似,用shell执行命令
注意:调用bash执行命令 类似 cat /tmp/test.md | awk -F'|' '{print $1,$2}' &> /tmp/example.txt 这些
复杂命令,即使使用shell也可能会失败,解决办法:写到脚本时,copy到远程,执行,再把需要的结
果拉回执行命令的机器

范例:将shell模块代替command,设为模块
[root@ansible ~]#vim /etc/ansible/ansible.cfg
#修改下面一行
module_name = shell
范例
ansible webservers -a 'useradd haha' 默认模块可以不用加 -m 模块
ansible webservers -m shell -a 'useradd haha'

Script 模块
功能:在远程主机上运行ansible服务器上的脚本(无需执行权限
范例
ansible websrvs -m script -a '/root/1.sh'

Copy 模块
功能:从ansible服务器主控端复制文件到远程主机
#如目标存在,默认覆盖,此处指定先备份
ansible websrvs -m copy -a "src=/root/test1.sh dest=/tmp/test2.sh owner=wang
mode=600 backup=yes"
#指定内容,直接生成目标文件
ansible websrvs -m copy -a "content='test line1\ntest line2' dest=/tmp/test.txt"
#复制/etc目录自身,注意/etc/后面没有/
ansible websrvs -m copy -a "src=/etc dest=/backup"
#复制/etc/下的文件,不包括/etc/目录自身,注意/etc/后面有/
ansible websrvs -m copy -a "src=/etc/ dest=/backup"

Fetch 模块
功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录
范例:
ansible websrvs -m fetch -a 'src=/root/test.sh dest=/data/scripts'

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'

unarchive 模块
功能:解包解压缩
实现有两种用法:
1、将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes
2、将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no
常见参数:
copy:默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为
copy=no,会在远程主机上寻找src源文件
remote_src:和copy功能一样且互斥,yes表示在远程主机,不在ansible主机,no表示文件在
ansible主机上
src:源路径,可以是ansible主机上的路径,也可以是远程主机(被管理端或者第三方主机)上的路
径,如果是远程主机上的路径,则需要设置copy=no
dest:远程主机上的目标路径
mode:设置解压缩后的文件权限
范例
ansible all -m unarchive -a 'src=/data/foo.tgz dest=/var/lib/foo owner=wang group=bin'
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'

Archive 模块
功能:打包压缩保存在被管理节点
范例:
ansible websrvs -m archive -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2
owner=wang mode=0600'

Hostname 模块
功能:管理主机名
范例:
ansible node1 -m hostname -a "name=websrv"
ansible 10.0.0.18 -m hostname -a 'name=node18.magedu.com'

Cron 模块
功能:计划任务
支持时间:minute,hour,day,month,weekday
范例
#创建任务
ansible 10.0.0.8 -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup mysql"
job=/root/mysql_backup.sh'
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate ntp.aliyun.com
&>/dev/null' name=Synctime"
#禁用计划任务
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1
&>/dev/null' name=Synctime disabled=yes"
#启用计划任务
ansible websrvs -m cron -a "minute=*/5 job='/usr/sbin/ntpdate 172.20.0.1
&>/dev/null' name=Synctime disabled=no"
#删除任务
ansible websrvs -m cron -a "name='backup mysql' state=absent"
ansible websrvs -m cron -a 'state=absent name=Synctime'

Yum 模块
功能:管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本
范例:
ansible websrvs -m yum -a 'name=httpd state=present' #安装
ansible websrvs -m yum -a 'name=httpd state=absent' #删除
[root@ansible ~]#ansible websrvs -m yum -a 'name=iotop,cowsay' 一次安装多个包

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'

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'
#remove=yes表示删除用户及家目录等数据,默认remove=no
ansible all -m user -a 'name=nginx state=absent remove=yes'

Group 模块
功能:管理组
范例:
#创建组
ansible websrvs -m group -a 'name=nginx gid=88 system=yes'
#删除组
ansible websrvs -m group -a 'name=nginx state=absent'

Lineinfile 模块
ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可以方便的进行替换
功能:相当于sed,可以修改文件内容
范例:
ansible websrvs -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 80'"
ansible all -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=disabled'"
ansible all -m lineinfile -a 'dest=/etc/fstab state=absent regexp="^#"' 删除#开头的行

Replace 模块
该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用
范例:
ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'"
ansible all -m replace -a "path=/etc/fstab regexp='^#(.*)' replace='\1'"

Setup 模块
功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机
较多,会影响执行速度,可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信息
范例:
ansible_nodename #节点名字
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_nodename'
192.168.100.120 | SUCCESS => {
"ansible_facts": {
"ansible_nodename": "ansible"
},
"changed": false
}

ansible_fqdb  #fqdn名
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_fqdn'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_fqdn": "ansible" }, "changed": false}

ansible_hostname  #主机短名称
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_hostname'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_hostname": "ansible" }, "changed": false}

 
ansible_domain  #主机域名后缀
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_domain'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_domain": "" }, "changed": false}

ansible_memtotal_mb  #总物理内存
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_memtotal_mb'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_memtotal_mb": 976 }, "changed": false}

 
ansible_swaptotal_mb  #swap总大小
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_swaptotal_mb'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_swaptotal_mb": 2047 }, "changed": false}

ansible_processor  #cpu信息
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_processor'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_processor": [ "0", "GenuineIntel", "Intel(R) Core(TM) i7-3612QM CPU @ 2.10GHz" ] }, "changed": false}

 
ansible_process_cores  #cpu核心数量
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_processor_cores'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_processor_cores": 1 }, "changed": false}

ansible_processor_vcpus  #cpu逻辑核心数量
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_processor_vcpus'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_processor_vcpus": 1 }, "changed": false}

ansible_all_ipv4_addresses  #所有ipv4地址
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_all_ipv4_addresses'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.100.120" ] }, "changed": false}

ansible_all_ipv6_addresses  #所有ipv6地址
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_all_ipv6_addresses'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_all_ipv6_addresses": [ "fe80::e010:cb1a:204a:861d" ] }, "changed": false}

ansible_default_ipv4  #默认网关的网卡配置信息
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_default_ipv4'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_default_ipv4": { "address": "192.168.100.120", "alias": "ens33", "broadcast": "192.168.100.255", "gateway": "192.168.100.2", "interface": "ens33", "macaddress": "00:0c:29:73:fd:2e", "mtu": 1500, "netmask": "255.255.255.0", "network": "192.168.100.0", "type": "ether" } }, "changed": false}

ansible_ens33  #具体某张网卡信息
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_ens33'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_ens33": { "active": true, "device": "ens33", "features": { "busy_poll": "off [fixed]", "fcoe_mtu": "off [fixed]", "generic_receive_offload": "on", "generic_segmentation_offload": "on", "highdma": "off [fixed]", "hw_tc_offload": "off [fixed]", "l2_fwd_offload": "off [fixed]", "large_receive_offload": "off [fixed]", "loopback": "off [fixed]", "netns_local": "off [fixed]", "ntuple_filters": "off [fixed]", "receive_hashing": "off [fixed]", "rx_all": "off", "rx_checksumming": "off", "rx_fcs": "off", "rx_vlan_filter": "on [fixed]", "rx_vlan_offload": "on", "rx_vlan_stag_filter": "off [fixed]", "rx_vlan_stag_hw_parse": "off [fixed]", "scatter_gather": "on", "tcp_segmentation_offload": "on", "tx_checksum_fcoe_crc": "off [fixed]", "tx_checksum_ip_generic": "on", "tx_checksum_ipv4": "off [fixed]", "tx_checksum_ipv6": "off [fixed]", "tx_checksum_sctp": "off [fixed]", "tx_checksumming": "on", "tx_fcoe_segmentation": "off [fixed]", "tx_gre_csum_segmentation": "off [fixed]", "tx_gre_segmentation": "off [fixed]", "tx_gso_partial": "off [fixed]", "tx_gso_robust": "off [fixed]", "tx_ipip_segmentation": "off [fixed]", "tx_lockless": "off [fixed]", "tx_mpls_segmentation": "off [fixed]", "tx_nocache_copy": "off", "tx_scatter_gather": "on", "tx_scatter_gather_fraglist": "off [fixed]", "tx_sctp_segmentation": "off [fixed]", "tx_sit_segmentation": "off [fixed]", "tx_tcp6_segmentation": "off [fixed]", "tx_tcp_ecn_segmentation": "off [fixed]", "tx_tcp_mangleid_segmentation": "off", "tx_tcp_segmentation": "on", "tx_udp_tnl_csum_segmentation": "off [fixed]", "tx_udp_tnl_segmentation": "off [fixed]", "tx_vlan_offload": "on [fixed]", "tx_vlan_stag_hw_insert": "off [fixed]", "udp_fragmentation_offload": "off [fixed]", "vlan_challenged": "off [fixed]" }, "hw_timestamp_filters": [], "ipv4": { "address": "192.168.100.120", "broadcast": "192.168.100.255", "netmask": "255.255.255.0", "network": "192.168.100.0" }, "ipv6": [ { "address": "fe80::e010:cb1a:204a:861d", "prefix": "64", "scope": "link" } ], "macaddress": "00:0c:29:73:fd:2e", "module": "e1000", "mtu": 1500, "pciid": "0000:02:01.0", "promisc": false, "speed": 1000, "timestamping": [ "tx_software", "rx_software", "software" ], "type": "ether" } }, "changed": false}

ansible_dns  #网卡dns信息
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_dns'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_dns": { "nameservers": [ "192.168.100.2", "114.114.114.114" ] } }, "changed": false}

ansible_architecture  #系统架构
  x86_64

 
ansible_machine  #主机类型
  x86_64

 
ansible_kernel  #内核版本
  2.6.32-696.el6.x86_64

 
ansible_distribution  #发行版本
  centos

 
ansible_distribution_release  #发行版名称
  final

 
ansible_distribution_major_version  #操作系统主版本号
  6

 
ansible_distribution_release  #发行版本名称
  Final

 
ansible_distribution_version  #完整版本号
  7.4.1708

 
ansible_pkg_mgr  #软件包管理方式
  yum

ansible_service-mgr  #进行服务方式
  systemd

ansible_os_family  #家族系列
  RedHat

 
ansible_cmdline  #内核启动参数
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_cmdline'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_cmdline": { "BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64", "LANG": "zh_CN.UTF-8", "crashkernel": "auto", "quiet": true, "rd.lvm.lv": "centos/swap", "rhgb": true, "ro": true, "root": "/dev/mapper/centos-root" } }, "changed": false}

 
ansible_selinux  #SElinux状态
  disbled

ansible_env  #当前环境变量参数

 
ansible_data_time  #时间相关

 
ansible_python_version  #python版本
  2.7.5
ansible_lvm  #lvm卷相关信息

 
ansible_mounts  #所有挂载点
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_mounts'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_mounts": [ { "block_available": 4130338, "block_size": 4096, "block_total": 4452864, "block_used": 322526, "device": "/dev/mapper/centos-root", "fstype": "xfs", "inode_available": 8863332, "inode_total": 8910848, "inode_used": 47516, "mount": "/", "options": "rw,relatime,attr2,inode64,noquota", "size_available": 16917864448, "size_total": 18238930944, "uuid": "282c72d5-8cf6-4ac2-9e6a-64d19ebe2998" }, { "block_available": 227195, "block_size": 4096, "block_total": 259584, "block_used": 32389, "device": "/dev/sda1", "fstype": "xfs", "inode_available": 523961, "inode_total": 524288, "inode_used": 327, "mount": "/boot", "options": "rw,relatime,attr2,inode64,noquota", "size_available": 930590720, "size_total": 1063256064, "uuid": "700f5755-18e9-45b5-ab39-9c699eb0f636" } ] }, "changed": false}

ansible_device_links  #所有挂在的设备uuid和卷标名
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_device_links'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_device_links": { "ids": { "dm-0": [ "dm-name-centos-root", "dm-uuid-LVM-rdlock3VAHcB3dNwwyYLym2uGO9gVYLylUjawiCM9Ei01XfqxeDCwuFZGm5GKTN4" ], "dm-1": [ "dm-name-centos-swap", "dm-uuid-LVM-rdlock3VAHcB3dNwwyYLym2uGO9gVYLye5ByMZPoe3dex42T2VCoRo0C2Rd4riqf" ], "sda2": [ "lvm-pv-uuid-f3IVfS-XHtK-6UjN-ZyOj-s1GO-1NdX-ZIh8UN" ] }, "labels": {}, "masters": { "sda2": [ "dm-0", "dm-1" ] }, "uuids": { "dm-0": [ "282c72d5-8cf6-4ac2-9e6a-64d19ebe2998" ], "dm-1": [ "c351d02a-6ffa-41b1-b108-9129dbcac1a2" ], "sda1": [ "700f5755-18e9-45b5-ab39-9c699eb0f636" ] } } }, "changed": false}

ansible_devices  #所有/dev/下的正在使用的设备信息
[root@ansible ~]# ansible test -m setup -a 'filter=ansible_devices'192.168.100.120 | SUCCESS => { "ansible_facts": { "ansible_devices": { "dm-0": { "holders": [], "host": "", "links": { "ids": [ "dm-name-centos-root", "dm-uuid-LVM-rdlock3VAHcB3dNwwyYLym2uGO9gVYLylUjawiCM9Ei01XfqxeDCwuFZGm5GKTN4" ], "labels": [], "masters": [], "uuids": [ "282c72d5-8cf6-4ac2-9e6a-64d19ebe2998" ] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "35643392", "sectorsize": "512", "size": "17.00 GB", "support_discard": "0", "vendor": null, "virtual": 1 }, "dm-1": { "holders": [], "host": "", "links": { "ids": [ "dm-name-centos-swap", "dm-uuid-LVM-rdlock3VAHcB3dNwwyYLym2uGO9gVYLye5ByMZPoe3dex42T2VCoRo0C2Rd4riqf" ], "labels": [], "masters": [], "uuids": [ "c351d02a-6ffa-41b1-b108-9129dbcac1a2" ] }, "model": null, "partitions": {}, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "", "sectors": "4194304", "sectorsize": "512", "size": "2.00 GB", "support_discard": "0", "vendor": null, "virtual": 1 }, "sda": { "holders": [], "host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": "VMware Virtual S", "partitions": { "sda1": { "holders": [], "links": { "ids": [], "labels": [], "masters": [], "uuids": [ "700f5755-18e9-45b5-ab39-9c699eb0f636" ] }, "sectors": "2097152", "sectorsize": 512, "size": "1.00 GB", "start": "2048", "uuid": "700f5755-18e9-45b5-ab39-9c699eb0f636" }, "sda2": { "holders": [ "centos-root", "centos-swap" ], "links": { "ids": [ "lvm-pv-uuid-f3IVfS-XHtK-6UjN-ZyOj-s1GO-1NdX-ZIh8UN" ], "labels": [], "masters": [ "dm-0", "dm-1" ], "uuids": [] }, "sectors": "39843840", "sectorsize": 512, "size": "19.00 GB", "start": "2099200", "uuid": null } }, "removable": "0", "rotational": "1", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "deadline", "sectors": "41943040", "sectorsize": "512", "size": "20.00 GB", "support_discard": "0", "vendor": "VMware,", "virtual": 1 } } }, "changed": false}

ansible_user_dir  #执行用户的家目录
  root

 
ansible_user_gecos  #执行用户的描述信息
  the root

 
ansible_user_gid  #执行用户的gid
  0  

 
ansible_user_id  #执行用户的用户名
  root

ansible_user_shell  #执行用户的shell类型
  /bin/bash

 
ansible_user_uid  #执行用户的uid
  0

 

posted @ 2022-03-14 17:27  45645+56  阅读(469)  评论(0)    收藏  举报