设为首页 收藏本站
查看: 2186|回复: 0

[经验分享] ansible之setup模块

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-5-3 09:05:40 | 显示全部楼层 |阅读模式
关于ansible的setup,具体说明在http: //docs.ansible.com/ansible/setup_module.html
其实类似saltstack的grains静态信息收集,收集一些主机硬件信息或者以及其他如fqdn等等


db是我在hosts中定义的组名也可以是主机ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
[iyunv@ansible ~]# ansible db -m setup
db | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.33.137",
            "172.17.42.1"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::20c:29ff:fe14:cc24",
            "fe80::3c17:8bff:fe88:6e2b"
        ],
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "05/20/2014",
        "ansible_bios_version": "6.00",
        "ansible_cmdline": {
            "KEYBOARDTYPE": "pc",
            "KEYTABLE": "us",
            "LANG": "en_US.UTF-8",
            "SYSFONT": "latarcyrheb-sun16",
            "quiet": true,
            "rd_LVM_LV": "VolGroup/lv_root",
            "rd_NO_DM": true,
            "rd_NO_LUKS": true,
            "rd_NO_MD": true,
            "rhgb": true,
            "ro": true,
            "root": "/dev/mapper/VolGroup-lv_root"
        },
        "ansible_date_time": {
            "date": "2016-05-02",
            "day": "02",
            "epoch": "1462151488",
            "hour": "09",
            "iso8601": "2016-05-02T01:11:28Z",
            "iso8601_basic": "20160502T091128125057",
            "iso8601_basic_short": "20160502T091128",
            "iso8601_micro": "2016-05-02T01:11:28.125158Z",
            "minute": "11",
            "month": "05",
            "second": "28",
            "time": "09:11:28",
            "tz": "CST",
            "tz_offset": "+0800",
            "weekday": "Monday",
            "weekday_number": "1",
            "weeknumber": "18",
            "year": "2016"
        },
        "ansible_default_ipv4": {
            "address": "192.168.33.137",
            "alias": "eth0",
            "broadcast": "192.168.33.255",
            "gateway": "192.168.33.2",
            "interface": "eth0",
            "macaddress": "00:0c:29:14:cc:24",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "192.168.33.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": "103831552",
                        "sectorsize": 512,
                        "size": "49.51 GB",
                        "start": "1026048"
                    }
                },
                "removable": "0",
                "rotational": "1",
                "scheduler_mode": "cfq",
                "sectors": "104857600",
                "sectorsize": "512",
                "size": "50.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",
                "scheduler_mode": "cfq",
                "sectors": "2097151",
                "sectorsize": "512",
                "size": "1024.00 MB",
                "support_discard": "0",
                "vendor": "NECVMWar"
            }
        },
        "ansible_distribution": "CentOS",
        "ansible_distribution_major_version": "6",
        "ansible_distribution_release": "Final",
        "ansible_distribution_version": "6.6",
        "ansible_dns": {
            "nameservers": [
                "192.168.33.2"
            ],
            "search": [
                "localdomain"
            ]
        },
        "ansible_docker0": {
            "active": true,
            "device": "docker0",
            "id": "8000.000000000000",
            "interfaces": [],
            "ipv4": {
                "address": "172.17.42.1",
                "broadcast": "global",
                "netmask": "255.255.0.0",
                "network": "172.17.0.0"
            },
            "ipv6": [
                {
                    "address": "fe80::3c17:8bff:fe88:6e2b",
                    "prefix": "64",
                    "scope": "link"
                }
            ],
            "macaddress": "3e:17:8b:88:6e:2b",
            "mtu": 1500,
            "promisc": false,
            "stp": false,
            "type": "bridge"
        },
        "ansible_domain": "",
        "ansible_env": {
            "G_BROKEN_FILENAMES": "1",
            "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:/sbin:/bin:/usr/sbin:/usr/bin",
            "PWD": "/root",
            "SELINUX_LEVEL_REQUESTED": "",
            "SELINUX_ROLE_REQUESTED": "",
            "SELINUX_USE_CURRENT_RANGE": "",
            "SHELL": "/bin/bash",
            "SHLVL": "2",
            "SSH_CLIENT": "192.168.33.131 38108 22",
            "SSH_CONNECTION": "192.168.33.131 38108 192.168.33.137 22",
            "SSH_TTY": "/dev/pts/0",
            "TERM": "xterm",
            "USER": "root",
            "_": "/usr/bin/python"
        },
        "ansible_eth0": {
            "active": true,
            "device": "eth0",
            "ipv4": {
                "address": "192.168.33.137",
                "broadcast": "192.168.33.255",
                "netmask": "255.255.255.0",
                "network": "192.168.33.0"
            },
            "ipv6": [
                {
                    "address": "fe80::20c:29ff:fe14:cc24",
                    "prefix": "64",
                    "scope": "link"
                }
            ],
            "macaddress": "00:0c:29:14:cc:24",
            "module": "e1000",
            "mtu": 1500,
            "pciid": "0000:02:01.0",
            "promisc": false,
            "type": "ether"
        },
        "ansible_fips": false,
        "ansible_form_factor": "Other",
        "ansible_fqdn": "db",
        "ansible_hostname": "db",
        "ansible_interfaces": [
            "lo",
            "docker0",
            "eth0"
        ],
        "ansible_kernel": "2.6.32-504.23.4.el6.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": {
                "lv_root": {
                    "size_g": "48.54",
                    "vg": "VolGroup"
                },
                "lv_swap": {
                    "size_g": "0.97",
                    "vg": "VolGroup"
                }
            },
            "vgs": {
                "VolGroup": {
                    "free_g": "0",
                    "num_lvs": "2",
                    "num_pvs": "1",
                    "size_g": "49.51"
                }
            }
        },
        "ansible_machine": "x86_64",
        "ansible_memfree_mb": 105,
        "ansible_memory_mb": {
            "nocache": {
                "free": 217,
                "used": 265
            },
            "real": {
                "free": 105,
                "total": 482,
                "used": 377
            },
            "swap": {
                "cached": 0,
                "free": 991,
                "total": 991,
                "used": 0
            }
        },
        "ansible_memtotal_mb": 482,
        "ansible_mounts": [
            {
                "device": "/dev/mapper/VolGroup-lv_root",
                "fstype": "ext4",
                "mount": "/",
                "options": "rw",
                "size_available": 44720926720,
                "size_total": 51164958720,
                "uuid": "ac02f356-e4ce-448b-914d-4ba34575f6a2"
            },
            {
                "device": "/dev/sda1",
                "fstype": "ext4",
                "mount": "/boot",
                "options": "rw",
                "size_available": 421961728,
                "size_total": 499355648,
                "uuid": "06425d02-4d1f-49a1-afdb-8ef1dafd3334"
            }
        ],
        "ansible_nodename": "db",
        "ansible_os_family": "RedHat",
        "ansible_pkg_mgr": "yum",
        "ansible_processor": [
            "GenuineIntel",
            "Intel(R) Core(TM) i5-3210M 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 a2 b5 7d 5d 89 ea-96 fa 0b 4a 97 14 cc 24",
        "ansible_product_uuid": "564DA2B5-7D5D-89EA-96FA-0B4A9714CC24",
        "ansible_product_version": "None",
        "ansible_python_version": "2.6.6",
        "ansible_selinux": {
            "config_mode": "enforcing",
            "mode": "enforcing",
            "policyvers": 24,
            "status": "enabled",
            "type": "targeted"
        },
        "ansible_service_mgr": "upstart",
        "ansible_ssh_host_key_dsa_public": "AAAAB3NzaC1kc3MAAACBANNbS+lp2pmFe5jHrIqbAxO0pPeuprVJVIKwnuNFzSavaoGBtK0PdtDhvuky4BJq9v2RnztCIaTohY2QqZE3hhknUAGywuAdpPGMnsWGazY6Ne70ZsobVegATk88G7e2A/pQsFLskJEjzieUtS4orAvCnAwssNJcbaCTw5EnfNfNAAAAFQCUWV2C4o0oTzBO9HfibENN7E9FCQAAAIEAhC/CKIvfxMLIbWG0nqpSmBP/aBzG+2nFKV326vDhReLJEC0cLKRggrtJdOlKClCnfVU8vmXjbjzuEDr/MF//8DbA/4kRNpjua+dZjnkrr4hbSzdQsCa1B2nzK1hioqh2Dn8rhsrZSrAwjYFcLa81p3s3+Ja02jCWt/rgBvDOLNQAAACAZ/LC5sF7jgZSLDMfrRJbEB4KgcadhpKSoISymE2+lHr9pO4lZfVG6djscA7WsF+cB5Td2or4XPeU/1IOWczaTieiFHGJ7ywioafGNbYXtrSf7z2KGbEwizGGMS4036RvA5Y3cbIT2166JAxeThaF+wb66r+AZKvRqk7kYGyoUdI=",
        "ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAABIwAAAQEAuIPddB4RPpv6RoeFlxMQV8l0aZPUy/S3z8JgmVgTZAGZCtULe6YFPnqUN1FvcPrIrHDDaRZ8AthzH8fpELGU9DDQpUpvHRXlV7Q29feQ1+N5v4fGE0nCwiS+T2exnBgyIh84FTDhpL/wdielu4+PtF3ED/IPGrxEepjm/ethqzV00g9e8IpdxKaFmhGFJB2HjPjBzywz/UwUzZ1XPNNRGWO/niVxivmIQPzul0OAIddTiXLW+5ay3D5ajrsFXfbpkUUBuwxzTdlNYmOR/YUonK76LUvIoUF4WZish4XcBEpHXn2lCI6zDyj8nGVX5yUDIlRdgTcMB0EWtv2cwLVkZw==",
        "ansible_swapfree_mb": 991,
        "ansible_swaptotal_mb": 991,
        "ansible_system": "Linux",
        "ansible_system_vendor": "VMware, Inc.",
        "ansible_uptime_seconds": 15909,
        "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
}




setup的用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@ansible ~]# ansible-doc -s setup
less 436
Copyright (C) 1984-2009 Mark Nudelman
less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
- name: Gathers facts about remote hosts
  action: setup
      fact_path              # path used for local ansible facts (*.fact) - files in this dir will be run (if executable) and
                               their results be added to ansible_local facts if a file is not
                               executable it is read. File/results format can be json or ini-
                               format
      filter                 # if supplied, only return facts that match this shell-style (fnmatch) wildcard.




使用setup获取ip地址以及主机名使用filter过滤等等

ip地址
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@ansible ~]# ansible db -m setup -a 'filter=ansible_default_ipv4'
db | SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "192.168.33.137",
            "alias": "eth0",
            "broadcast": "192.168.33.255",
            "gateway": "192.168.33.2",
            "interface": "eth0",
            "macaddress": "00:0c:29:14:cc:24",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "192.168.33.0",
            "type": "ether"
        }
    },
    "changed": false
}



获取内存信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[iyunv@ansible ~]# ansible db -m setup -a 'filter=ansible_memory_mb'
db | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 217,
                "used": 265
            },
            "real": {
                "free": 101,
                "total": 482,
                "used": 381
            },
            "swap": {
                "cached": 0,
                "free": 991,
                "total": 991,
                "used": 0
            }
        }
    },
    "changed": false
}



获取主机名。。
[iyunv@ansible ~]# ansible db -m setup -a 'filter=ansible_nodename'
db | SUCCESS => {
    "ansible_facts": {
        "ansible_nodename": "db"
    },
    "changed": false
}

ansible的setup就到这吧。那个fact_path,其实就是在本地定义一些键值对,然后指定文件路径使用.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-211940-1-1.html 上篇帖子: ansible介绍以及安装 下篇帖子: ansible的配置文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表