0% found this document useful (0 votes)
18 views1 page

Ansible Playbook To Run The Commands 2 Output File With Hostname

This document provides instructions for customizing an Ansible playbook that runs commands on specified server groups. Users are guided to update file paths, output filenames, and target server groups in the playbook. It also includes the command to execute the playbook after saving it to a file.

Uploaded by

afba2331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

Ansible Playbook To Run The Commands 2 Output File With Hostname

This document provides instructions for customizing an Ansible playbook that runs commands on specified server groups. Users are guided to update file paths, output filenames, and target server groups in the playbook. It also includes the command to execute the playbook after saving it to a file.

Uploaded by

afba2331
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

'''

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

- name: Run commands on servers


shell: "{{ item }}"
register: command_output
with_items: "{{ commands_file.content | b64decode | split('\n') }}"
ignore_errors: true

- name: Store command output


copy:
content: "{{ item.stdout }}"
dest: "{{ inventory_hostname }}_output.txt"
with_items: "{{ command_output.results }}"

You might also like