美文网首页
2. Including and Importing

2. Including and Importing

作者: Besty_00 | 来源:发表于2019-01-11 16:24 被阅读0次

编写大型的playbook时,都挤在同一个playbook文件会显得很臃肿,所以应当拆分成多个文件,每个文件实现特定的功能,最后统一导入到主 playbook(main playbooks)文件中,相当于编程语言的import/include, 而 ansible 则提供了 import/include 两种导入方式。

include vs. import

  • All import statements are pre-processed at the time playbooks are parsed.
  • All include statements are processed as they are encountered during the execution of the playbook.

不同的地方是 import 导入后做了预处理,可能更快些。

Importing and Including Playbooks

# cat playbooks/import_and_include.yml
- hosts: servers
  tasks:
    - import_tasks: apache.yml
      remote_user: root
      vars:
        package_name: httpd
    - include_tasks: apache.yml
      vars:
        package_name: httpd

# cat playbooks/apache.yml
- yum: name={{ package_name }} state=latest
  sudo: yes
- service: name={{ package_name }} state=started
  sudo: yes

被导入的文件不需要指定机器,只需执行任务就行了。

相关文章

网友评论

      本文标题:2. Including and Importing

      本文链接:https://www.haomeiwen.com/subject/bjvxdqtx.html