ansible系列(27)--ansible的include任务复用
1. include任务复用
有时,我们发现大量的 Playbook
内容需要重复编写,各 Tasks
之间功能需相互调用才能完成各自功能, Playbook
庞大到维护困难,这时我们需要使用 include
。
include
支持在tasks
阶段和handles
阶段调用,调用多个include
时可以使用loop
循环的方式,还可以向include
的playbook
中传递变量。
比如:A项目需要用到重启 httpd
,B项目需要用到重启 httpd
,那么我们可以使用 Include
来减少重复编写。
1.1 多个项目调用相同task
-
编写
restart_httpd.yml
文件:#注意这是一个tasks所有没有play的任何信息 [root@ansible project1]# cat restart_httpd.yml - name: Restart Httpd Server service: name: httpd state: restarted
-
A Project
的playbook
如下:[root@ansible project1]# cat a_project.yml - hosts: webserver tasks: - name: A Project command command: echo "A" - name: Restart httpd include: restart_httpd.yml
-
B Project
的playbook
如下:[root@ansible project1]# cat b_project.yml - hosts: webserver tasks: - name: B Project command command: echo "B" - name: Restart httpd include: restart_httpd.yml
-
playbook
运行如下:[root@ansible project1]# ansible-playbook a_project.yml [root@ansible project1]# ansible-playbook b_project.yml
1.2 Inlcude结合tags应用
”include”
不仅能够引用任务列表,还能够引用playbook
,比如,在一个playbook
中引用另一个playbook
。
示例:通过指定标签 tags
,来说明是安装 tomcat8
还是 tomcat9
1.准备入口 main.yml
文件,然后包含 install_tomcat8.yml
以及install_tomcat9.yml
2.在执行 main.yml
时,需要通过 --tags
指明要安装的版本
3.还可以在主playbook
文件中向引用的playbook
传递变量。
-
编写
main.yml
入口文件:#注意:引用playbook时建议使用import_playbook参数:官方提示:[DEPRECATION WARNING]: 'include' for playbook includes. You should use 'import_playbook' instead. This feature will be removed in version 2.12. [root@xuzhichao playbook]# cat tomcat_main.yml - name: Install Tomcat8 import_playbook: install_tomcat8.yml tags: tomcat8 vars: tomcat_version: 8.5.69 tomcat_install_path: /usr/local - name: Install Tomcat9 import_playbook: install_tomcat9.yml tags: tomcat9 vars: tomcat_version: 9.0.50 tomcat_install_path: /usr/local
-
编写
install_tomcat8.yml
文件:[root@xuzhichao playbook]# cat install_tomcat8.yml --- - hosts: localhost tasks: - name: Install JDK yum: name: java-1.8.0-openjdk state: present - 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 - 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 }}/tomcat8" state: link - name: Start Tomcat shell: cd "{{ tomcat_install_path }}"/tomcat8/bin && nohup ./startup.sh &
-
编写
install_tomcat9.yml
文件:[root@xuzhichao playbook]# cat install_tomcat9.yml --- - hosts: localhost tasks: - name: Install JDK yum: name: java-1.8.0-openjdk state: present - 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 &
-
运行
playbook
文件:#安装tomcat9 [root@xuzhichao playbook]# ansible-playbook -t tomcat9 tomcat_main.yml #安装tomcat8 [root@xuzhichao playbook]# ansible-playbook -t tomcat8 tomcat_main.yml