【Ansible】 Ansible 模块 setup 与 stat 模块用法

setup模块

1 通过setup模块获取主机信息

# 执行命令 

ansible webserver -m setup 

# 返回信息

192.168.138.137 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.138.137"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::72c6:bd9a:7a8d:9650"
        ], 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "07/02/2015", 
        "ansible_bios_version": "6.00", 
        "ansible_cmdline": {
            "BOOT_IMAGE": "/boot/vmlinuz-4.15.0-96-generic", 
            "auto": true, 
            "find_preseed": "/preseed.cfg", 
            "locale": "en_US", 
            "noprompt": true, 
            "priority": "critical", 
            "quiet": true, 
            "ro": true, 
            "root": "UUID=0979b935-32fa-41b6-9179-6ce0a798388e"
        }, 
        "ansible_date_time": {
            "date": "2020-04-22", 
            "day": "22", 
            "epoch": "1587557888", 
            "hour": "20", 
            "iso8601": "2020-04-22T12:18:08Z", 
            "iso8601_basic": "20200422T201808601498", 
            "iso8601_basic_short": "20200422T201808", 
            "iso8601_micro": "2020-04-22T12:18:08.601578Z", 
            "minute": "18", 
            "month": "04", 
            "second": "08", 
            "time": "20:18:08", 
            "tz": "CST", 
            "tz_offset": "+0800", 
            "weekday": "星期三", 
            "weekday_number": "3", 
            "weeknumber": "16", 
            "year": "2020"
        }, 
        "ansible_default_ipv4": {
            "address": "192.168.138.137", 
            "alias": "ens33", 
            "broadcast": "192.168.138.255", 
            "gateway": "192.168.138.2", 
            "interface": "ens33", 
            "macaddress": "00:0c:29:45:3b:39", 
            "mtu": 1500, 
            "netmask": "255.255.255.0", 
            "network": "192.168.138.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": "39843840", 
                        "sectorsize": 512, 
                        "size": "19.00 GB", 
                        "start": "2048"
                    }, 
                    "sda2": {
                        "sectors": "2", 
                        "sectorsize": 512, 
                        "size": "1.00 KB", 
                        "start": "39847934"
                    }, 
                    "sda5": {
                        "sectors": "2093056", 
                        "sectorsize": 512, 
                        "size": "1022.00 MB", 
                        "start": "39847936"
                    }
                }, 
                "removable": "0", 
                "rotational": "1", 
                "scheduler_mode": "cfq", 
                "sectors": "41943040", 
                "sectorsize": "512", 
                "size": "20.00 GB", 
                "support_discard": "0", 
                "vendor": "VMware,"
            }, 
            "sr0": {
                "holders": [], 
                "host": "SATA controller: VMware SATA AHCI controller", 
                "model": "VMware SATA CD01", 
                "partitions": {}, 
                "removable": "1", 
                "rotational": "1", 
                "scheduler_mode": "cfq", 
                "sectors": "2097151", 
                "sectorsize": "512", 
                "size": "1024.00 MB", 
                "support_discard": "0", 
                "vendor": "NECVMWar"
            }
        }, 
        "ansible_distribution": "Ubuntu", 
        "ansible_distribution_major_version": "16", 
        "ansible_distribution_release": "xenial", 
        "ansible_distribution_version": "16.04", 
        "ansible_dns": {
            "nameservers": [
                "127.0.1.1"
            ], 
            "search": [
                "localdomain"
            ]
        }, 
        "ansible_domain": "", 
        "ansible_ens33": {
            "active": true, 
            "device": "ens33", 
            "ipv4": {
                "address": "192.168.138.137", 
                "broadcast": "192.168.138.255", 
                "netmask": "255.255.255.0", 
                "network": "192.168.138.0"
            }, 
            "ipv6": [
                {
                    "address": "fe80::72c6:bd9a:7a8d:9650", 
                    "prefix": "64", 
                    "scope": "link"
                }
            ], 
            "macaddress": "00:0c:29:45:3b:39", 
            "module": "e1000", 
            "mtu": 1500, 
            "pciid": "0000:02:01.0", 
            "promisc": false, 
            "type": "ether"
        }, 
        "ansible_env": {
            "HOME": "/root", 
            "LANG": "zh_CN.UTF-8", 
            "LANGUAGE": "zh_CN:en_US:en", 
            "LC_ADDRESS": "zh_CN.UTF-8", 
            "LC_ALL": "zh_CN.UTF-8", 
            "LC_IDENTIFICATION": "zh_CN.UTF-8", 
            "LC_MEASUREMENT": "zh_CN.UTF-8", 
            "LC_MESSAGES": "zh_CN.UTF-8", 
            "LC_MONETARY": "zh_CN.UTF-8", 
            "LC_NAME": "zh_CN.UTF-8", 
            "LC_NUMERIC": "zh_CN.UTF-8", 
            "LC_PAPER": "zh_CN.UTF-8", 
            "LC_TELEPHONE": "zh_CN.UTF-8", 
            "LC_TIME": "zh_CN.UTF-8", 
            "LOGNAME": "root", 
            "MAIL": "/var/mail/root", 
            "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin", 
            "PWD": "/home/python", 
            "SHELL": "/bin/bash", 
            "SUDO_COMMAND": "/bin/sh -c echo BECOME-SUCCESS-lspmdoftjjqryqzyywnxwwfmvtkadosd; LANG=zh_CN.UTF-8 LC_ALL=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8 /usr/bin/python /home/python/.ansible/tmp/ansible-tmp-1587557887.54-150801282271866/setup; rm -rf \"/home/python/.ansible/tmp/ansible-tmp-1587557887.54-150801282271866/\" > /dev/null 2>&1", 
            "SUDO_GID": "1000", 
            "SUDO_UID": "1000", 
            "SUDO_USER": "python", 
            "TERM": "xterm-256color", 
            "USER": "root", 
            "USERNAME": "root"
        }, 
        "ansible_fips": false, 
        "ansible_form_factor": "Other", 
        "ansible_fqdn": "ubuntu", 
        "ansible_hostname": "ubuntu", 
        "ansible_interfaces": [
            "lo", 
            "ens33"
        ], 
        "ansible_kernel": "4.15.0-96-generic", 
        "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_lsb": {
            "codename": "xenial", 
            "description": "Ubuntu 16.04.4 LTS", 
            "id": "Ubuntu", 
            "major_release": "16", 
            "release": "16.04"
        }, 
        "ansible_machine": "x86_64", 
        "ansible_machine_id": "da036fc2cd804ffbabc7e80e5a1f3f7a", 
        "ansible_memfree_mb": 119, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 751, 
                "used": 1219
            }, 
            "real": {
                "free": 119, 
                "total": 1970, 
                "used": 1851
            }, 
            "swap": {
                "cached": 2, 
                "free": 992, 
                "total": 1021, 
                "used": 29
            }
        }, 
        "ansible_memtotal_mb": 1970, 
        "ansible_mounts": [
            {
                "device": "/dev/sda1", 
                "fstype": "ext4", 
                "mount": "/", 
                "options": "rw,relatime,errors=remount-ro,data=ordered", 
                "size_available": 6850600960, 
                "size_total": 19945680896, 
                "uuid": "0979b935-32fa-41b6-9179-6ce0a798388e"
            }
        ], 
        "ansible_nodename": "ubuntu", 
        "ansible_os_family": "Debian", 
        "ansible_pkg_mgr": "apt", 
        "ansible_processor": [
            "GenuineIntel", 
            "Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz"
        ], 
        "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 6d 51 03 6a 01 11-0d 39 64 b3 ba 45 3b 39", 
        "ansible_product_uuid": "516D4D56-6A03-1101-0D39-64B3BA453B39", 
        "ansible_product_version": "None", 
        "ansible_python_version": "2.7.12", 
        "ansible_selinux": false, 
        "ansible_service_mgr": "systemd", 
        "ansible_ssh_host_key_dsa_public": "AAAAB3NzaC1kc3MAAACBAPg61Ex+xwP3FYpWCRPZOzlXsNkmVYOv5cPhNiADQaFvuDwTXk8l9wKvbBSfcQtQ+YUCS+/GzgNlqIsXWTRp7KhVSGzI5T7egK61P1O2uG1uiSJDfrkPKu5VWtmD3L+/5uf2x3etD2g2TsSOpY3uvSts1mYAY3l6JJYaCjdl/hkLAAAAFQCw4N9aCV0Uc++SOPhEEIrK+MSdcwAAAIEAw2z5UmTkyX4pMF9+31qY2lEG39SAV7P387JkDV/NDIIishsk2mEOwC4aHRb7DQwbA1xljTRK3GpZzPJRxp/Z6zMZebxpyC4W+QHl/Y90NGbVG7TmeH0YRlxeiTxDlkbpj8kPI6T0i94Qo0GnAVn3rcUq29kLJbMkkPDIAzOx/ngAAACAUeW3YName/MLd7Ia1jR75Of5+y9yd94bQ+915HIBMk84k9IfNlgeuvgyrf9udyYGonzLcF/BHgmiq6coig+ZROcHFV8hpdNRutyx91gr16D8BKgKnqQVo5HyQUWrdLHknWSyIl9GhPcepxf80qRe8ItR3gncEQpnMu1+zwoGZko=", 
        "ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMmRbCZ65HRy8pb/fDOLnJEmwXoOXI0X4SKR1JHKhV2G6sagXNNFsV98S0V+PSKeSPR52l5GLyq/fQkXqwXSxlg=", 
        "ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIOXWPZXZu9ideCZuBUej90q0m9gxHm8hKiVmyxy8Ok7p", 
        "ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDaBzsPdckXol3qTcL5lIzb6/Uf+MjAjv2BdlpnlRC5vIT2PyKYgLZQO4YZqGH4Qe7Vhk+zy1X8YiLwuBwJMbVp2pe7na15APOiAt2J+sF9XwEmsFJK73dcbclsK0reLJAXBhp23KePAXgslHio/rj83dTDHXb8fCuPvN52SmDGlH6CucjHmzoJ4XsTfwET/3uCsrWKtffV3zTWsdEIN30/b1/jMw9QUcklYYW7kg55kg+zRBbTBPVRNPnPw0y9gaTlHBrOxGHMPqjfpfP2c7VWXcpovfBNzBWneLLaz/uNP19t5eLr8tl4EVlyoCSo5/kZNFe8uQVBca7G4i7U4lyb", 
        "ansible_swapfree_mb": 992, 
        "ansible_swaptotal_mb": 1021, 
        "ansible_system": "Linux", 
        "ansible_system_vendor": "VMware, Inc.", 
        "ansible_uptime_seconds": 4675, 
        "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_virtualization_role": "guest", 
        "ansible_virtualization_type": "VMware", 
        "module_setup": true
    }, 
    "changed": false
}

2  setup 返回信息较多,可以过滤

python@ubuntu:~$ ansible webserver -m setup -a "filter=ansible_user*"
192.168.138.137 | SUCCESS => {
    "ansible_facts": {
        "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"
    }, 
    "changed": false
}

3 可通过tree参数将返回信息写入到文件中,不同主机返回的内容写入到不同的文件中,文件名以主机IP命名

python@ubuntu:~$ ansible webserver -m setup -a "filter=*address*" --tree ~/zml
192.168.138.137 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.138.137"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::72c6:bd9a:7a8d:9650"
        ]
    }, 
    "changed": false
}


#  查看文件
python@ubuntu:~/zml$ ll
总用量 12
drwxrwxr-x  2 python python 4096 4月  22 20:40 ./
drwxr-xr-x 40 python python 4096 4月  22 20:28 ../
-rw-rw-r--  1 python python  149 4月  22 20:28 192.168.138.137
python@ubuntu:~/zml$ cat 192.168.138.137 
{"ansible_facts": {"ansible_all_ipv4_addresses": ["192.168.138.137"], "ansible_all_ipv6_addresses": ["fe80::72c6:bd9a:7a8d:9650"]}, "changed": false}python@ubuntu:~/zml$ 

stat模块

1 获取文件的详细信息

python@ubuntu:~/zml$ ansible webserver -m stat -a "path=/home/python/zml"
192.168.138.137 | SUCCESS => {
    "changed": false, 
    "stat": {
        "atime": 1587555333.9527695, 
        "ctime": 1587555022.3954942, 
        "dev": 2049, 
        "exists": true, 
        "gid": 1000, 
        "gr_name": "python", 
        "inode": 399851, 
        "isblk": false, 
        "ischr": false, 
        "isdir": true, 
        "isfifo": false, 
        "isgid": false, 
        "islnk": false, 
        "isreg": false, 
        "issock": false, 
        "isuid": false, 
        "mode": "0775", 
        "mtime": 1587555022.3954942, 
        "nlink": 2, 
        "path": "/home/python/zml", 
        "pw_name": "python", 
        "rgrp": true, 
        "roth": true, 
        "rusr": true, 
        "size": 4096, 
        "uid": 1000, 
        "wgrp": true, 
        "woth": false, 
        "wusr": true, 
        "xgrp": true, 
        "xoth": true, 
        "xusr": true
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值