'Ansible : how to read hosts from txt or CSV or JSON file

I have written the below playbook to uninstall agents on linux hosts and it is working, currently its reading hosts from /etc/ansible/hosts file(hardcoded) but I want to read hosts dynamically from txt/csv/json files while running playbook, please suggest.

playbook

---
- hosts: Linux
  name: "Uninstall oneAgent on Linux hosts."
  tasks:

   - name: stop oneAgent service
     become: true
     ansible.builtin.service:
      name: oneagent.service
      state: stopped
     register: result3

   - name: Printing service status
      debug:
        msg: "{{result3}}"

   - name: "Uninstall oneAgent"
     become: true
     command: "sh /opt/dynatrace/oneagent/agent/uninstall.sh"
     register: result1

   - name: Printing Uninstallation status
     debug:
       msg: "{{result1}}"

   - name: Declaring variable stdout_lines
     debug:
       var: result1.stdout_lines

   - name: Printing Uninstallation status with stdout_lines
     debug:
       msg: "{{result1.stdout_lines}}"

   - name: "Clear oneAgent config files"
     become: true
     ansible.builtin.file:
       path: /var/lib/dynatrace/
       state: absent
     register: result1

   - name: "printing oneAgent config files clear status"
     debug:
       msg: "{{result1}}"

   - name: "Clear oneAgent log files"
     become: true
     ansible.builtin.file:
       path: /var/log/dynatrace/
       state: absent
     register: result2

   - name: "printing oneAgent log files clear status"
     debug:
       msg: "{{result2}}"


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source