Ansible - write only specific output

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

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

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

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

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-user@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”

],

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

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: