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

全部博文(112)

文章存档

2020年(1)

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: 系统运维

2017-03-17 22:44:24

 playbook的格式是yaml,由一个或多个‘play’组成,一个play对应一个task(任务),一个task是对ansible模块的调用,playbook命令自上而下顺序执行。
      
例1:

点击(此处)折叠或打开

  1. #主机与用户
  2. ---
  3. - hosts: test    #定义主机
  4.   remote_user: root    #定义远程用户
  5.   sudo: yes    #普通用户支持sudo
  6.   sudo_user: username    #指定了普通用户使用sudo
  7.   gather_facts: false    #指定了在任务执行前,是否执行setup模块获取主机相关信息,这里关闭了facts的信息
  8.   vars:    #指定了变量
  9.   - user: "test"    #指定了变量user,其值为"test"
  10. #Tasks列表
  11.   tasks:    #指定了一个任务
  12.   - name: template    #name参数对任务进行描述
  13.     template: src=template dest=/etc/test    #
  14.     meta: flush_handlers    #handlers会在执行到meta部分时,优先执行。
  15.     tags: template    #tag用于让用户选择运行或略过playbook中部分代码,如确信其没发生变化就可以通过tag跳过此段代码。可以通过命令行指定执行tags代码块: ansible-playbook test.yml --tags="template" 
  16.     notify:     #会在每一个task结束时被触发,即使有多个不同的task发生改动,notify只会被触发一次。当template模块中的文件被改动时,执行下列操作。
  17.     - restart apache
  18.     - restart mysql
  19.   handlers:    #handlers也是task任务,不过是由notify通知来操作,如果没有notify,handlers不会执行,不管有多少个notigy,等到play中所有的task执行完成之后,handlers也只会被执行一次。Handlers 最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了。
  20.     - name: restart apache
  21.       service: name=apache state=restarted
  22.     - name: restart mysql
  23.       service: name=mysql state=restarted

  24.     - include: test.yml    #可以和其它非include的tasks和handlers混合使用,也可以用来导入playbook文件。 
  25.    


yaml文件都是从一个列表开始,列表中的每一项都是一个键值对(哈希或字典)
1、yaml语法与范例:
    1.1、开始行都应该是---,区分多个play;...可选择性用来表示play结尾;
    1.2、yaml使用可打印的unicode字符,可使用utf-8或utf-16;
    1.3、使用空格(不能使用Tab)分隔,同行元素左对齐;
    1.4、使用井号(#)注释;
    1.5、每个play成员以单行表示,并用短杠+空格(- )起始;
   1.6、每个play成员用冒号+空格(: )分开键和值;
   1.7、play成员可以用问号(?)起始,表示多个词汇组成键值;
   1.8、字符串一般不适用引号,必要时可以用引号;
   1.9、使用双引号表示字符串时,可用反斜杠(\)进行转义;
   1.10、">"的作用,以缩进对齐来判断是否为一段文字,缩进与上一行不一致,则认为是新行;
   1.11、"|"的作用,表示之后的文字,每一行均为一个新行。
   1.12、"&"的作用,表示一个锚点标记,其它节点可以使用"*"或"<<: *"来引用它的值;
   1.13、"!!"的作用,强制转换类型;
 
  2、playbook实战(批量修改hostname)

点击(此处)折叠或打开

  1. /etc/ansible/hosts配置如下:
  2. [web1]
  3. 192.168.3.60 ansible_ssh_port=8020 ansible_ssh_user=root ansible_ssh_pass=123456
  4. [web2]
  5. 192.168.3.61 ansible_ssh_port=8020 ansible_ssh_user=root ansible_ssh_pass=123456
  6. [web3]
  7. 192.168.3.253  ansible_ssh_port=8020  ansible_ssh_user=test ansible_ssh_pass=123456 ansible_become_pass=123456 #普通用户通过su切root密码
  8. [web3:vars]
  9. web_hostname=web3_test
  10. #become=True
  11. #become_method=su
  12. [web2:vars]
  13. web_hostname=web2_test
  14. [web1:vars]
  15. web_hostname=web1_test
  16. [web:children]
  17. web1
  18. web2
  19. web3

  20. 此例用到了roles,目录结构为:/etc/ansible/roles/games/{defaults,files,handlers,meta,tasks,templates,vars}
  21. /etc/ansible/site_change_hostname.yml 内容如下:
  22. ---
  23. - hosts: all
  24.   remote_user: root
  25.   roles:
  26. - {role: games,tags: ['hostname']}
  27.   tasks:
  28.   - include: roles/games/tasks/modify_hostname.yml

  29. /etc/ansible/roles/games/tasks/modify_hostname.yml 内容如下:
  30. ---
  31. - name: modify the hostname config
  32.   lineinfile:
  33.      dest: /etc/sysconfig/network
  34.      regexp: '^HOSTNAME='
  35.      line: 'HOSTNAME={{ web_hostname }}'
  36. - name: modify the hostname
  37.   hostname: name={{ web_hostname }}
  38.   tags: hostname
  39. # ansible-playbook -i hosts site_host.yml -s

普通用户执行报错

点击(此处)折叠或打开

  1. #ansible web3 -m ping
  2. 192.168.3.249 | => {
  3.     "changed": false,
  4.     "msg": "Authentication or permission failure. In some cases, you may have been able to authenticate and did not have permissions on the remote directory. Consider changing the remote temp path in ansible.cfg to a path rooted in \"/tmp\". Failed command was: ( umask 77 && mkdir -p \"` echo /tmp/.ansible/tmp/ansible-tmp-1498121479.39-227152147951637 `\" && echo ansible-tmp-1498121479.39-227152147951637=\"` echo /tmp/.ansible/tmp/ansible-tmp-1498121479.39-227152147951637 `\" ), exited with result 1",
  5.     "unreachable": true
  6. }
  7. #/etc/ansible/ansible.cfg
  8. #保证普通用户对remote_tmp目录有写权限。
  9. remote_tmp     = /tmp/.ansible/tmp
  10. remote_tmp     = $HOME/.ansible/tmp
  11. #远程主机日志/var/log/message
  12. localhost ansible-ping: Invoked with data=None
  13. localhost ansible-command: Invoked with warn=True executable=None _uses_shell=True _raw_params=w removes=None creates=None chdir=None

点击(此处)折叠或打开

  1. # ansible web3 -m ping
  2. 192.168.3.253 | => {
  3.     "changed": false,
  4.     "msg": "Failed to connect to the host via ssh: Received message too long 458960955\r\n",
  5.     "unreachable": true
  6. }
  7. 这个报错是因为加载253的~/.bashrc文件输出内容过多导致的,通过过滤其输出来解决这个问题。


初始化系统环境:

点击(此处)折叠或打开

  1. /etc/ansible/site_os_initenv.yml
  2. ---
  3. - hosts: all
  4.   remote_user: root
  5.   roles:
  6.     - {role: games,tags: ['os_init_env']}
  7.   tasks:
  8.   - include: roles/games/tasks/build_os_initenv.yml

  9. /etc/ansible/roles/games/vars/main.yml
  10. ---
  11. game_dir: "/Game/Scripts/"
  12. scripts_dir: "/var/www/html/test/"
  13. scripts_file: "scripts/test.zip"
  14. tmp_dir: "/Game/Tmp"
  15. lib_file: "lib/lib64.zip"
  16. scripts_init: "initscript.sh nc"
  17. scripts_update: "update.sh all"
  18. os_init_env: "initenv.sh 1"

  19. /etc/ansible/roles/games/tasks/build_os_initenv.yml

  20. ---
  21. - name: copycopy lib
  22.   copy: " src={{ scripts_dir }}{{ lib_file }} dest={{ tmp_dir }} "
  23. - name: init os env
  24.   shell: "sh {{ game_dir }}cjsh_mobile_sh/{{ os_init_env }}"
  25.   tags: initos_init_env


初始化应用环境:

点击(此处)折叠或打开

  1. /etc/ansible/site_game_initenv.yml
  2. ---
  3. - hosts: web,game
  4. remote_user: root
  5. roles:
  6. - {role: games,tags: ['init']}
  7. - {role: games,tags: ['update']}
  8. tasks:
  9.   - include: roles/games/tasks/build_game_initenv.yml


  10. /etc/ansible/roles/games/tasks/build_game_initenv.yml
  11. ---
  12. - name: create games dir
  13.   file: " path={{ game_dir }} state=directory owner=root group=root mode=755 "
  14. - name: copy scripts
  15.   copy: " src={{ scripts_dir }}scripts/{{ scripts_file }} dest={{ game_dir }} "
  16. - name: unzip scripts.zip
  17.   shell: " cd {{ game_dir }} && unzip {{ scripts_file }} "
  18.   tags: build_env
  19. - name: begin build games init
  20.   shell: "sh {{ game_dir }}cjsh_mobile_sh/{{ scripts_init }}"
  21.   tags: init
  22. - name: update game
  23.   shell: "sh {{ game_dir }}cjsh_mobile_sh/{{ scripts_update }}"
  24.   tags: update








点击(此处)折叠或打开

  1. 在playbook和包含变量设置的配置文件中,使用冒号(:)来为变量赋值。
  2. 如:a:b
  3. 指定额外的变量--extra-vars
  4. ansible-playbook test.yml --extra-vars "a=b"
  5. playbook中vars代码块使用方法
  6. ---
  7. -hosts:test
  8.   vars:
  9.       a:b
  10. playbook中vars_file代码块使用方法
  11. ---
  12. -hosts:test
  13.   vars_files:
  14.       -vars.yml
  15. vars.yml内容如下:
  16. #当变量被独立文件定义时,变量可在ymal中顶格进行定义,也不需要vars标识。
  17. ---
  18. a:b

  19. playbook中使用双大括号加变量名来读取变量内容
  20. ---
  21. -hosts:test
  22.   vars:
  23.       a:b
  24.   tasks:
  25.       -command:echo {{ a }}

  26. playerbook数组变量(列表变量)
  27. foo_list:
  28.     -one
  29.     -two
  30.     -three
  31. 读取第一个变量,方法如下:
  32. foo[0]    #python语法格式
  33. foo|first    #jinja2语法格式

  34. ansible内置变量ansible_eth0:
  35. tasks:
  36.     -debug:var=ansible_eth0
  37. ok:[webserver] => {
  38.    "ansible_eth0": {
  39.        "active":true,
  40.        "device":"eth0",
  41.        "ipv4":{
  42.            "address":"10.0.2.15",
  43.            "netmask":"255.255.255.0",
  44.            "network":"10.0.2.0"
  45. }
  46. 读取ipv4的地址方法:
  47. {{ ansible_eth0.ipv4.address }}
  48. {{ ansible_eth0['ipv4']['address'] }}


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