【问题标题】:How rename or remap key in ansible如何在ansible中重命名或重新映射键
【发布时间】:2021-10-16 18:34:03
【问题描述】:

我正在尝试将 geerlingguythis 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

【问题讨论】:

    标签: ansible yaml certbot


    【解决方案1】:

    您可以在之前创建列表 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
    

    我的 create-cert-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"
    }
    

    【讨论】:

    • 您对“set_fact”的回答对我有帮助!
    猜你喜欢
    • 2010-10-05
    • 2019-04-15
    • 2013-02-23
    • 2010-10-13
    • 1970-01-01
    • 2014-03-05
    • 2021-09-04
    • 2013-11-29
    • 2020-10-03
    相关资源
    最近更新 更多