How can I store in a local file more than one line of what ansible playbook outputs?

Viewed 41

I'm trying to store what the playbook of ansible outputs to a local file. I think I already accomplished my goal but it should store more than one line. The playbook outputs two lines, but on the local file, only stores one

This is my playbook:

---
- hosts: all
  become: true
  tasks:


  - name: Comando para el hostname
    shell: hostname
    register: hostname_output


  - name: Mostrar el hostname
    debug:
      msg: "{{ hostname_output.stdout }}"


  - name: Comando para el timer
    shell: cat /etc/systemd/system/runrestic.timer | grep OnCalendar*
    register: timer_output


  - name: Mostrar el timer
    debug:
      msg: "{{ timer_output.stdout }}"


  - name: guardar en fichero local
    lineinfile:
      dest: /home/rvargas/register.txt
      line: "{{ hostname_output.stdout, timer_output.stdout }}"

    delegate_to: localhost

This is the ouput I get running the playbook:

rvargas@DESKTOP-4NPLJA6:/etc/ansible$ ansible-playbook --ask-become-pass oncalendar.yml
BECOME password:

PLAY [all] **************************************************************************

TASK [Gathering Facts] **************************************************************
ok: [192.168.1.68]
ok: [192.168.1.109]

TASK [Comando para el hostname] *****************************************************
changed: [192.168.1.109]
changed: [192.168.1.68]

TASK [Mostrar el hostname] **********************************************************
ok: [192.168.1.109] => {
    "msg": "grafana-prom"
}
ok: [192.168.1.68] => {
    "msg": "ansible"
}

TASK [Comando para el timer] ********************************************************
changed: [192.168.1.109]
changed: [192.168.1.68]

TASK [Mostrar el timer] *************************************************************
ok: [192.168.1.109] => {
    "msg": "OnCalendar=daily"
}
ok: [192.168.1.68] => {
    "msg": "OnCalendar=*-*-* 2:00:00"
}

TASK [guardar en fichero local] *****************************************************
changed: [192.168.1.109 -> localhost]
changed: [192.168.1.68 -> localhost]

PLAY RECAP **************************************************************************
192.168.1.109              : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.1.68               : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

And this is what is stored in my file

rvargas@DESKTOP-4NPLJA6:/etc/ansible$ cat /home/rvargas/register.txt                 
('ansible', 'OnCalendar=*-*-* 2:00:00')

This is what I actually expect:

rvargas@DESKTOP-4NPLJA6:/etc/ansible$ cat /home/rvargas/register.txt               
('ansible', 'OnCalendar=*-*-* 2:00:00')
('grafana-prom', 'OnCalendar=daily')
1 Answers

Given the variables

    - debug:
        msg: |-
          hostname_output.stdout: {{ hostname_output.stdout }}
          timer_output.stdout: {{ timer_output.stdout }}
TASK [debug] ****************************************************
ok: [192.168.1.109] => 
  msg: |-
    hostname_output.stdout: grafana-prom
    timer_output.stdout: OnCalendar=daily
ok: [192.168.1.68] => 
  msg: |-
    hostname_output.stdout: ansible
    timer_output.stdout: OnCalendar=*-*-* 2:00:00

For example, the task below

    - copy:
        dest: register.txt
        content: |-
          {% for i in hostvars|json_query(_query) %}
          ({% for j in i %}'{{ j }}'{% if not loop.last %}, {% endif %}{% endfor %})
          {% endfor %}
      vars:
        _query: "*.[hostname_output.stdout, timer_output.stdout]"
      delegate_to: localhost
      run_once: true

creates the file

shell> cat register.txt 
('grafana-prom', 'OnCalendar=daily')
('ansible', 'OnCalendar=*-*-* 2:00:00')

Optionally, add the host to each line

    - copy:
        dest: register.txt
        content: |-
          {% for i in hostvars|json_query(_query) %}
          {{ _hosts[loop.index0] }}: (
          {%- for j in i %}'{{ j }}'{% if not loop.last %}, {% endif %}{% endfor %})
          {% endfor %}
      vars:
        _query: "*.[hostname_output.stdout, timer_output.stdout]"
        _hosts: "{{ hostvars.keys()|list }}"
      delegate_to: localhost
      run_once: true

gives

shell> cat register.txt 
192.168.1.109: ('grafana-prom', 'OnCalendar=daily')
192.168.1.68: ('ansible', 'OnCalendar=*-*-* 2:00:00')
Related