Ansible Playbook To Run The Commands 2 Output File With Hostname
Ansible Playbook To Run The Commands 2 Output File With Hostname
Make sure to update the following parts according to your specific needs:
Replace commands.txt with the path to the text file containing the commands you
want to run on the servers.
Modify {{ inventory_hostname }}_output.txt to the desired path and filename for
storing the command output. You can change it to a fixed filename if you don't want
to use the inventory hostname.
Adjust the hosts value to specify the server groups from your inventory file that
you want to target. For example, you can replace all with a specific group name
like web_servers.
Depending on your inventory file format, you may need to ensure that the groups and
server hostnames/IP addresses are defined correctly.
To run this playbook, save it to a file (e.g., command_playbook.yml) and execute
the following command:
shell
Copy code
ansible-playbook -i inventory_file command_playbook.yml
Replace inventory_file with the path to your inventory file.
'''
---
- name: Run commands on server groups
hosts: all
gather_facts: false
tasks:
- name: Read commands from file
slurp:
src: commands.txt
register: commands_file