Hosts文件介绍
357 |Anasible管理者海量的机器,这些机器的ip和用户密码都配置在Ansible的hosts文件中,hosts文件默认是存在/etc/ansible/hosts这个地址,也是可以在运行过程中指定的,比如
358 |bash-3.2$ ansible huanggai -m command -a "echo hello world" -v -i /Users/jukay/Desktop/hosts
359 | No config file found; using defaults
360 | huanggai | SUCCESS | rc=0 >>
361 | hello world
362 |
363 | 编写host文件
364 |Ansible的hosts文件格式如下:
365 |
366 | [test]
367 | aliyun ansible_host=47.92.102.184 ansible_user=root ansible_ssh_private_key_file=~/.ssh/aliyun
368 | huanggai ansible_host=192.168.1.197 ansible_user=deploy ansible_ssh_port=9122
369 |
370 | [defualt]
371 | ansible_ssh_private_key_file=~/.ssh/aliyun
372 | baochai ansible_host=192.168.1.191 ansible_user=deploy ansible_ssh_port=9122
373 | daiyu ansible_host=192.168.1.192 ansible_user=deploy ansible_ssh_port=9122
374 | xiangyun ansible_host=192.168.1.193 ansible_user=deploy ansible_ssh_port=9122
375 | xifeng ansible_host=192.168.1.195 ansible_user=deploy ansible_ssh_port=9122
376 | xiren ansible_host=192.168.1.196 ansible_user=deploy ansible_ssh_port=9122
377 | huanggai ansible_host=192.168.1.197 ansible_user=deploy ansible_ssh_port=9122
378 | yuanchun ansible_host=192.168.1.224 ansible_user=deploy ansible_ssh_port=9122
379 | puppetmaster ansible_host=192.168.1.224 ansible_user=deploy ansible_ssh_port=9122
380 | hp ansible_host=192.168.1.225 ansible_user=deploy ansible_ssh_port=9122
381 | jira ansible_host=192.168.1.227 ansible_user=deploy ansible_ssh_port=9122
382 | caiyun ansible_host=192.168.1.234 ansible_user=deploy ansible_ssh_port=9122
383 | caohong ansible_host=192.168.2.49 ansible_user=deploy ansible_ssh_port=9122
384 | caozhi ansible_host=192.168.2.50 ansible_user=deploy ansible_ssh_port=9122
385 | caopi ansible_host=192.168.2.51 ansible_user=deploy ansible_ssh_port=9122
386 | caochong ansible_host=192.168.2.52 ansible_user=deploy ansible_ssh_port=9122
387 | ceshi ansible_host=47.92.102.184 ansible_user=deploy ansible_ssh_port=9122
388 |
389 | [test]是什么意思?
390 |391 |421 |[test]表示对机器进行了分组,这里aliyun和huanggai作为一组机器,组的名字叫test,其他机器座位一组,名字叫defualt,我们可以直接对着一组机器进操作,比如,我们直接调用ping模块来查看一组机器的网络情况
392 |403 |bash-3.2$ ansible test -m ping -v -i /Users/jukay/Desktop/hosts 393 | No config file found; using defaults 394 | huanggai | SUCCESS => { 395 | "changed": false, 396 | "ping": "pong" 397 | } 398 | aliyun | SUCCESS => { 399 | "changed": false, 400 | "ping": "pong" 401 | } 402 |
我们也可以一组机器加若干台机器一起执行命令
404 |420 |bash-3.2$ ansible test,daiyu -m ping -v -i /Users/jukay/Desktop/hosts 405 | No config file found; using defaults 406 | daiyu | UNREACHABLE! => { 407 | "changed": false, 408 | "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", 409 | "unreachable": true 410 | } 411 | huanggai | SUCCESS => { 412 | "changed": false, 413 | "ping": "pong" 414 | } 415 | aliyun | SUCCESS => { 416 | "changed": false, 417 | "ping": "pong" 418 | } 419 |
ansible_host是什么意思?
422 |423 |425 |ansible_host表示目标机器的ip地址.如果这个地址如当前网络不通,那么ansible命令会返回UNREACHABLE!
424 |
ansible_user是什么意思?
426 |427 |429 |ansible_user表示登录到目标机器时候使用的用户账号。
428 |
ansible_ssh_port是什么意思?
430 |431 |433 | 434 | 435 |ansible_ssh_port是ansible通过ssh连接到目标机器时候使用的端口,默认是22号端口。
432 |