Chinaunix首页 | 论坛 | 博客
  • 博客访问: 385439
  • 博文数量: 112
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 800
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-29 13:41
文章分类

全部博文(112)

文章存档

2020年(1)

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: 系统运维

2017-03-14 21:54:08

1、ansible目录结构
   

点击(此处)折叠或打开

  1. #rpm -ql ansible
  2. /etc/ansible/ #配置文件目录
  3. /usr/bin/ #执行文件目录
  4. /usr/lib/python2.6/site-packages/ansible/ #lib库依赖目录
  5. /usr/share/doc/ansible-1.9.4/ #help文档
  6. /usr/share/man/man1/ #man文档目录
2、ansible配置文件ansible.cfg

点击(此处)折叠或打开

  1. [defaults]
  2. # some basic default values...

  3. inventory = /etc/ansible/hosts #定义inventory
  4. #library = /usr/share/my_modules/ #自定义lib库存放目录
  5. remote_tmp = $HOME/.ansible/tmp #临时文件远程主机存放目录或/tmp/.ansible/tmp(有写权限)
  6. #forks = 5 #默认开启的并发数
  7. poll_interval = 15 #默认轮询时间间隔
  8. sudo_user = root #默认sudo用户
  9. #ask_sudo_pass = True #是否需要ssh密码
  10. #ask_pass = True #是否需要sudo密码
  11. transport = smart #通信机制,默认为smart。
  12. #remote_port = 8020 #远程ssh端口,默认22
  13. module_lang = C #模块和系统之间通信的计算机语言,默认是C语言。
  14. #gathering = implicit #控制默认facts手机(远程系统变量),默认值为implicit,每一次play,facts都会被收集。
  15. #roles_path = /etc/ansible/roles #默认下载的roles存放的目录
  16. host_key_checking = False #首次链接是否需要检查key认证,建议设为false
  17. timeout = 30 #ssh超时时间
  18. #remote_user = root #使用/usr/bin/ansible-playbook链接的默认用户名,如果不指定,会使用当前登录的用户名
  19. #log_path = /var/log/ansible.log #执行日志存放目录
  20. #module_name = command #默认执行的模块
  21. #action_plugins = /usr/share/ansible/plugins/action #action插件存放目录
  22. #callback_plugins = /usr/share/ansible/plugins/callback #callback插件的存放目录
  23. #connection_plugins = /usr/share/ansible/plugins/connection #connection插件的存放目录
  24. #lookup_plugins = /usr/share/ansible/plugins/lookup #lookup插件的存放目录
  25. #vars_plugins = /usr/share/ansible/plugins/vars #vars插件的存放目录
  26. #filter_plugins = /usr/share/ansible/plugins/filter #filter插件的存放目录
  27. #test_plugins = /usr/share/ansible/plugins/test #test插件的存放目录
  28. #fact_caching = memory #getfact缓存的主机信息存放方式。
  29. #retry_files_enabled = False
  30. #retry_files_save_path = ~/.ansible-retry #错误重启文件存放目录


点击(此处)折叠或打开

  1. [privilege_escalation]
  2. #become=True #是否sudo
  3. #become_method=sudo #sudo方式
  4. #become_user=root #sudo后变为root用户
  5. #become_ask_pass=False #sudo后是否验证密码

点击(此处)折叠或打开

  1. [paramiko_connection]
  2. #record_host_keys=False #不记录新主机的key以提升效率
  3. #pty=False #禁用sudo功能

点击(此处)折叠或打开

  1. [ssh_connection]
  2. #pipelining = False #管道加速功能,需配合requiretty使用方可生效

点击(此处)折叠或打开

  1. [accelerate]
  2. #accelerate_port = 5099 #加速连接端口
  3. #accelerate_timeout = 30 #命令执行超时时间,单位秒
  4. #accelerate_connect_timeout = 5.0 #连接超时时间,单位秒
  5. #accelerate_daemon_timeout = 30         #上一个活动连接的时间,单位分钟
  6. #accelerate_multi_key = yes

点击(此处)折叠或打开

  1. [selinux]
  2. #special_context_filesystems=nfs,vboxsf,fuse,ramfs

3、ansible static inventory(静态库存)配置及详解
        static Inventory指的是在文件/etc/ansible/hosts中指定的主机和组。
    

点击(此处)折叠或打开

  1. # Ex 1: Ungrouped hosts, specify before any group headers.
  2. #未定义组的主机,指定在组前面。
  3. ## green.example.com
  4. ## blue.example.com
  5. ## 192.168.100.1
  6. ## 192.168.100.10

  7. # Ex 2: A collection of hosts belonging to the 'webservers' group
  8. #定义一个叫‘webservers’的组,里面的主机都属于这个组
  9. ## [webservers]
  10. ## alpha.example.org
  11. ## beta.example.org
  12. ## 192.168.1.100
  13. ## 192.168.1.110
  14. # If you have multiple hosts following a pattern you can specify
  15. # them like this:

  16. ## www[001:006].example.com    #[001:006]表示001~006之间所有的数字(包括001和006),如:www001.example.com、www002.example.com......www006.example.com

  17. # Ex 3: A collection of database servers in the 'dbservers' group

  18. ## [dbservers]
  19. ##
  20. ## db01.intranet.mydomain.net
  21. ## db02.intranet.mydomain.net
  22. ## 10.25.1.56
  23. ## 10.25.1.57
  24. ## db-[a:z]-node.example.com    #[a:z]表示a~z之间所有的字母。

  25. [test]
  26. 127.0.0.1
  27. 192.168.3.251
  28. #定义主机变量
  29. [webservers]
  30. web1.test.com http_port=8080 maxRequestsPerChild=801    #自定义http_port的端口号为8080,配置maxRequestsPerChild为801
  31. #定义组变量
  32. [groupserver]
  33. web1.test.com
  34. web2.test.com

  35. [groupservers:vars]
  36. ntp_server=ntp.test.com    #定义groupservers组中所有主机ntp_server值为ntp.magedu

  37. #定义嵌套及组变量
  38. [apache]
  39. [a:d].test.com

  40. [nginx]
  41. web[5:7].test.com


  42. [webservers:children]
  43. apache
  44. nginx

  45. #hosts文件中,变量会被定义在主机名的后面或组名的下方
  46. #为某台主机指定变量,作用范围仅限于当台主机
  47. [bj]
  48. app1.test.com proxy_state=present
  49. app2.test.com proxy_state=absent

  50. #为主机组指定变量,作用范围为整个主机组
  51. [bj:vars]
  52. bj_host=bj.static.test.com
  53. api_version=3.0





      3.1、定义主机和组:
             

点击(此处)折叠或打开

  1. #主机(hosts)可以使用域名、主机名、IP地址表示。
  2. 192.168.3.3
  3. web1.test.com
  4. #组(group)
  5. [webservers]
  6. 192.168.3.4
  7. web2.test.com

       3.2、端口与别名:
       

点击(此处)折叠或打开

  1. 192.168.3.5:6060
  2. #dbserver为192.168.3.5:6060的别名。
  3. dbserver ansible_ssh_port = 6060 ansible_ssh_host = 192.168.3.5

        3.3、指定主机范围:
         

点击(此处)折叠或打开

  1. [webservers]
  2. web[1:5].test.com
  3. [daserver]
  4. db-[a:e].test.com

          3.4、查看系统变量(facts):
         ansible test -m setup

点击(此处)折叠或打开

  1. ansible_ssh_host #要连接的主机名
  2. ansible_ssh_port #要连接的端口号(默认ssh端口22)
  3. ansible_ssh_user #ssh连接时指定的用户名
  4. ansible_ssh_pass #ssh连接时用户登录密码
  5. ansible_sudo_pass #使用sudo连接用户时的密码
  6. ansible_ssh_private_key_file #密钥文件,如果不适用ssh-agent管理时可以使用此选项。
  7. ansible_shell_type #shell类型,默认sh。
  8. ansible_connection #ssh连接的类型:local,ssh,paramiko;ansible1.2之前默认是paramiko,以后版本优先使用基于controlpersist的ssh。
  9. ansible_os_family  #系统类型。
  10. ansible_distribution_version  #系统版本。
  11. ansible_python_interpreter #用来指定python解释器的路径,也可以指定ruby、perl的路径。
  12. #例:
  13. [web1]
    192.168.3.60  ansible_ssh_port=8020  ansible_ssh_user=root ansible_ssh_pass=123456

         3.5、组内变量:
          

点击(此处)折叠或打开

  1. #变量也可以通过组名应用到组内的所有成员。
  2. #web组中包含两台主机web1和web2,通过对web组指定vars变更,相应web1web2apache和nginx变量参数的值web1.test.com和web2.test.com。
  3. [web]
  4. web1
  5. web2
  6. [web:vars]
  7. apache=web1.test.com
  8. nginx=web2.test.com
         3.6、组的子组与组内变量
         

点击(此处)折叠或打开


  1. [beijing]
  2. host1
  3. host2
  4. [tianjin]
  5. host3
  6. host4
  7. [northern:children]
  8. beijing
  9. tianjin
  10. [northern:vars]
  11. web_server=northern.test.com
  12. web_port=6060
  13. client_header_timeout=10
  14. client_body_timeout=10
  15. [china:children]
  16. northern
  17. southern

4、ansible dynamic inventory(动态库存)配置及详解
     dynamic inventory指通过外部脚本获取主机列表,并按照ansible 所要求的格式返回给ansilbe命令的。这部分一般会结合CMDB资管系统、zabbix 监控系统、crobble安装系统、云计算平台等获取主机信息。由于主机资源一般会动态的进行增减,而这些系统一般会智能更新。我们可以通过这些工具提供的API 或者接入库查询等方式返回主机列表。
    例1:通过一段python代码打印一个段json格式的主机信息:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. import json
  4. host1 = ['192.168.3.60','192.168.3.61']
  5. host2 = ['192.168.3.253']
  6. group1 = 'test1'
  7. group2 = 'test2'
  8. hostdata = {group1:host1,group2:host2}
  9. print json.dumps(hostdata,indent=4)    #indent代表缩进,这里缩进4个空格。
     执行结果:
   

点击(此处)折叠或打开

  1. root@ubuntu:/etc/ansible/roles# python host.py
  2. {
  3.     "test1": [
  4.         "192.168.3.60",
  5.         "192.168.3.61"
  6.     ],
  7.     "test2": [
  8.         "192.168.3.253"
  9.     ]
  10. }

  11. root@ubuntu:/etc/ansible/roles# ansible -i host.py test1 -m ping -k
  12. SSH password:
  13. 192.168.3.61 | SUCCESS => {
  14.     "changed": false,
  15.     "ping": "pong"
  16. }
  17. 192.168.3.60 | SUCCESS => {
  18.     "changed": false,
  19.     "ping": "pong"
  20. }
  21. root@ubuntu:/etc/ansible/roles# ansible -i host.py test2 -m command -a 'uptime' -k
  22. SSH password: 
  23. 192.168.3.61 | SUCCESS | rc=0 >>
  24.  12:17:01 up 7 days, 22:26,  3 users,  load average: 0.00, 0.00, 0.00

      例2:通过一段较复杂的python代码打印一个段json格式的主机信息:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. import json
  4. group1 = 'databases'
  5. group2 = 'webservers'
  6. group3 = 'test1'
  7. group4 = 'test2'
  8. group5 = 'test3'
  9. host1 = ['192.168.3.60','192.168.3.61']
  10. var1 = 'true'
  11. host2 = ['192.168.3.62','192.168.3.63']
  12. host3 = ['192.168.3.64','192.168.3.65','192.168.3.66']
  13. var2 = 'false'
  14. group6 = [group4,group5]
  15. host4 = ['192.168.3.67']
  16. host5 = ['192.168.3.68']
  17. hostdata = {group1:{"hosts":host1,"vars":{"a":var1}},group2:host2,group3:{"hosts":host3,"vars":{"b":var2},"children":group6},group4:host4,group5:host5}
  18. print json.dumps(hostdata,indent=4)    #indent代表缩进
     执行结果
 

点击(此处)折叠或打开

  1. root@ubuntu:/etc/ansible/roles# python hosts.py
  2. {
  3.     "webservers": [
  4.         "192.168.3.62",
  5.         "192.168.3.63"
  6.     ],
  7.     "test1": {
  8.         "hosts": [
  9.             "192.168.3.64",
  10.             "192.168.3.65",
  11.             "192.168.3.66"
  12.         ],
  13.         "children": [
  14.             "test2",
  15.             "test3"
  16.         ],
  17.         "vars": {
  18.             "b": "false"
  19.         }
  20.     },
  21.     "test3": [
  22.         "192.168.3.68"
  23.     ],
  24.     "test2": [
  25.         "192.168.3.67"
  26.     ],
  27.     "databases": {
  28.         "hosts": [
  29.             "192.168.3.60",
  30.             "192.168.3.61"
  31.         ],
  32.         "vars": {
  33.             "a": "true"
  34.         }
  35.     }
  36. }



     例:通过一段shell代码打印一个段json格式的主机信息:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. group='''
  3. {
  4.     "test":[
  5.         "192.168.3.60",
  6.         "192.168.3.61"
  7.       ]
  8. }
  9. '''
  10. echo ${group}
        执行结果

点击(此处)折叠或打开

  1. root@ubuntu:/etc/ansible/roles# ansible -i host.sh test -m command -a 'uname -o' -k
  2. SSH password:
  3. 192.168.3.61 | SUCCESS | rc=0 >>
  4. GNU/Linux

  5. 192.168.3.60 | SUCCESS | rc=0 >>
  6. GNU/Linux

阅读(1940) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~