Use of register variable in following play(s)

27 views
Skip to first unread message

Dimitri Yioulos

unread,
May 21, 2024, 3:31:14 PMMay 21
to Ansible Project
Good day.

I need to make changes in the know_hosts files of users on various hosts. I'll use a script to do the actual changes in known_hosts. I have the following simple playbook, so far. It identifies users with known_host files:

---

- hosts: all
  gather_facts: false
  become: yes

  tasks:

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

    - debug: msg="{{ known.stdout }}"

It produces the following output:

ok: [host1] =>
  msg: |-
    /root/.ssh/known_hosts
    /home/user1/.ssh/known_hosts
    /home/user2/.ssh/known_hosts
    /home/user3/.ssh/known_hosts
ok: [host2] =>
  msg: |-
    /root/.ssh/known_hosts
    /home/user1/.ssh/known_hosts
    /home/user2/.ssh/known_hosts
    /home/user3/.ssh/known_hosts
    /home/user4/.ssh/known_hosts
    /home/user5/.ssh/known_hosts
ok: [host3] =>
  msg: /root/.ssh/known_hosts
ok: [host4] =>
  msg: |-
    /root/.ssh/known_hosts
    /home/user1/.ssh/known_hosts
    /home/user2/.ssh/known_hosts
    /home/user3/.ssh/known_hosts
~

I want to apply the script against the known_hosts file for each of the identified users. How do I do that?

Dick Visser

unread,
May 21, 2024, 4:07:19 PMMay 21
to ansible...@googlegroups.com
I would use the dedicated find task. Apply a depth filter just in case.
Something like this should do the trick:


- name: known hosts script play
  hosts: all
  become: true
  gather_facts: false

  tasks:
    - name: Find known hosts
      ansible.builtin.find:
        paths:
          - /root
          - /home
        patterns: known_hosts
        recurse: true
        depth: 3
      register: found

    - name: Change known_host file
      ansible.builtin.script:
        cmd: foo.sh "{{ item }}"
      loop: "{{ found.files|map(attribute='path') }}"



foo.sh is a script on your controller, adjacent to your playbook.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/623b5aa3-d20b-4648-b169-d2cce2b00cadn%40googlegroups.com.

Dimitri Yioulos

unread,
May 21, 2024, 4:34:20 PMMay 21
to Ansible Project
Thank you, Dick!
Reply all
Reply to author
Forward
0 new messages