如何在ansible中重命名或重新映射键
How rename or remap key in ansible
我正在尝试将 geerlingguy 的 this repo(谢谢你!)与我实际的东西一起使用
但是我的文件中的变量不一样,不能更改它们,因为它们被其他任务使用...
我的结构实际上是这样的:
apache2:
vhost:
- name: toto.com
add_vhost: true
add_ssl: true
- name: AAA.toto.com
add_vhost: true
add_ssl: true
- name: BBB.toto.com
add_vhost: true
add_ssl: true
- name: CCC.toto.com
add_vhost: true
add_ssl: true
我重复此任务以使用我的变量 (source),但我的变量名为 cert_item.name
我必须翻译成 cert_item.domains
才能完成任务 in this file 工作。
- include_tasks: create-cert-standalone.yml
with_items:
- "{{ apache2.vhost }}"
when:
- certbot_create_if_missing
- certbot_create_method == 'standalone'
- cert_item_apache2.add_ssl | d(true)
loop_control:
loop_var: cert_item
我怎样才能把cert_item.name
传给任务他读cert_item.domains
?
您可以先创建列表 certbot_certs 以包含任务 create-cert-standalone.yml
---
- hosts: localhost
gather_facts: false
vars:
certbot_create_if_missing: true
certbot_create_method: "standalone"
apache2:
vhost:
- name: toto.com
add_vhost: true
add_ssl: true
- name: AAA.toto.com
add_vhost: true
add_ssl: true
- name: BBB.toto.com
add_vhost: true
add_ssl: true
- name: CCC.toto.com
add_vhost: true
add_ssl: true
tasks:
- name: SET_FACTS | Populate attribute domains
vars:
certbot_entry: "{{ item.name }}"
set_fact:
certbot_certs: "{{ certbot_certs |default([]) + [certbot_entry] }}"
loop: "{{ apache2.vhost }}"
when:
- certbot_create_if_missing
- certbot_create_method == 'standalone'
- item.add_ssl | d(true)
- include_tasks: create-cert-standalone.yml
with_items: "{{ certbot_certs }}"
when:
- certbot_create_if_missing
- certbot_create_method == 'standalone'
loop_control:
loop_var: cert_item
我的创建证书-standalone.yml 文件:
---
- name: "Create cert standalone {{ cert_item }}"
debug:
msg: "{{ cert_item }}"
这里是输出:
PLAY [localhost] *******************************************************************************
TASK [SET_FACTS | Populate attribute domains] *******************************************************************************
ok: [localhost] => (item={'name': 'toto.com', 'add_vhost': True, 'add_ssl': True})
ok: [localhost] => (item={'name': 'AAA.toto.com', 'add_vhost': True, 'add_ssl': True})
ok: [localhost] => (item={'name': 'BBB.toto.com', 'add_vhost': True, 'add_ssl': True})
ok: [localhost] => (item={'name': 'CCC.toto.com', 'add_vhost': True, 'add_ssl': True})
TASK [include_tasks] *******************************************************************************
included: /mnt/c/Users/tonip/ansible_test_yaml/create-cert-standalone.yml for localhost
included: /mnt/c/Users/tonip/ansible_test_yaml/create-cert-standalone.yml for localhost
included: /mnt/c/Users/tonip/ansible_test_yaml/create-cert-standalone.yml for localhost
included: /mnt/c/Users/tonip/ansible_test_yaml/create-cert-standalone.yml for localhost
TASK [Create cert standalone toto.com] *******************************************************************************
ok: [localhost] => {
"msg": "toto.com"
}
TASK [Create cert standalone AAA.toto.com] *******************************************************************************
ok: [localhost] => {
"msg": "AAA.toto.com"
}
TASK [Create cert standalone BBB.toto.com] *******************************************************************************
ok: [localhost] => {
"msg": "BBB.toto.com"
}
TASK [Create cert standalone CCC.toto.com] *******************************************************************************
ok: [localhost] => {
"msg": "CCC.toto.com"
}
我正在尝试将 geerlingguy 的 this repo(谢谢你!)与我实际的东西一起使用
但是我的文件中的变量不一样,不能更改它们,因为它们被其他任务使用...
我的结构实际上是这样的:
apache2:
vhost:
- name: toto.com
add_vhost: true
add_ssl: true
- name: AAA.toto.com
add_vhost: true
add_ssl: true
- name: BBB.toto.com
add_vhost: true
add_ssl: true
- name: CCC.toto.com
add_vhost: true
add_ssl: true
我重复此任务以使用我的变量 (source),但我的变量名为 cert_item.name
我必须翻译成 cert_item.domains
才能完成任务 in this file 工作。
- include_tasks: create-cert-standalone.yml
with_items:
- "{{ apache2.vhost }}"
when:
- certbot_create_if_missing
- certbot_create_method == 'standalone'
- cert_item_apache2.add_ssl | d(true)
loop_control:
loop_var: cert_item
我怎样才能把cert_item.name
传给任务他读cert_item.domains
?
您可以先创建列表 certbot_certs 以包含任务 create-cert-standalone.yml
---
- hosts: localhost
gather_facts: false
vars:
certbot_create_if_missing: true
certbot_create_method: "standalone"
apache2:
vhost:
- name: toto.com
add_vhost: true
add_ssl: true
- name: AAA.toto.com
add_vhost: true
add_ssl: true
- name: BBB.toto.com
add_vhost: true
add_ssl: true
- name: CCC.toto.com
add_vhost: true
add_ssl: true
tasks:
- name: SET_FACTS | Populate attribute domains
vars:
certbot_entry: "{{ item.name }}"
set_fact:
certbot_certs: "{{ certbot_certs |default([]) + [certbot_entry] }}"
loop: "{{ apache2.vhost }}"
when:
- certbot_create_if_missing
- certbot_create_method == 'standalone'
- item.add_ssl | d(true)
- include_tasks: create-cert-standalone.yml
with_items: "{{ certbot_certs }}"
when:
- certbot_create_if_missing
- certbot_create_method == 'standalone'
loop_control:
loop_var: cert_item
我的创建证书-standalone.yml 文件:
---
- name: "Create cert standalone {{ cert_item }}"
debug:
msg: "{{ cert_item }}"
这里是输出:
PLAY [localhost] *******************************************************************************
TASK [SET_FACTS | Populate attribute domains] *******************************************************************************
ok: [localhost] => (item={'name': 'toto.com', 'add_vhost': True, 'add_ssl': True})
ok: [localhost] => (item={'name': 'AAA.toto.com', 'add_vhost': True, 'add_ssl': True})
ok: [localhost] => (item={'name': 'BBB.toto.com', 'add_vhost': True, 'add_ssl': True})
ok: [localhost] => (item={'name': 'CCC.toto.com', 'add_vhost': True, 'add_ssl': True})
TASK [include_tasks] *******************************************************************************
included: /mnt/c/Users/tonip/ansible_test_yaml/create-cert-standalone.yml for localhost
included: /mnt/c/Users/tonip/ansible_test_yaml/create-cert-standalone.yml for localhost
included: /mnt/c/Users/tonip/ansible_test_yaml/create-cert-standalone.yml for localhost
included: /mnt/c/Users/tonip/ansible_test_yaml/create-cert-standalone.yml for localhost
TASK [Create cert standalone toto.com] *******************************************************************************
ok: [localhost] => {
"msg": "toto.com"
}
TASK [Create cert standalone AAA.toto.com] *******************************************************************************
ok: [localhost] => {
"msg": "AAA.toto.com"
}
TASK [Create cert standalone BBB.toto.com] *******************************************************************************
ok: [localhost] => {
"msg": "BBB.toto.com"
}
TASK [Create cert standalone CCC.toto.com] *******************************************************************************
ok: [localhost] => {
"msg": "CCC.toto.com"
}