# How to access the results of "register: results"

**URL:** <https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793>\
**Category:** Get Help\
**Created:** [September 26, 2024, 1:07pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793 "2024-09-26T13:07:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpearceFL](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@dpearceFL](https://forum.ansible.com/u/dpearceFL)\
**Post date:** [September 26, 2024, 1:07pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/1 "2024-09-26T13:07:08Z")

</div>

I have a task to collect information from a bunch of servers. Here is what I have started with:

```auto
  tasks:
    - name: Check cert end date
      shell: |
        date
        hostname
        openssl x509 -enddate -noout -in /etc/pki/vdsm/libvirt-spice/ca-cert.pem | cut -d "=" -f 2
        date -r /etc/pki/vdsm/libvirt-spice/ca-cert.pem -u
      args:
        executable: /bin/bash
        warn: false
      register: result

    - name: Result
      debug:
        msg: "{{ result }}"

```

How do I itinerate over result to produce a CSV file, one line per server?

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [September 26, 2024, 2:24pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/2 "2024-09-26T14:24:59Z")

</div>

You should be able to have a task that delegates to localhost (if you want the CSV file local), and use a loop through the data in the variable + `ansible.builtin.lineinfile` to add lines

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 26, 2024, 4:26pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/4 "2024-09-26T16:26:13Z")

</div>

Don’t use lifeline, use a template with run\_once and loop over all hosts, lineinfile does not handle parallelism and will end up overwriting entries.

template

```auto
host, date, hostname, certificate
{% for host in ansible_play_hosts %}
{% if 'result' in hostvars[host] %}
{{host}}, {{ hostvars[host]['result']['stdout'].split('\n', 3) | join(',') }}
{% endif %}
{% endfor %}

```

use in play

```auto
    - name: Create csv.
      template:
        path: csvfile.csv
        src: mytemplate.j2
      delegate_to: localhost
      run_once: True

```

---

<div class="post-metadata">

**Author:** ![dpearceFL](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@dpearceFL](https://forum.ansible.com/u/dpearceFL)\
**Post date:** [September 26, 2024, 5:02pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/5 "2024-09-26T17:02:09Z")

</div>

Yup, lineinfile does not handle parallelism well.

---

<div class="post-metadata">

**Author:** ![dpearceFL](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@dpearceFL](https://forum.ansible.com/u/dpearceFL)\
**Post date:** [September 26, 2024, 5:04pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/6 "2024-09-26T17:04:27Z")

</div>

I tried this:

```auto
    - name: Create csv
      template:
        dest: "{{ output_fn }}"
        src: mytemplate.j2
      delegate_to: localhost
      run_once: True

```

But I only get four lines, not the 17 I was expecting.

---

<div class="post-metadata">

**Author:** ![dpearceFL](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@dpearceFL](https://forum.ansible.com/u/dpearceFL)\
**Post date:** [September 26, 2024, 5:08pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/7 "2024-09-26T17:08:15Z")

</div>

In looking at the output, I see four lines, all from the first server, one line for each piece of data I am looking for.

---

<div class="post-metadata">

**Author:** ![dpearceFL](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@dpearceFL](https://forum.ansible.com/u/dpearceFL)\
**Post date:** [September 26, 2024, 5:41pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/9 "2024-09-26T17:41:30Z")

</div>

OK, I am missing something really basic here.

If I print “result”, I get multiple elements(?), one for each server.

> To make it simple, I tried this:
> 
> ```auto
> - name: Result
> debug:
> msg: "{{ reand I get this:
> .stdout
> And }}"ok: [compute13.slc.csswx.nas.faa.gov] => {
> "msg": "Thu Sep 26 17:37:01 UTC 2024\ncompute13.slc.csswx.nas.faa.gov\nDec 2 21:24:48 2033 GMT\nTue Dec 5 22:29:42 UTC 2023"
> }
> ok: [compute14.slc.csswx.nas.faa.gov] => {
> "msg": "Thu Sep 26 17:37:00 UTC 2024\ncompute14.slc.csswx.nas.faa.gov\nDec 2 21:24:48 2033 GMT\nTue Dec 5 22:34:19 UTC 2023"
> }
> ok: [compute15.slc.csswx.nas.faa.gov] => {
> "msg": "Thu Sep 26 17:37:00 UTC 2024\ncompute15.slc.csswx.nas.faa.gov\nDec 2 21:24:48 2033 GMT\nTue Dec 5 22:38:
> 
> ```

18 UTC 2023"  
}

```auto
How do I access each of these elements (or whatever they are called) in a loop?
```

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 26, 2024, 5:53pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/12 "2024-09-26T17:53:10Z")

</div>

The `run_once` is only for the template action, not the shell one.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 26, 2024, 5:54pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/13 "2024-09-26T17:54:21Z")

</div>

@jrglynn2 don’t use `connection: local` this only works if controller and targets are configured the same, but for those in heterogeneous environments that breaks, use `delegate_to: localhost`

---

<div class="post-metadata">

**Author:** ![dpearceFL](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@dpearceFL](https://forum.ansible.com/u/dpearceFL)\
**Post date:** [September 26, 2024, 6:19pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/15 "2024-09-26T18:19:55Z")

</div>

Here is where I am now:

```
- name: Add data to CSV file
  ansible.builtin.lineinfile:
    path: "{{ output_fn }}"
    line: "{{ result.stdout_lines[0]|string, result.stdout_lines[1]|string,result.stdout_lines[2]|string,result.stdout_lines[3]|string }}"
    state: present
  with_items: slc_computes

```

But the output in the file is this:

`(u'Thu Sep 26 18:12:43 UTC 2024', u'compute01.slc.csswx.nas.faa.gov', u'Dec 2 21:24:48 2033 GMT', u'Tue Dec 5 21:28:15 UTC 2023')`  
Where are the “u’” coming from? How can I get rid of them and the parens?

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 26, 2024, 6:32pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/16 "2024-09-26T18:32:49Z")

</div>

the `u` is for ‘python unicode string’, it happens when you stringify some python objects using their default representation. The parens and the `u` show that you are stringifying a tuple, which you are creating with the `,` s

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 26, 2024, 6:46pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/19 "2024-09-26T18:46:54Z")

</div>

i’ve had problems with 2 forks, either your controller is slow parallelizing or your hosts have very different/stepped response times.

---

<div class="post-metadata">

**Author:** ![dpearceFL](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@dpearceFL](https://forum.ansible.com/u/dpearceFL)\
**Post date:** [September 26, 2024, 7:12pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/20 "2024-09-26T19:12:23Z")

</div>

What finally worked for me. Thanks for all your help.

```auto
    - name: Add data to CSV file
      connection: local
      ansible.builtin.lineinfile:
        path: "{{ output_fn }}"
        line: "{{ result.stdout_lines | list | join(',') }}"
        state: present
        insertafter: EOF
      with_items: servers

```

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 26, 2024, 9:44pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/21 "2024-09-26T21:44:05Z")

</div>

> [@jiholland](#):
>
> I have not encountered any problems the few times I have used `ansible.builtin.lineinfile` either.

Then you’ve gotten lucky (or just haven’t noticed when it failed to work.) This approach is not safe and _should not be used_ ever. Even if it seems to work perfectly for you it is inherently a flawed pattern that relies on luck to win the race.

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [September 27, 2024, 6:06am UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/22 "2024-09-27T06:06:06Z")

</div>

I´ve really only used `lineinfile` in conjunction with `delegate_to: localhost` and `run_once: true` as a gateway of last resort once - which should be ok if a specific module does not exist and `template` is unpractical. And yes, I would have noticed if it failed to work 🙂 However, I can see that parallel writing to the same file is not a good idea.

---

<div class="post-metadata">

**Author:** ![dpearceFL](https://avatars.discourse-cdn.com/v4/letter/d/73ab20/32.png) [@dpearceFL](https://forum.ansible.com/u/dpearceFL)\
**Post date:** [September 27, 2024, 2:02pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/23 "2024-09-27T14:02:46Z")

</div>

I totally understand why lineinfile is not a great option. I’ve never used templating before. I tried to use the code example you gave me but failed horribly. What other options do I have? I need to run this script against 250+ VMs to collect the data I need.

Thanks.

Dave

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 27, 2024, 3:21pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/25 "2024-09-27T15:21:34Z")

</div>

> [@jrglynn2](#):
>
> lineinfile is fine for certain use cases, terrible for others… like many Ansible modules LOL.

We are not talking about `lineinfile` in the abstract, we are talking about the concrete approach of using it to update a single file on localhost for multiple hosts, since that is what you suggested doing. Specifically, this was your suggestion:

```yaml
    - name: Add data to CSV file
      delegate_to: localhost
      ansible.builtin.lineinfile:
        path: "/tmp/CSV-FILE.csv"
        line: "{{ tags.Name, private_ip_address, item.path }}"
        state: present
      with_items: "{{ file_list.files }}"

```

This introduces a race condition if it’s in a play with multiple targets and _will_ eventually fail to record some data.

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 27, 2024, 3:34pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/26 "2024-09-27T15:34:31Z")

</div>

> [@dpearceFL](#):
>
> I’ve never used templating before. I tried to use the code example you gave me but failed horribly. What other options do I have?

Figure out why you failed horribly? It works just fine.

```yaml
- hosts: Class_mx
  tasks:
    - name: Check cert end date
      shell: |
        date
        hostname
        openssl x509 -enddate -noout -in /etc/pki/collab/private/mx.x.mail.umich.edu.ecc.crt | cut -d "=" -f 2
        date -r /etc/pki/collab/private/mx.x.mail.umich.edu.ecc.crt -u
      register: result

    - name: Create csv
      template:
        dest: csvfile.csv
        src: mytemplate.j2
      delegate_to: localhost
      run_once: true

```

```plaintext
$ ansible-playbook ~/test.yml                          
                                                                                
PLAY [Class_mx] ****************************************************************
                                                                                
TASK [Check cert end date] *****************************************************
changed: [hushed-kameosa.mx.x.mail.umich.edu]                                   
changed: [laureate-ungaikyo.mx.x.mail.umich.edu]                              
changed: [small-jatai.mx.x.mail.umich.edu]                                      
                                        
TASK [Create csv.] *************************************************************
changed: [laureate-ungaikyo.mx.x.mail.umich.edu -> localhost]                   
                                                                                                                              
PLAY RECAP *********************************************************************                                              
hushed-kameosa.mx.x.mail.umich.edu : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   
laureate-ungaikyo.mx.x.mail.umich.edu : ok=2 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0    
small-jatai.mx.x.mail.umich.edu : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0       
                                                               
$ cat ~/csvfile.csv                                                                                  
host, date, hostname, certificate                              
laureate-ungaikyo.mx.x.mail.umich.edu, Fri Sep 27 11:30:21 AM EDT 2024,laureate-ungaikyo.mx.x.mail.umich.edu,Dec 12 23:59:59 2024 GMT,Thu Sep 26 10:38:50 PM UTC 2024
small-jatai.mx.x.mail.umich.edu, Fri Sep 27 11:30:21 AM EDT 2024,small-jatai.mx.x.mail.umich.edu,Dec 12 23:59:59 2024 GMT,Thu Sep 26 10:38:50 PM UTC 2024
hushed-kameosa.mx.x.mail.umich.edu, Fri Sep 27 11:30:21 AM EDT 2024,hushed-kameosa.mx.x.mail.umich.edu,Dec 12 23:59:59 2024 GMT,Thu Sep 26 10:38:50 PM UTC 2024

```

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 27, 2024, 3:57pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/28 "2024-09-27T15:57:58Z")

</div>

Then you have not used the construct you suggested, got incredibly lucky, or just didn’t notice when it failed. This is not about my experience, it is an inherent flaw in how that task works.

```yaml
- hosts: aws_ec2
  tasks:
    - name: Remove CSV file
      delegate_to: localhost
      ansible.builtin.file:
        path: /tmp/CSV-FILE.csv
        state: absent

    - name: Add data to CSV file
      delegate_to: localhost
      ansible.builtin.lineinfile:
        path: "/tmp/CSV-FILE.csv"
        line: "{{ tags.Name, private_ip_address, item }}"
        state: present
        create: true
      loop:
        - foo
        - bar
        - baz

```

```plaintext
$ for i in {1..10} ; do ansible-playbook test.yml &>/dev/null; wc -l /tmp/CSV-FILE.csv ; done
48 /tmp/CSV-FILE.csv
47 /tmp/CSV-FILE.csv
48 /tmp/CSV-FILE.csv
45 /tmp/CSV-FILE.csv
48 /tmp/CSV-FILE.csv
48 /tmp/CSV-FILE.csv
48 /tmp/CSV-FILE.csv
48 /tmp/CSV-FILE.csv
47 /tmp/CSV-FILE.csv
47 /tmp/CSV-FILE.csv

```

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 27, 2024, 4:43pm UTC](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793/30 "2024-09-27T16:43:36Z")

</div>

> [@jrglynn2](#):
>
> I don’t understand the continued argument. I gave two example tasks that I have used, not including the larger playbook, for the OP to learn from, edit, and test.

The argument arises because you provided conceptually broken tasks that should not be learned from, and will cause problems for people that can be subtle and hard to notice. I don’t know why you’re still arguing that it’s a safe thing to do when it’s demonstrably not, as shown in my immediately previous post. I didn’t have to try to break it or jump through hoops to get those results, I just ran it ten times and it failed on 4 of them.

Don’t write code with race conditions. It’s a bad idea.

[Next page](https://forum.ansible.com/t/how-to-access-the-results-of-register-results/8793.md?page=2)
