0

I am currently trying to use the acme_certificate module from ansible to automatically generate a lets encrypt certificate for my servers. Since my service provider doesn't provide a route53 compatible endpoint I had to write my own python script to upload the DNS record including the challenge. Therefore I need to pass the challenge data (to be specific the challenge string itself) to the python script and I dont know how I can nest variables inside the dictionary in order to access the right element. Any ideas how to solve this problem (I tried to use lookup but ended up with having even more error)?

My Playbook:

---
- hosts: localhost
  vars:
    dns_name: sample.com

tasks:
- name: submit request
  acme_certificate:
    account_key_src: "{{ tmpdir }}{{ account_key }}"
    account_email: cert@{{ dns_name }}
    src: "{{ tmpdir }}{{ csrname }}"
    fullchain_dest: "{{ tmpdir }}{{ certname }}"
    challenge: dns-01
    acme_directory: https://acme-staging-v02.api.letsencrypt.org/directory
    acme_version: 2
    terms_agreed: yes
    remaining_days: 60
  register: challenge

- name: publish challenge as TXT record
  shell: "python3 /tmp/publish_dns.py --dns_name {{ dns_name }} {{ challenge.challenge_data['*.{{ dns_name }}']['dns-01'].resource_value }}"

When I run the playbook I get following error:

TASK [deploy_phishing : publish challenge as TXT record] 
***********************
fatal: [server1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute '*.{{ dns_name }}'\n\nThe error appears to be in '/var/lib/awx/projects/test/roles/generat_cert/tasks/tasks1.yml': line 76, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: publish challenge as TXT record\n  ^ here\n"}
ket
  • 39
  • 1
  • 7
  • I answered an almost identical question yesterday: [Use a var in a var in Ansible (lookup) - aws\_ssm plugin](https://stackoverflow.com/questions/58879307/use-a-var-in-a-var-in-ansible-lookup-aws-ssm-plugin) – larsks Nov 17 '19 at 13:35
  • I tried to use the lookup function as suggested in the your question and my solution would look like that: {{ lookup('vars', challenge.challenge_data['*.' + dns_name + '']['dns-01'].resource_value) }} – ket Nov 17 '19 at 14:04
  • However I do get the following error: TASK [deploy_phishing : publish challenge as TXT record] *********************** fatal: [ps1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: No variable found with this name: huchABCDbdcRHfumR_AjQxl_8XnSkzS6OEahwW_9FFw\n\nThe error appears to be in '/var/lib/awx/projects/test/roles/deploy_cert/tasks/tasks1.yml': line 76, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: publish challenge as TXT record\n ^ here\n"} – ket Nov 17 '19 at 14:05

0 Answers0