# Parsing a local file to get variables

**URL:** <https://forum.ansible.com/t/parsing-a-local-file-to-get-variables/38705>\
**Category:** Ansible Project\
**Created:** [March 6, 2024, 2:08pm UTC](https://forum.ansible.com/t/parsing-a-local-file-to-get-variables/38705 "2024-03-06T14:08:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 6, 2024, 2:08pm UTC](https://forum.ansible.com/t/parsing-a-local-file-to-get-variables/38705/1 "2024-03-06T14:08:56Z")

</div>

Good morning. Here’s a bit of a challenge. I’m working on a playbook to get certs from letsencrypt, then put those into zone files. Rather than use the Ansible letsencrypt module, we’ve written a Perl script to get the certs. For each domain that we get certs for, a separate file named -le.txt is created in /etc/ansible/playbooks/files. It looks like this:

**[mydomain.com](http://mydomain.com)**  
**cert1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**  
**cert2yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy**

I need to extract the certs and put them into the domain’s zone file. This is what the playbook looks like right now, where we currently have to paste in the certs, as can be seen from the two prompts. That’s what I’m trying to further automate:

---

<div class="post-metadata">

**Author:** ![Stu](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Stu](https://forum.ansible.com/u/Stu)\
**Post date:** [March 6, 2024, 4:01pm UTC](https://forum.ansible.com/t/parsing-a-local-file-to-get-variables/38705/2 "2024-03-06T16:01:45Z")

</div>

Probably dirty but something like reading out the file and setting facts based on the line?

- name: Read certificate file

shell: cat {{ certdir }}/{{ zonefile }}-le.txt

register: cert\_content

- name: Set cert variables from file

set\_fact:

acmechallenge1: “{{ cert\_content.stdout\_lines[1] }}”

acmechallenge2: “{{ cert\_content.stdout\_lines[2] }}”

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 6, 2024, 4:52pm UTC](https://forum.ansible.com/t/parsing-a-local-file-to-get-variables/38705/3 "2024-03-06T16:52:00Z")

</div>

Hey, Stuart. Yup, use of set\_fact was really the key (pardon the pun). Here’s the playbook, which now works perfectly, if it might be useful to anyone else (the debug stuff in it can, of course, be removed):

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 6, 2024, 8:33pm UTC](https://forum.ansible.com/t/parsing-a-local-file-to-get-variables/38705/4 "2024-03-06T20:33:00Z")

</div>

Ah, I thought I was done with this. But,now, running the Perl script failed. I guess the script module only works with bash scripts? So, that’s probably out. Among other things, I tried this, but it didn’t work:

tasks:

- name: Run scripts to create Letsencrypt keys  
local\_action: ansible.builtin.shell /etc/ansible/playbooks/files/renew\_le\_domain.pl {{ domain }}

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 6, 2024, 10:31pm UTC](https://forum.ansible.com/t/parsing-a-local-file-to-get-variables/38705/5 "2024-03-06T22:31:35Z")

</div>

Some additional info. With the above play, i get the following error:

**fatal: [dns1 → localhost]: FAILED! =\> changed=true**  
**cmd: /etc/ansible/playbooks/files/renew\_le\_domain.pl [mydomain.com](http://mydomain.com)**  
**delta: ‘0:00:00.019169’**  
**end: ‘2024-03-06 17:22:01.951852’**  
**msg: non-zero return code**  
**rc: 25**  
**start: ‘2024-03-06 17:22:01.932683’**  
**stderr: done**  
**stderr\_lines:**   
**stdout: ‘’**  
**stdout\_lines:**

The command " /etc/ansible/playbooks/files/renew\_le\_domain.pl [mydomain.com](http://mydomain.com)" appears correct (where " [mydomain.com](http://mydomain.com)" is an argument that the Perl script expects). The script has to run on the command node, and must generate a file file on the command node. When I run “last” on dns1, it looks like the ansible user has tried to connect to it.
