• Ansible - playbook


    playbooks 本身由以下各部分组成

    1. Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行
    2. Variables:变量
    3. Templates:模板
    4. Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
    5. Roles:角色

    运行playbook

    ansible-playbook 文件名 

    补充参数

    1. -k(–ask-pass):用来交互输入ssh密码
    2. -K(-ask-become-pass):用来交互输入sudo密码
    3. -u:指定用户
    4. ansible-playbook 文件名 --syntax-check #检查yaml文件的语法是否正确
    5. ansible-playbook 文件名 --list-task #检查tasks任务
    6. ansible-playbook 文件名 --list-hosts #检查生效的主机
    7. ansible-playbook 文件名 --start-at-task='install httpd' #指定从某个task开始运行

    案例:

    1. vim test1.yaml
    2. --- #yaml文件以---开头,以表明这是一个yaml文件,可省略
    3. - name: first play #定义一个play的名称,可省略
    4. gather_facts: false #设置不进行facts信息收集,这可以加快执行速度,可省略
    5. hosts: webservers #指定要执行任务的被管理主机组,如多个主机组用冒号分隔
    6. remote_user: root #指定被管理主机上执行任务的用户
    7. tasks: #定义任务列表,任务列表中的各任务按次序逐个在hosts中指定的主机上执行
    8. - name: test connection #自定义任务名称
    9. ping: #使用 module: [options] 格式来定义一个任务
    10. - name: disable selinux
    11. command: '/sbin/setenforce 0' #command模块和shell模块无需使用key=value格式
    12. ignore_errors: True #如执行命令的返回值不为0,就会报错,tasks停止,可使用ignore_errors忽略失败的任务
    13. - name: disable firewalld
    14. service: name=firewalld state=stopped #使用 module: options 格式来定义任务,option使用key=value格式
    15. - name: install httpd
    16. yum: name=httpd state=latest
    17. - name: install configuration file for httpd
    18. copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf #这里需要一个事先准备好的/opt/httpd.conf文件
    19. notify: "restart httpd" #如以上操作后为changed的状态时,会通过notify指定的名称触发对应名称的handlers操作
    20. - name: start httpd service
    21. service: enabled=true name=httpd state=started
    22. handlers: #handlers中定义的就是任务,此处handlers中的任务使用的是service模块
    23. - name: restart httpd #notify和handlers中任务的名称必须一致
    24. service: name=httpd state=restarted
    25. ##Ansible在执行完某个任务之后并不会立即去执行对应的handler,而是在当前play中所有普通任务都执行完后再去执行handler,这样的好处是可以多次触发notify,但最后只执行一次对应的handler,从而避免多次重启。

    定义、引用变量

    1. - name: second play
    2. hosts: dbservers
    3. remote_user: root
    4. vars: #定义变量
    5. - groupname: mysql #格式为 key: value
    6. - username: nginx
    7. tasks:
    8. - name: create group
    9. group: name={{groupname}} system=yes gid=306 #使用 {{key}} 引用变量的值
    10. - name: create user
    11. user: name={{username}} uid=306 group={{groupname}}
    12. - name: copy file
    13. copy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt #在setup模块中可以获取facts变量信息
    14. ansible-playbook test1.yaml -e "username=nginx" #在命令行里定义变量

    when条件判断

    Ansible中,提供的唯一一个通用的条件判断是when指令,当when指令的值为true时,则该任务执行,否则不执行该任务。

    when一个比较常见的应用场景是实现跳过某个主机不执行任务或者只有满足条件的主机执行任务

    1. vim test2.yaml
    2. ---
    3. - hosts: all
    4. remote_user: root
    5. tasks:
    6. - name: shutdown host
    7. command: /sbin/shutdown -r now
    8. when: ansible_default_ipv4.address == "192.168.86.11"
    9. 或者 :when: inventory_hostname == "<主机名>"
    10. #when指令中的变量名不需要手动加上 {{}}
    11. ansible-playbook test2.yaml

    迭代:

    Ansible提供了很多种循环结构,一般都命名为with_items,作用等同于 loop 循环。

    1. vim test3.yaml
    2. ---
    3. - name: play1
    4. hosts: dbservers
    5. gather_facts: false
    6. tasks:
    7. - name: create directories
    8. file:
    9. path: "{{item}}"
    10. state: directory
    11. with_items: #等同于 loop:
    12. - /tmp/test1
    13. - /tmp/test2
    14. - name: add users
    15. user: name={{item.name}} state=present groups={{item.groups}}
    16. with_items:
    17. - name: test1
    18. groups: wheel
    19. - name: test2
    20. groups: root
    21. with_items:
    22. - {name:'test1', groups:'wheel'}
    23. - {name:'test2', groups:'root'}
    24. ansible-playbook test3.yaml

    Templates 模块

    Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。

    1. 1.先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量
    2. cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2
    3. vim /opt/httpd.conf.j2
    4. Listen {{http_port}} #42行,修改
    5. ServerName {{server_name}} #95行,修改
    6. DocumentRoot "{{root_dir}}" #119行,修改
    7. 2.修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量
    8. vim /etc/ansible/hosts
    9. [webservers]
    10. 192.168.86.11 http_port=192.168.86.11:80 server_name=www.accp.com:80 root_dir=/etc/httpd/htdocs
    11. [dbservers]
    12. 192.168.86.22 http_port=192.168.86.22:80 server_name=www.benet.com:80 root_dir=/etc/httpd/htdocs
    13. 3.编写 playbook
    14. vim apache.yaml
    15. ---
    16. - hosts: all
    17. remote_user: root
    18. vars:
    19. - package: httpd
    20. - service: httpd
    21. tasks:
    22. - name: install httpd package
    23. yum: name={{package}} state=latest
    24. - name: install configure file
    25. template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #使用template模板
    26. notify:
    27. - restart httpd
    28. - name: create root dir
    29. file: path=/etc/httpd/htdocs state=directory
    30. - name: start httpd server
    31. service: name={{service}} enabled=true state=started
    32. handlers:
    33. - name: restart httpd
    34. service: name={{service}} state=restarted
    35. ansible-playbook apache.yaml

  • 相关阅读:
    AI 大战 AI,一个深度强化学习多智能体竞赛系统
    企业新闻稿怎么写?教你撰写企业新闻稿
    软文推广提升品牌曝光的技巧,媒介盒子告诉你
    【Spring5】AOP面向切面?程序不可多得的Buff
    学信息系统项目管理师第4版系统36_结语
    JavaScript Object 转 FormData
    没有机会就创造机会
    vue单向数据流?
    C++11 互斥锁
    如何训练专属的OCR文字识别模型
  • 原文地址:https://blog.csdn.net/2301_78069073/article/details/133938975