PlayBook

playbook是Ansible的配置、部署和编制语言。每个playbook由一个列表中的一个或多个”palys”组成。

  • Ansible-playbook(剧本)执行过程
    • 将已有编排好的任务集写入Ansible-Playbook
    • 通过ansible-playbook命令分拆任务集至逐条ansible命令,按预定规则逐条执行

案例样式

  1. name: "Ansible"
  2. version: 2.4
  3. python_version: 2.7
  4. module:
  5. - "Network"
  6. - "Linux Server"
  7. - "Windows Server"
  8. plugins:
  9. action_plugin: true
  10. cache_plugin: true
  11. shell_plugin: false

ansible 演示 palyload

  1. vim host1.yaml
  2. - hosts: test1 #组名称,或者host名称
  3. name: play-test #设置该host组的名称
  4. gather_facts: no
  5. tasks: #设置任务
  6. - name: check host connection #任务名称
  7. ping: #使用ping模块

debug模块

平时我们在使用ansible编写playbook时,经常会遇到错误,很多时候有不知道问题在哪里 。这个时候可以使用-vvv参数打印出来详细信息,不过很多时候-vvv参数里很多东西并不是我们想要的,这时候就可以使用官方提供的debug模块来查找问题出现在哪里。

debug参数 作用
msg 调试输出的消息
var 将某个任务执行的输出作为变量传递给debug模块,debug会直接将其打印输出
verbosity debug的级别(默认是0级,全部显示,写其他数字则跳过) 加上-vvv 显示详细的debug参数

常见的变量:

  • {{ansible_hostname}} #显示hostname
  • {{ansible_product_uuid}} #显示机器的uuid

  • ```shell vim helloworld.yaml
  • name: Hello world hosts: localhost #只检查本地 gather_facts: no

    tasks:

    • name: Hello world debug: msg: “hello ansible” #msg默认打印hello world verbosity: 0 ansible-playbook playbook/helloworkd.yml -vvv #调用debug模块运行

vim uuids.yaml

  • name: Hello world hosts: localhost

    tasks:

    • name: Hello world debug: msg: “hello ansible” verbosity: 0 ```

image.png

var变量的使用

  1. vim heeloworld.yaml
  2. - name: Hello world
  3. hosts: localhost
  4. gather_facts: no
  5. vars:
  6. grettings: "hello from vars"
  7. tasks:
  8. - name: Hello world
  9. debug:
  10. msg: "{{grettings}}"
  11. verbosity: 0
  12. ansible-playbook playbook/helloworkld.yml

image.png

  1. - name: Hello world
  2. hosts: localhost
  3. vars:
  4. grettings: "hello from vars"
  5. demo:
  6. a:
  7. - a: 1
  8. - b: 2
  9. b: test
  10. tasks:
  11. - name: Hello world
  12. debug:
  13. msg: "{{demo}}"
  14. verbosity: 0
  15. ansible-playbook playbook/helloworkd.yml

image.png

设置变量文件

如果我们将变量在每个文件中创建,文件会变得臃肿,并且占用内存更多,无法重复利用。我们可以将变量创建到某个具体文件中。

  1. mkdir /root/vars
  2. vim deno_vars.yml
  3. grettings: "hello i in vars"
  4. vim helloworld.yml
  5. - name: Hello world
  6. hosts: localhost
  7. vars_files:
  8. - "../vars/demo_vars.yml"
  9. tasks:
  10. - name: Hello world
  11. debug:
  12. msg: "{{grettings}}"
  13. verbosity: 0

注意:关于vars_files的优先级。本地创建<先调用文件<后调用文件

image.png

var变量循环

单循环

  1. vim loops.yml
  2. - name: loop one
  3. hosts: localhost
  4. gather_facts: no
  5. vars:
  6. test:
  7. - test1
  8. - test2
  9. - test3
  10. - test4
  11. tasks:
  12. - name: Test loop
  13. debug:
  14. msg: "{{item}}"
  15. verbosity: 0
  16. with_items: "{{test}}" #创建一个with_items,其中写入变量

image.png

多层循环

  1. - name: loop two
  2. hosts: localhost
  3. gather_facts: no
  4. vars:
  5. test:
  6. - test1
  7. - test2
  8. - test3
  9. - test4
  10. demo:
  11. - demo1
  12. - demo2
  13. - demo3
  14. tasks:
  15. - name: Test loop
  16. debug:
  17. msg: "{{item[0]}},{{item[1]}}"
  18. verbosity: 0
  19. #with_items: "{{test}}"
  20. with_nested: #可以以该格式,多次循环下去
  21. - "{{test}}"
  22. - "{{demo}}"

image.png

条件语句

  1. - name: when
  2. hosts: localhost
  3. gather_facts: no
  4. vars:
  5. seq:
  6. - 1
  7. - 2
  8. - 3
  9. - 4
  10. tasks:
  11. - name: Test when
  12. debug:
  13. msg: "{{item}}"
  14. verbosity: 0
  15. with_items: "{{seq}}"
  16. when: item >2 #当数值大于2时打印

image.png

Group和Host变量

在主机配置文件中,也可以设置变量,并且变量使多个服务器使用或重复利用。

方式1:在一个文件中设置

这种大多情况下是在配置几乎相同,没有差别变量时

  1. vim host #创建host文件,其中创建主机和所需变量
  2. [ans]
  3. ans2
  4. ans3
  5. [mydb]
  6. mydb1
  7. mydb2
  8. [ans:var] #此时创建的变量,就被ans中全部的服务器使用
  9. ansible_connection: ssh
  10. ansible_user: root
  11. ansible_password: mly118235
  12. [mydb:var] #此时创建的变量,就被var中全部的服务器使用
  13. ansible_connection: ssh
  14. ansible_user: root
  15. ansible_password: mydb123
  16. http_port: 3306
  17. vim main.yml #创建playbook文件
  18. - name: group
  19. hosts: all
  20. gather_facts: no
  21. tasks:
  22. - name: Test group
  23. debug:
  24. msg: "ansible_user={{ansible_user}},ansible_password={{ansible_password}}"
  25. ansible-playbook -i inventory/host playbook/main.yml

image.png

方式2:在不同文件中设置

  1. vim host
  2. [all]
  3. ans2
  4. ans3
  5. [mydb]
  6. mydb1
  7. mydb2
  8. #只需要创建服务器名称,必须要再创建变量了
  9. mkdir group_vars #创建文件夹,方便管理变量。文件夹名称没限制,但是需要和host文件在同一个文件夹中
  10. vim group_vars/ans.yml #文件名必须是host文件中的组名
  11. ansible_connection: ssh
  12. ansible_user: root
  13. ansible_password: mly118235
  14. vim group_vars/mydb.yml
  15. ansible_connection: ssh
  16. ansible_user: root
  17. ansible_password: mydb123
  18. http_port: 3306
  19. mkdir host_vars #创建文件夹,该文件夹存放不同的变量,依然会共用group_vars中的变量。
  20. vim host_vars/ans2.yml #文件名必须是host的名称
  21. http_port: 443 #此时就是单独创建的变量
  22. vim host_vars/ans3.yml
  23. http_port: 80
  24. #两个端口不一样
  25. vim main.yml
  26. - name: group
  27. hosts: all
  28. gather_facts: no
  29. tasks:
  30. - name: Test group
  31. debug:
  32. msg: "ansible_user={{ansible_user}},ansible_password={{ansible_password}},http_port={{http_port}}"
  33. #输出用户名,用户名密码,端口号

image.png

ansible.cfg 免去填写文件路径

  1. vim /root/playbook/ansible.cfg #将ansible.cfg放入到和main.yml一个文件夹
  2. [defaults]
  3. inventory= /root/inventory/host
  4. ansible-playbook main.yml