我有以下数据作为输入:
list_domains:
domain1:
server_name: "domain1.com"
server_alias:
- "domain1.fr"
certificate:
method: "webroot"
php_custom_values:
memory_limit: "128M"
database:
type: "mysql"
cms:
type: "prestashop"
domain2:
server_name: domain2.com
php_custom_values:
memory_limit: "512M"在多个主机上创建多个域。
我使用此循环来创建所有域配置:
tasks:
- name: "Create domain configuration"
include_role:
name: webserver/domain
loop: "{{ list_domains| dict2items }}"
loop_control:
loop_var: domain但我想只更新一个域配置,当行动手册运行时,域名作为额外的变量
例如:
ansible-playbook domains.yml -e target=domain1但我没有设法从字典中提取一个条目并在角色中使用它。
发布于 2020-12-10 02:24:50
简而言之,就是让你走上正轨
---
- name: Not fully tested example only playbook
hosts: localhost
gather_facts: false
vars:
# Your original data
list_domains: <your full dict as in example>
# The loopable list of domains
my_list: "{{ list_domains| dict2items }}"
tasks:
- name: Filter out the list if target is defined
set_fact:
my_list: "{{ my_list | selectattr('key', '==', target) }}"
when: target is defined
- name: Do the job with the list
include_role:
name: webserver/domain
loop: "{{ my_list }}"
loop_control:
loop_var: domain发布于 2020-12-10 17:15:43
添加| list会使工作
下面是完整的过滤器:
- name: Filter out the list if target is defined
set_fact:
my_list: "{{ my_list | selectattr('key', '==', target) | list }}"
when: target is definedhttps://stackoverflow.com/questions/65222017
复制相似问题