Docsite: improve user_guide/playbooks_reuse_roles (#71857)
* Docsite: improve user_guide/playbooks_reuse_roles * fix
This commit is contained in:
parent
2655fc0c23
commit
7a0a2f188b
1 changed files with 29 additions and 15 deletions
|
@ -51,22 +51,23 @@ You can add other YAML files in some directories. For example, you can place pla
|
||||||
.. code-block:: yaml
|
.. code-block:: yaml
|
||||||
|
|
||||||
# roles/example/tasks/main.yml
|
# roles/example/tasks/main.yml
|
||||||
- name: install the correct web server for RHEL
|
- name: Install the correct web server for RHEL
|
||||||
import_tasks: redhat.yml
|
import_tasks: redhat.yml
|
||||||
when: ansible_facts['os_family']|lower == 'redhat'
|
when: ansible_facts['os_family']|lower == 'redhat'
|
||||||
- name: install the correct web server for debian
|
|
||||||
|
- name: Install the correct web server for Debian
|
||||||
import_tasks: debian.yml
|
import_tasks: debian.yml
|
||||||
when: ansible_facts['os_family']|lower == 'debian'
|
when: ansible_facts['os_family']|lower == 'debian'
|
||||||
|
|
||||||
# roles/example/tasks/redhat.yml
|
# roles/example/tasks/redhat.yml
|
||||||
- install web server
|
- name: Install web server
|
||||||
yum:
|
ansible.builtin.yum:
|
||||||
name: "httpd"
|
name: "httpd"
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
# roles/example/tasks/debian.yml
|
# roles/example/tasks/debian.yml
|
||||||
- install web server
|
- name: Install web server
|
||||||
apt:
|
ansible.builtin.apt:
|
||||||
name: "apache2"
|
name: "apache2"
|
||||||
state: present
|
state: present
|
||||||
|
|
||||||
|
@ -174,11 +175,16 @@ To include a role:
|
||||||
---
|
---
|
||||||
- hosts: webservers
|
- hosts: webservers
|
||||||
tasks:
|
tasks:
|
||||||
- debug:
|
- name: Print a message
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: "this task runs before the example role"
|
msg: "this task runs before the example role"
|
||||||
- include_role:
|
|
||||||
|
- name: Include the example role
|
||||||
|
include_role:
|
||||||
name: example
|
name: example
|
||||||
- debug:
|
|
||||||
|
- name: Print a message
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: "this task runs after the example role"
|
msg: "this task runs after the example role"
|
||||||
|
|
||||||
You can pass other keywords, including variables and tags, when including roles:
|
You can pass other keywords, including variables and tags, when including roles:
|
||||||
|
@ -188,7 +194,8 @@ You can pass other keywords, including variables and tags, when including roles:
|
||||||
---
|
---
|
||||||
- hosts: webservers
|
- hosts: webservers
|
||||||
tasks:
|
tasks:
|
||||||
- include_role:
|
- name: Include the foo_app_instance role
|
||||||
|
include_role:
|
||||||
name: foo_app_instance
|
name: foo_app_instance
|
||||||
vars:
|
vars:
|
||||||
dir: '/opt/a'
|
dir: '/opt/a'
|
||||||
|
@ -205,7 +212,8 @@ You can conditionally include a role:
|
||||||
---
|
---
|
||||||
- hosts: webservers
|
- hosts: webservers
|
||||||
tasks:
|
tasks:
|
||||||
- include_role:
|
- name: Include the some_role role
|
||||||
|
include_role:
|
||||||
name: some_role
|
name: some_role
|
||||||
when: "ansible_facts['os_family'] == 'RedHat'"
|
when: "ansible_facts['os_family'] == 'RedHat'"
|
||||||
|
|
||||||
|
@ -219,11 +227,16 @@ You can reuse roles statically anywhere in the ``tasks`` section of a play using
|
||||||
---
|
---
|
||||||
- hosts: webservers
|
- hosts: webservers
|
||||||
tasks:
|
tasks:
|
||||||
- debug:
|
- name: Print a message
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: "before we run our role"
|
msg: "before we run our role"
|
||||||
- import_role:
|
|
||||||
|
- name: Import the example role
|
||||||
|
import_role:
|
||||||
name: example
|
name: example
|
||||||
- debug:
|
|
||||||
|
- name: Print a message
|
||||||
|
ansible.builtin.debug:
|
||||||
msg: "after we ran our role"
|
msg: "after we ran our role"
|
||||||
|
|
||||||
You can pass other keywords, including variables and tags, when importing roles:
|
You can pass other keywords, including variables and tags, when importing roles:
|
||||||
|
@ -233,7 +246,8 @@ You can pass other keywords, including variables and tags, when importing roles:
|
||||||
---
|
---
|
||||||
- hosts: webservers
|
- hosts: webservers
|
||||||
tasks:
|
tasks:
|
||||||
- import_role:
|
- name: Import the foo_app_instance role
|
||||||
|
import_role:
|
||||||
name: foo_app_instance
|
name: foo_app_instance
|
||||||
vars:
|
vars:
|
||||||
dir: '/opt/a'
|
dir: '/opt/a'
|
||||||
|
|
Loading…
Add table
Reference in a new issue