实例学习ansible系列(7)常用模块之ping/setup

时间:2022-07-10 14:39:55

知识点:ping模块,用于确认和对象机器之间是否能够ping通,正常情况会返回pong
知识点:setup模块,用于收集对象机器的基本设定信息。

ping使用实例

[root@host31 ansible]# ansible host32 -m ping
host32 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@host31 ansible]#

setup使用实例

不用option的情况会输出所有相关的对象机器的facts

[root@host31 ~]# ansible host32 -m setup
host32 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.122.1",
"172.16.96.1",
"172.16.96.0",
"192.168.32.32"
],
"ansible_all_ipv6_addresses": [
"fe80::20:56ff:fe8a:59b",
"fe80::20c:29ff:febe:1cff"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "07/02/2015",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-327.el7.x86_64",
"LANG": "en_US.UTF-8",
"quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
"ansible_date_time": {
"date": "2016-07-30",
"day": "30",
"epoch": "1469884080",
"hour": "09",
"iso8601": "2016-07-30T13:08:00Z",
"iso8601_basic": "20160730T090800352254",
"iso8601_basic_short": "20160730T090800",
"iso8601_micro": "2016-07-30T13:08:00.352338Z",
"minute": "08",
"month": "07",
"second": "00",
"time": "09:08:00",
"tz": "EDT",
"tz_offset": "-0400",
"weekday": "Saturday",
"weekday_number": "6",
"weeknumber": "30",
"year": "2016"
},
"ansible_default_ipv4": {
"address": "192.168.32.32",
"alias": "eno16777736",
"broadcast": "192.168.32.255",
"gateway": "192.168.32.2",
"interface": "eno16777736",
"macaddress": "00:0c:29:be:1c:ff",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.32.0",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_devices": {
"sda": {
"holders": [],
"host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)",
"model": "VMware Virtual S",
"partitions": {
"sda1": {
"sectors": "1024000",
"sectorsize": 512,
"size": "500.00 MB",
"start": "2048"
},
"sda2": {
"sectors": "19945472",
"sectorsize": 512,
"size": "9.51 GB",
"start": "1026048"
}
},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "deadline",
"sectors": "20971520",
"sectorsize": "512",
"size": "10.00 GB",
"support_discard": "0",
"vendor": "VMware,"
},
"sr0": {
"holders": [],
"host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
"model": "VMware IDE CDR10",
"partitions": {},
"removable": "1",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "cfq",
"sectors": "2097151",
"sectorsize": "512",
"size": "1024.00 MB",
"support_discard": "0",
"vendor": "NECVMWar"
}
},
"ansible_distribution": "CentOS",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.2.1511",
"ansible_dns": {
"nameservers": [
"8.8.4.4"
]
},
"ansible_docker0": {
"active": false,
"device": "docker0",
"id": "8000.02425613a4da",
"interfaces": [],
"ipv4": {
"address": "172.16.96.1",
"broadcast": "global",
"netmask": "255.255.255.0",
"network": "172.16.96.0"
},
"macaddress": "02:42:56:13:a4:da",
"mtu": 1500,
"promisc": false,
"stp": false,
"type": "bridge"
},
"ansible_domain": "",
"ansible_eno16777736": {
"active": true,
"device": "eno16777736",
"ipv4": {
"address": "192.168.32.32",
"broadcast": "192.168.32.255",
"netmask": "255.255.255.0",
"network": "192.168.32.0"
},
"ipv6": [
{
"address": "fe80::20c:29ff:febe:1cff",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "00:0c:29:be:1c:ff",
"module": "e1000",
"mtu": 1500,
"pciid": "0000:02:01.0",
"promisc": false,
"type": "ether"
},
"ansible_env": {
"HOME": "/root",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US.UTF-8",
"LC_MESSAGES": "en_US.UTF-8",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"LOGNAME": "root",
"MAIL": "/var/mail/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
"PWD": "/root",
"PYTHONPATH": "",
"SELINUX_LEVEL_REQUESTED": "",
"SELINUX_ROLE_REQUESTED": "",
"SELINUX_USE_CURRENT_RANGE": "",
"SHELL": "/bin/bash",
"SHLVL": "2",
"SSH_CLIENT": "192.168.32.31 48444 22",
"SSH_CONNECTION": "192.168.32.31 48444 192.168.32.32 22",
"SSH_TTY": "/dev/pts/0",
"TERM": "xterm",
"USER": "root",
"XDG_RUNTIME_DIR": "/run/user/0",
"XDG_SESSION_ID": "150",
"_": "/usr/bin/python"
},
"ansible_fips": false,
"ansible_flannel.1": {
"active": true,
"device": "flannel.1",
"ipv4": {
"address": "172.16.96.0",
"broadcast": "global",
"netmask": "255.240.0.0",
"network": "172.16.0.0"
},
"ipv6": [
{
"address": "fe80::20:56ff:fe8a:59b",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "02:20:56:8a:05:9b",
"mtu": 1450,
"promisc": false,
"type": "ether"
},
"ansible_form_factor": "Other",
"ansible_fqdn": "host32",
"ansible_gather_subset": [
"hardware",
"network",
"virtual"
],
"ansible_hostname": "host32",
"ansible_interfaces": [
"docker0",
"lo",
"flannel.1",
"eno16777736",
"virbr0-nic",
"virbr0"
],
"ansible_kernel": "3.10.0-327.el7.x86_64",
"ansible_lo": {
"active": true,
"device": "lo",
"ipv4": {
"address": "127.0.0.1",
"broadcast": "host",
"netmask": "255.0.0.0",
"network": "127.0.0.0"
},
"ipv6": [
{
"address": "::1",
"prefix": "128",
"scope": "host"
}
],
"mtu": 65536,
"promisc": false,
"type": "loopback"
},
"ansible_lvm": {
"lvs": {
"docker-poolmeta": {
"size_g": "0.01",
"vg": "centos"
},
"root": {
"size_g": "8.47",
"vg": "centos"
},
"swap": {
"size_g": "1.00",
"vg": "centos"
}
},
"vgs": {
"centos": {
"free_g": "0.03",
"num_lvs": "3",
"num_pvs": "1",
"size_g": "9.51"
}
}
},
"ansible_machine": "x86_64",
"ansible_machine_id": "c7e3ea1209604e87aaad01ff134e9e3b",
"ansible_memfree_mb": 111,
"ansible_memory_mb": {
"nocache": {
"free": 1103,
"used": 882
},
"real": {
"free": 111,
"total": 1985,
"used": 1874
},
"swap": {
"cached": 0,
"free": 1023,
"total": 1023,
"used": 0
}
},
"ansible_memtotal_mb": 1985,
"ansible_mounts": [
{
"device": "/dev/mapper/centos-root",
"fstype": "xfs",
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 4573728768,
"size_total": 9082765312,
"uuid": "badd077b-0a3e-40ee-99f7-dab08665cee5"
},
{
"device": "/dev/sda1",
"fstype": "xfs",
"mount": "/boot",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 374276096,
"size_total": 520794112,
"uuid": "15d4410f-795d-4e37-8731-1d104b525b3f"
}
],
"ansible_nodename": "host32",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
"ansible_processor": [
"GenuineIntel",
"Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 1,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 1,
"ansible_product_name": "VMware Virtual Platform",
"ansible_product_serial": "VMware-56 4d d6 c3 21 f3 9c bf-0b 6b e1 a7 ee be 1c ff",
"ansible_product_uuid": "564DD6C3-21F3-9CBF-0B6B-E1A7EEBE1CFF",
"ansible_product_version": "None",
"ansible_python": {
"executable": "/usr/bin/python",
"has_sslcontext": true,
"type": "CPython",
"version": {
"major": 2,
"micro": 5,
"minor": 7,
"releaselevel": "final",
"serial": 0
},
"version_info": [
2,
7,
5,
"final",
0
]
},
"ansible_python_version": "2.7.5",
"ansible_selinux": {
"config_mode": "permissive",
"mode": "permissive",
"policyvers": 28,
"status": "enabled",
"type": "targeted"
},
"ansible_service_mgr": "systemd",
"ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGSR3DThz2RLoPAZKtY6jIFJun355SHQlrY3ATc4nEmERiscRviOWZaLRL1d6NwGZxFAIrpHqQ8tYr2VGZIcYPk=",
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIBkXu5pAOmK96YZJmws0QruTPUtf8CAaUZ0d5iPp6C1Q",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDjiG6UaDQakAGpi0RMarJwdzv6XBayhLFL0+uwzqrS0LdC4PMyoqU9rQQrpugXht0JKCbRs/pi4/bNt1x3YrWjkpIGUE88jgq9JSnzPkcWh1ffpCFih7Tr4PZkVp2NeeyuJXgvxiXVw34ZpZ8Kcl0wOAyyYGJNbIS38zAl3SmFTbrEEZCQhXxCA/m5GUYkjnoJxXezeE3uAdQ5vVU/k20tfPEpEeYR9RPaCgEUteHh9fKIHD8f4kXaiD3vT2tG9JHU35V3vI/EkJLtKBsUAXDgUaydOYrmn9xJb92kWFMTug5aBllwuf5O1Ws98xOzPANp4EWUrbAzLf/MFhzxBIwH",
"ansible_swapfree_mb": 1023,
"ansible_swaptotal_mb": 1023,
"ansible_system": "Linux",
"ansible_system_capabilities": [
"cap_chown",
"cap_dac_override",
"cap_dac_read_search",
"cap_fowner",
"cap_fsetid",
"cap_kill",
"cap_setgid",
"cap_setuid",
"cap_setpcap",
"cap_linux_immutable",
"cap_net_bind_service",
"cap_net_broadcast",
"cap_net_admin",
"cap_net_raw",
"cap_ipc_lock",
"cap_ipc_owner",
"cap_sys_module",
"cap_sys_rawio",
"cap_sys_chroot",
"cap_sys_ptrace",
"cap_sys_pacct",
"cap_sys_admin",
"cap_sys_boot",
"cap_sys_nice",
"cap_sys_resource",
"cap_sys_time",
"cap_sys_tty_config",
"cap_mknod",
"cap_lease",
"cap_audit_write",
"cap_audit_control",
"cap_setfcap",
"cap_mac_override",
"cap_mac_admin",
"cap_syslog",
"35",
"36+ep"
],
"ansible_system_capabilities_enforced": "True",
"ansible_system_vendor": "VMware, Inc.",
"ansible_uptime_seconds": 56290,
"ansible_user_dir": "/root",
"ansible_user_gecos": "root",
"ansible_user_gid": 0,
"ansible_user_id": "root",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 0,
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virbr0": {
"active": false,
"device": "virbr0",
"id": "8000.000000000000",
"interfaces": [],
"ipv4": {
"address": "192.168.122.1",
"broadcast": "192.168.122.255",
"netmask": "255.255.255.0",
"network": "192.168.122.0"
},
"mtu": 1500,
"promisc": false,
"stp": true,
"type": "bridge"
},
"ansible_virbr0_nic": {
"active": false,
"device": "virbr0-nic",
"macaddress": "52:54:00:6b:d9:6c",
"mtu": 1500,
"promisc": false,
"type": "ether"
},
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "VMware",
"module_setup": true
},
"changed": false
}
[root@host31 ~]#

setup常用Option:filter
比如收集对象机器的环境变量信息

[root@host31 ~]# ansible host32 -m setup -a "filter=ansible_env"
host32 | SUCCESS => {
"ansible_facts": {
"ansible_env": {
"HOME": "/root",
"LANG": "en_US.UTF-8",
"LC_ALL": "en_US.UTF-8",
"LC_MESSAGES": "en_US.UTF-8",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"LOGNAME": "root",
"MAIL": "/var/mail/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
"PWD": "/root",
"PYTHONPATH": "",
"SELINUX_LEVEL_REQUESTED": "",
"SELINUX_ROLE_REQUESTED": "",
"SELINUX_USE_CURRENT_RANGE": "",
"SHELL": "/bin/bash",
"SHLVL": "2",
"SSH_CLIENT": "192.168.32.31 48482 22",
"SSH_CONNECTION": "192.168.32.31 48482 192.168.32.32 22",
"SSH_TTY": "/dev/pts/0",
"TERM": "xterm",
"USER": "root",
"XDG_RUNTIME_DIR": "/run/user/0",
"XDG_SESSION_ID": "151",
"_": "/usr/bin/python"
}
},
"changed": false
}
[root@host31 ~]#