Ansible - write only specific output

33 views
Skip to first unread message

DM

unread,
Feb 13, 2019, 8:45:06 AM2/13/19
to Ansible Project
How do you write to a file the output of a yaml?

If possible, how do you only write to a file specific info if?

Thanks

ryad9...@gmail.com

unread,
Feb 13, 2019, 10:26:58 AM2/13/19
to Ansible Project
Hi,

You must create file with extension .yml and beginning this file with "---" at the top.
It's simply.

And launch file with line command "ansible-playbook my_file.yml"

This answer answers your question ?

Thanks

DM

unread,
Feb 13, 2019, 11:46:48 AM2/13/19
to Ansible Project
Thanks I do have the "---" at the top
When I run t he ansible-playbook how do get all that info to write to a file?
It only shows up on  the cli which I know I can copy and paste to a file, but would like to output to a file

Thanks

ryad9...@gmail.com

unread,
Feb 13, 2019, 12:08:51 PM2/13/19
to Ansible Project
Hi,

If you want include task in your playbook, you must only do it :

- include_task: /my_path/.../.../my_file_externe.yml

This answer answers your question ?

Thanks

Will McDonald

unread,
Feb 13, 2019, 12:37:48 PM2/13/19
to ansible...@googlegroups.com
There's nothing in the ansible CLI that supports explicitly dumping the output to a file. You could just use a simple shell redirect.

$ ansible -i inventories/aws -m setup all > host-facts.yaml

It's worth noting that the CLI's output doesn't make a distinction between failed plays or tasks in terms of shell standard in/standard out, all the output goes to standard out (unless it's something to do with command line parsing or something lower-level than play execution).

So for example if I remove a pubkey on a host and run the setup module to gather facts, all the output goes to STDOUT:

xx.xx.xx.xx | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ec2-...@xx.xx.xx.xx: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).\r\n",
    "unreachable": true
}
yy.yy.yy.yy | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "aa.aa.aa.aa"
        ],

<snip>





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/35a3b1fc-da7b-4f4b-a2be-ebd9bf89c49b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

DM

unread,
Feb 13, 2019, 12:55:37 PM2/13/19
to Ansible Project
Thanks to all who responded.

The example below is a yaml file to gather data from a Juniper Networks Switch device.
How would you do it this?


- name: Get Facts
  hosts: 192.168.1.1
  roles:
    - Juniper.junos
  connection: local
  gather_facts: no
  vars_prompt:
    - name: ADMUSER
      prompt: Username
      private: no
    - name: ADMPASS
      prompt: password
      private: yes
  tasks:
    - name: Retrieve LLDP Neighbor Information Using PyEZ-included Table
      juniper_junos_table:
        file: "lldp.yml"
        table: "RouteTable"
        host: '{{ inventory_hostname }}'
        user: '{{ ADMUSER }}'
        passwd: '{{ ADMPASS }}'
        port: 830
      register: response
    - name: Print response
      debug:
        var: response
    - copy: content="{{ your_json_feed }}" dest=/etc/ansible/file-1





On Wednesday, February 13, 2019 at 8:45:06 AM UTC-5, DM wrote:

Hugo Gonzalez

unread,
Feb 13, 2019, 1:37:56 PM2/13/19
to ansible...@googlegroups.com

Hello,

I've seen this use case before. It's easier to build the reporting directly into the playbook. You can, for exmaple, create a template that will print what you want for each managed host, and assemble the pieces later:

On 2/13/19 11:55 AM, DM wrote:

The example below is a yaml file to gather data from a Juniper Networks Switch device.
How would you do it this?


I've seen this exact same use case. It's better to build the reporting directly into the playbook. You can, for exaple, create a template that will print what you want for each managed host, and assemble the pieces later:

Something like adding a template file like:

{{ inventory_hostname }} :
{{ response }}


Then templating it:

- name: create part of the report for {{ inventory_hostname }}
  template:
    src: my_template.j2
    dest: "{{ inventory_hostname.report }}"


Then you can use the  fetch module to bring it over to the controller and the assemble module to generate a full report.


Hope that helps,


Hugo








On Wednesday, February 13, 2019 at 8:45:06 AM UTC-5, DM wrote:
How do you write to a file the output of a yaml?

If possible, how do you only write to a file specific info if?

Thanks

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Hugo F. gonzalez

Senior Consultant

Red Hat LATAM

Reply all
Reply to author
Forward
0 new messages