Ansible-include任务复用
1、include作用
有时,我们发现大量的 Playbook 内容需要重复编写,各 Tasks 之间功能需相互调用才能完成各自功能, Playbook 庞大到维护困难,这时我们需要使用 include
比如:A项目需要用到重启 httpd ,B项目需要用到,重启 httpd ,那么我们可以使用 Include 来减少重复编写。
2、示例1-多个项目调用相同task
2.1、编写 restart_httpd.yml 文件
cat << 'CAT_END' > restart_httpd.yaml - name: restart httpd server systemd: name=httpd state=restarted CAT_END
2.2、项目A-playbook
cat << 'CAT_END' > a_project.yaml - hosts: httpd tasks: - name: A project command command: echo "A" - name: restart httpd include: restart_httpd.yaml CAT_END
2.3、项目B-playbook
cat << 'CAT_END' > b_project.yaml - hosts: httpd tasks: - name: B project command command: echo "B" - name: restart httpd include: restart_httpd.yaml CAT_END
2.4、执行项目A、B
ansible-playbook a_project.yaml
ansible-playbook b_project.yaml