REQUIREMENT:
Write an ansible script ,that will
1. Copy the script collect_info.sh to all the hosts.
1. Run the shell script ( collect_info.sh ) , against the hosts mentioned in host file.
2. Script will be run as root user.
3. The output file of the script ( will be like collect*txt) need to be copied to local host.
Below is the ansible playbook script:
cat collect_info.yml
– hosts: all
strategy: free
user: bvunix
become: yes
become_method: sudo
become_user: root
tasks:
– name: Copy script collect_audit.sh
copy: src=collect_audit.sh dest=/home/bvunix mode=0777
– name: Run script for audit
command: sh /home/bvunix/collect_audit.sh
– name: find the output file
shell: (cd /home/bvunix; find collect*.txt)
register: files_to_copy
– debug:
var: files_to_copy
– name: Fetch the file from remote to local
fetch: src=/home/bvunix/{{ item }} dest=/home/bvunix/ mode=0777 flat=yes
with_items: “{{ files_to_copy.stdout_lines }}”
– Execute the playbook
Ansible-playbook collect_info.yml -f 10
-HOSTFILE :
— If password less ssh connectivity is established, then use the below simple hostfile
172.20.192.1
172.20.192.2
172.20.192.3
— If no passwordless ssh setup is there, then hardcode the credentials as below:
linux1 ansible_host=172.20.192.1 ansible_connection=ssh ansible_user=bvunix ansible_ssh_pass=classic123
linux2 ansible_host=172.20.192.3 ansible_connection=ssh ansible_user=bvunix ansible_ssh_pass=classic123
linux3 ansible_host=172.20.192.3 ansible_connection=ssh ansible_user=bvunix ansible_ssh_pass=classic123
In the later tutorial, we will explain how to use ansible-vault to encrypt the host file.