0

I have made a script which will put the hostname and ip adress into a file. My script is below:

echo "*********************************************">>a.txt;

hostname>>a.txt; 

/sbin/ifconfig>>a.txt

This script is working fine locally.

There are 1000 servers in our environment. My target is to fetch the hostname and ip address of all the servers in a local file(a.txt is a file in which I am executing ansible command). For this I am using ansible.

I am running this command:

$ ansible test -m script -a ankush.sh 

The Command is executing but I can see my local hostname and ip address of local system in "a.txt". I want the remote system hostname and remote ip address.

2

With ansible you run all your scripts remotely, so you will have a.txt file on every server.
I could suggest next options:

  1. Change your script to print results, so you will get all output running ansible and after that parse that output.

  2. Use ansible fetch module to fetch all a.txt after running scripts, for example: ansible test -m fetch -a "src=a.txt dest=out/"

  3. Use ansible setup module to fetch hostnames and ip addresses, for example: ansible test -m setup -a 'filter=ansible_hostname' and more details on http://docs.ansible.com/ansible/setup_module.html

  • @ankush-kalra There a lot of ways to do step 2 without ansible like using "PSSH", "Gnu Parallel" or other software just explain whats wrong with ansible for that so it will be easier to suggest alternative solution. p.s. if my answer helps you just "accept it" or add more details in comments if you have more issues with suggested solution. – Fedor Dikarev Nov 19 '16 at 12:32
0

Perhaps you should try filtering through the ansible collected facts using the ansible setup module without writing any scripts.

Try the following

ansible test -m setup -a 'filter=ansible_all_ipv*_addresses' > ip_addrs.txt
ansible test -m setup -a 'filter=ansible_hostname' > hostnames.txt

This is probably what you actually want, but might output a bit too much data:

ansible test -m setup - 'gather_subset=network'
0

Please try creating yaml files like below and run the yaml file

- hosts: <Mention the inventory name>
  tasks:
  - name: use script module to execute local script on remote
    script: /ankush.sh 
    register: scriptoutput
  - name: local copy
    local_action: copy content="{{ scriptoutput.stdout }}" dest=/home/ankush.txt

In the script mention only the commands, so that the commands output will redirect to ankush.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.