十一、ansible的include任务重复使用

本文介绍了如何在AnsiblePlaybooks中利用`include`进行任务复用,如在多个项目中调用共享的任务,并展示了如何结合`tags`进行版本选择以及如何传递变量给被引用的playbook。
摘要由CSDN通过智能技术生成

目录

一、include任务复用

1、多个项目调用task

2、include结合tags 应用


一、include任务复用

        有时,我们大量的playbook 内容需要重复编写,各 tasks 之间功能需要相互调用才能完成,这时我们需要使用 include

        include 支持在tasks阶段和handles阶段调用,调用多个include时可以使用loop循环的方式,还可以向include的playbook中传递变量。 include在2.12版本之后删除了,官方建议使用“import_playbook”

1、多个项目调用task

示例:

比如:A项目需要用到重启httpd,B项目也需要重启,那就可以使用include,来减少重复编写
1、先创建爱你一个tasks,
[root@k8s-master-1 include]# vim restart_httpd.yaml
- name: restart httpd service
  service:
    name: httpd
    state: restarted
[root@k8s-master-1 include]# vim a_project.yaml
---
- hosts: web
  remote_user: root

  tasks:
  - name: command
    command: echo "A"

  - name: test include
    include: restart_httpd.yaml
[root@k8s-master-1 include]# vim b_project.yaml
---
- hosts: web
  remote_user: root

  tasks:
  - name: command
    command: echo "B"

  - name: test include
    include: restart_httpd.yaml
[root@k8s-master-1 include]# ansible-playbook -C a_project.yaml 
[root@k8s-master-1 include]# ansible-playbook -C b_project.yaml 

2、include结合tags 应用

        include 不仅能够引用任务列表,还能够引用playbook,比如,在一个playbook中引用另一个playbook

        示例:通过指定标签,来说明是安装tomcat8 还是tomcat9

1、准备入口,main.yaml 文件,然后包含installed_tomcat8.yaml以及installed_tomcat9.yaml
2、在执行 main.yaml时,需要通过  --tags 指明要安装的版本
3、还可以在主playbook文件中,向引用的playbook传递变量

[root@k8s-master-1 include]# vim tomcat_main.yaml
- name: Install Tomcat8
  import_playbook: install_tomcat8.yaml
  tags: tomcat8
  vars:
    tomcat_version: 8.5.91
    tomcat_install_path: /usr/local

- name: Install Tomcat9
  import_playbook: install_tomcat9.yaml
  tags: tomcat9
  vars:
    tomcat_version: 9.0.78
    tomcat_install_path: /usr/local

[root@k8s-master-1 include]# vim install_tomcat8.yaml
---
- hosts: web
  remote_user: root

  tasks:

    - name: Download Tomacat
      get_url:
        url: https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz
        dest: /root
        validate_certs: false

    - name: Unarchive Tomcat
      unarchive:
        src: /root/apache-tomcat-{{ tomcat_version }}.tar.gz
        dest: "{{ tomcat_install_path }}"
        remote_src: yes  #需要指定此参数,不然会去管理端找源文件
    - name: Create Link File
      file:
        src: "{{ tomcat_install_path }}/apache-tomcat-{{ tomcat_version }}"
        dest: "{{ tomcat_install_path }}/tomcat8"
        state: link

    - name: Start Tomcat
      shell: cd "{{ tomcat_install_path }}"/tomcat8/bin && nohup ./startup.sh &
[root@k8s-master-1 include]# vim install_tomcat9.yaml
---
- hosts: web

  tasks:

    - name: Download Tomacat
      get_url:
        url: https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz
        dest: /root

    - name: Unarchive Tomcat
      unarchive:
        src: /root/apache-tomcat-{{ tomcat_version }}.tar.gz
        dest: "{{ tomcat_install_path }}"

    - name: Create Link File
      file:
        src: "{{ tomcat_install_path }}/apache-tomcat-{{ tomcat_version }}"
        dest: "{{ tomcat_install_path }}/tomcat9"
        state: link

    - name: Start Tomcat
      shell: cd "{{ tomcat_install_path }}"/tomcat9/bin && nohup ./startup.sh &
[root@k8s-master-1 include]# ansible-playbook -t tomcat8 tomcat_main.yaml
执行完,去呗管理端就可以看到,tomcat进程已经被启动
浏览器访问8080  也是正常的

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁华依在

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值