Debug output as copy source

16 views
Skip to first unread message

Dimitri Yioulos

unread,
May 28, 2024, 11:34:03 AMMay 28
to Ansible Project
Hi, all.

I want to use the output of debug as the source to then create backups of that source (eek, I understand that; hope everyone else does). Here's the playbook relevant parts:

---

- hosts: host1,host2
  #- hosts: busdev-web-dev,busdev-web-qa,busdev-web-prod,bed-deploy1,bed-deploy2,uoj,uojclone2,uojdev,bed-crm-web-qa
  gather_facts: false
  become: yes

    #  vars:
    # script_path: <script>

  tasks:

    - name: Find known hosts
      shell: find /root /home -type f -name known_hosts
      register: known
      tags:
        - known

    - name: Debug known_hosts paths
      debug:
        msg: "{{ known.stdout }}"
      tags:
        - known

    - name: Set fact with known_hosts paths
      set_fact:
        known_hosts_paths: "{{ known.stdout_lines }}"
      tags:
        - known

    - name: Back up known_host files
      copy:
        src: "{{ known.stdout }}"
        dest: /tmp
        remote_src: true
        backup: yes
        #delegate_to: localhost
      tags:
        - known


I hope the idea is clear. I want to make backups of users' know_host files, which I've gleaned with the first play. My attempt not only doesn't work, but seems messy, regardless. As always, help much appreciated.

Dimitri Yioulos

unread,
May 28, 2024, 11:42:18 AMMay 28
to Ansible Project
Btw, the error output from the above is:

fatal: [bed-test-9-dy2]: FAILED! => changed=false
  msg: |-
    Source /root/.ssh/known_hosts
    /home/user1/.ssh/known_hosts
    /home/user2/.ssh/known_hosts
    /home/user3/.ssh/known_hosts
    /home/user4/.ssh/known_hosts not found

fatal: [bed-test-9-dy5]: FAILED! => changed=false
  msg: |-
    Source /root/.ssh/known_hosts
    /home/ user1/.ssh/known_hosts
    /home/user2/.ssh/known_hosts
    /home/user3/.ssh/known_hosts not found


Those files do exist.

Dimitri Yioulos

unread,
May 28, 2024, 12:35:38 PMMay 28
to Ansible Project
[SOLVED] Answered my own question, and, to simple really. Hope it helps someone:

    - name: Backup all known_hosts files
      shell: cp -p {{ item }} {{ item }}.bak
      loop: '{{ known.stdout_lines }}'
      tags:
        - known
Reply all
Reply to author
Forward
0 new messages