Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible write info about nodes to local csv file

I have written an ansible script that returns some information from various sources. One of the variables I am saving in a variable during a task is the number of records in a certain mysql database table.

I can print out results in the playbook quite well. What I want to do however is write the results from all hosts in a single (csv) file on the master/control server or computer running the playbook

  - name: Show results
    debug:
    msg: "URL: {{hostvars[inventory_hostname]['ansible_nodename']}} RECORDCOUNT: {{results.stdout}} BASE VERSION: {{baseversion.stdout}}"

this piece prints the info I want for 20 nodes. I now want to write this line in a single file on the master server. ( csv )

I've tried various things with local copy but until now to no avail

like image 980
Synbitz Prowduczions Avatar asked Aug 16 '17 14:08

Synbitz Prowduczions


People also ask

How do you capture command output in Ansible?

To capture the output, you need to specify your own variable into which the output will be saved. To achieve this, we use the 'register' parameter to record the output to a variable. Then use the 'debug' module to display the variable's content to standard out.

What is the csvfile plugin in Ansible?

This lookup plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name csvfile even without specifying the collections: keyword.

Can I use Ansible playbook with Catalyst 4500?

Actually no more data is needed. This ansible playbook is very basic, and can be used for small enterprise switches much like f.e. the Catalyst 1000, Catalyst 2000 and Catalyst 3000 series. This will not work with Catalyst 4500 and bigger modular switches, only because of the hardcoded flash name in the script.

How do I find the Red Hat version of Ansible?

Red Hat subscribers, select 2.9 in the version selection to the left for the most recent Red Hat release. This lookup plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name csvfile even without specifying the collections: keyword.

What is the default name of the CSV file to open?

name of the CSV/TSV file to open. The default is for TSV files (tab delimited) not CSV (comma delimited) … yes the name is misleading. As of version 2.11, the search parameter (text that must match the first column of the file) and filename parameter can be multi-word.


1 Answers

shell module can come up handy when no other solution visible:

  - name: Save results
    shell: echo URL: {{hostvars[inventory_hostname]['ansible_nodename']}} RECORDCOUNT: {{results.stdout}} BASE VERSION: {{baseversion.stdout}} >> /opt/my_file.log
    delegate_to: localhost
like image 63
Konstantin Suvorov Avatar answered Sep 28 '22 03:09

Konstantin Suvorov