官术网_书友最值得收藏!

  • Ansible Playbook Essentials
  • Gourav Shah
  • 278字
  • 2021-07-16 20:47:49

Creating the www playbook

We created a site-wide playbook and used an include statement to call another playbook by the name www.yml. We will now create this file with one play, which maps our web server hosts to the Nginx role:

---
#www.yml : playbook for web servers
- hosts: www
  remote_user: vagrant
  sudo: yes
  roles:
     - nginx

The above code works as follows:

  • Run this code on any host that maps to the [www] group specified in the hosts file.
  • For each directory inside the roles/nginx/* file, include roles/nginx/*/main.yml to the play. This includes tasks, handlers, vars, meta, default, and so on. This is where the auto include rules apply.

The default and custom role paths

By default, Ansible looks inside the roles/ subdirectory of the project that we create playbooks for. Being top-class devops engineers, we will follow the best practice to have a centralized, version-controlled repository to store all your roles. We may also end up reusing the roles created by community. Once we do this, we can reuse these roles across multiple projects. In such cases, we will check out the code at one or more locations, for example:

  • /deploy/ansible/roles
  • /deploy/ansible/community/roles

For nondefault paths, we will need to add the roles_path parameter to ansible.cfg as shown in the following command:

roles_path = /deploy/ansible/roles:/deploy/ansible/community/roles

Parameterizing the roles

At times, we may need to override default parameters specified inside vars or the default directory of a role, for example, running web servers on port 8080 instead of 80. In such cases, we can also pass parameters to roles in the preceding playbook, as follows:

---
#www.yml : playbook for web servers
- hosts: www
  roles:
- { role: nginx, port: 8080 }
主站蜘蛛池模板: 牡丹江市| 大新县| 治多县| 上饶市| 锡林郭勒盟| 武平县| 闵行区| 清徐县| 宝鸡市| 潢川县| 宁德市| 鹿泉市| 黎平县| 南平市| 大庆市| 呼图壁县| 郁南县| 鄂温| 普宁市| 策勒县| 保康县| 长白| 阳原县| 乌拉特中旗| 丁青县| 福清市| 光山县| 阳新县| 广饶县| 岗巴县| 乐都县| 荃湾区| 蒙阴县| 铜鼓县| 北海市| 武宣县| 临颍县| 太湖县| 法库县| 耿马| 广南县|