Thứ Tư, 28 tháng 4, 2021

Ansible : Ví dụ về cấu hình hóa các biến

Khi viết các role cho ansible, nhiều khi ta muốn tạo biến để có thể tùy biến nội dung muốn thực hiện nhằm cho phép thêm/bớt nội dung cho việc thực hiện một vòng lặp. Dưới đây là một vài ví dụ :

Ví dụ 1

- name: dowload module jars to /tmp
get_url:
url: "{{ item }}"
dest: "/tmp/"
loop_control:
label: "download {{ item }} to /tmp/"
loop:
- "{{ download_jar_url }}/ojdbc8-12.2.0.1.jar"
- "{{ download_jar_url }}/logback-core-1.2.3.jar"
- "{{ download_jar_url }}/logback-classic-1.2.3.jar"
- "{{ download_jar_url }}/eclipselink-2.7.5.jar"

- name: add modules
become: yes
shell:
"{{ wildfly_bin_cli }} --connect command=\"module add --name={{ item.name }} --resource-delimiter=, --resources={{ item.resources }} --dependencies={{ item.dependencies }} \""
loop_control:
label: "add module {{ item.name }} with {{ item.resources }}"
loop:
- { name: "com.antonyudin.wildfly.security", resources: "/tmp/ay-securityrealm-ejb-1.0.jar", dependencies: "org.wildfly.security.elytron,org.wildfly.extension.elytron"}
- { name: "com.oracle", resources: "/tmp/ojdbc8-12.2.0.1.jar", dependencies: "javax.api,javax.transaction.api"}
- { name: "ch.qos.logback", resources: "/tmp/logback-classic-1.2.3.jar,/tmp/logback-core-1.2.3.jar", dependencies: "org.slf4j,javax.api,javax.transaction.api"}
- { name: "org.eclipse.persistence", resources: "/tmp/eclipselink-2.7.5.jar", dependencies: "asm.asm,javax.api,javax.annotation.api,javax.enterprise.api,javax.persistence.api,javax.transaction.api,javax.validation.api,javax.xml.bind.api,javax.ws.rs.api,org.antlr,org.apache.commons.collections,org.dom4j,org.jboss.as.jpa.spi,org.jboss.logging,org.jboss.vfs"}

 

Ta có thể định nghĩa lại biến cho vòng lặp trên như sau :

wildfly_bin_cli: /opt/wildfly/bin/jboss-cli.sh 
wildfly_modules_to_install:
- name: "ch.qos.logback"
resources:
- {jar_name: "logback-classic-1.2.3.jar", download_url: "{{ download_jar_url }}/logback-classic-1.2.3.jar"}
- {jar_name: "logback-core-1.2.3.jar", download_url: "{{ download_jar_url }}/logback-core-1.2.3.jar"}
dependencies: ["org.slf4j", "javax.api", "javax.transaction.api"]
- name: "com.oracle"
resources:
- { jar_name: "ojdbc8-12.2.0.1.jar", download_url: "{{ download_jar_url }}/ojdbc8-12.2.0.1.jar"}
dependencies: ["javax.api" , "javax.transaction.api"]
- name: "org.eclipse.persistence"
resources:
- {jar_name: "eclipselink-2.7.5.jar", download_url: "{{ download_jar_url }}/eclipselink-2.7.5.jar"}
dependencies: ["asm.asm", "javax.api", "javax.annotation.api", "javax.enterprise.api", "javax.persistence.api", "javax.transaction.api", "javax.validation.api", "javax.xml.bind.api", "javax.ws.rs.api,org.antlr", "org.apache.commons.collections", "org.dom4j", "org.jboss.as.jpa.spi", "org.jboss.logging", "org.jboss.vfs"]

 

Đoạn code để thực hiện trong vòng lặp trên sẽ được viết trong tập tin install_module.yml, và dưới đây là đoạn code để đưa biến vào vòng lặp

- name: install modules
include: install_module.yml
vars:
list_jar_module: "{{ module_item.resources | map(attribute='download_url') | list }}"
module_name: "{{ module_item.name }}"
module_path: "{{ module_item.name | replace('.', '/')}}"
list_resources: "{{ module_item.resources | map(attribute='jar_name') | list }}"
module_resources: "{{ ['/tmp/'] | product(list_resources) | map('join') | join(',') }}"
module_dependencies: "{{ module_item.dependencies | join(',') }}"
with_items: "{{ wildfly_modules_to_install }}"
loop_control:
loop_var: module_item

Nội dung của tập tin install_module.yml

- name: dowload {{ module_name }} module jars to /tmp
get_url:
url: "{{ item }}"
dest: "/tmp/"
loop_control:
label: "download {{ item }} to /tmp/"
loop: "{{ list_jar_module }}"


- name: "add module {{ module_name }} ; resources {{ module_resources }} ; dependencies {{ module_dependencies }}"
become: yes
shell:
"{{ wildfly_bin_cli }} --connect command=\"module add --name={{ module_name }} --resource-delimiter=, --resources={{ module_resources }} --dependencies={{ module_dependencies }} \""

Ví dụ 2

Định nghĩa biến :

wildfly_modules_base: "/opt/wildfly/modules/system/layers/base" 
wildfly_modules_to_configure:
- name: "org.slf4j"
xmlns: "urn:jboss:module:1.6"
resources: ["slf4j-api-1.7.22.jbossorg-1.jar"]
dependencies: ["ch.qos.logback"]
file_path: "{{ wildfly_modules_base }}/org/slf4j/main/module.xml"
- name: "org.jboss.logging"
xmlns: "urn:jboss:module:1.6"
resources: ["jboss-logging-3.4.1.Final.jar"]
dependencies: ["org.slf4j", "ch.qos.logback"]
file_path: "{{ wildfly_modules_base }}/org/jboss/logging/main/module.xml"

Gọi vòng lặp :

- name: update module.xml files
include: configure.yml
vars:
module_name: "{{ module_item.name }}"
module_xmlns: "{{ module_item.xmlns }}"
module_resources: "{{ module_item.resources }}"
module_dependencies: "{{ module_item.dependencies }}"
module_file_path: "{{ module_item.file_path }}"
with_items: "{{ wildfly_modules_to_configure }}"
loop_control:
loop_var: module_item

 

Nội dung tập tin configure.yml

- name: update {{ module_name }} module file {{ module_file_path }}
template:
src: "module.xml.j2"
dest: "{{ module_file_path }}"

 

Nội dung tập tin template module.xml.j2

<module name="{{ module_name }}" xmlns="{{ module_xmlns }}">
<resources>
{% for item in module_resources %}
<resource-root path="{{ item }}"/>
{% endfor %}
</resources>
<dependencies>
{% for item in module_dependencies %}
<module name="{{ item }}"/>
{% endfor %}
</dependencies>
</module>

 

Không có nhận xét nào:

Đăng nhận xét