Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send the output from Ansible to a file [duplicate]

Tags:

ansible

I am trying to gain knowledge in Ansible and solve a few problems:

I want to, not sure if it is even possible. Can the output be saved local to the server the playbook is being run on?

in the example, I am just printing to terminal I am running the playbook. I it not much use when there is a large amount of data. I would like it to be saved in a file on the server I am running the playbook instead.

---

- name: list os version
  hosts: test
  become: true

  tasks:
  - name: hostname
    command: hostname
    register: command_output
  - name: cat /etc/redhat-release
    command: cat redhat-release chdir=/etc

  - name: Print output to console
    debug:
      msg: "{{command_output.stdout}}"

I really want the output to go to a file. I cant find anything about if this is possible.

like image 486
Randal Avatar asked Nov 30 '25 12:11

Randal


1 Answers

as you can read on the ansible documentation, you can create a local configuration file ansible.cfg inside the directory where you have your playbook and then set the proper config log file to output all the playbook output inside: Ansible output documentation

By default Ansible sends output about plays, tasks, and module arguments to your screen (STDOUT) on the control node. If you want to capture Ansible output in a log, you have three options: To save Ansible output in a single log on the control node, set the log_path configuration file setting. You may also want to set display_args_to_stdout, which helps to differentiate similar tasks by including variable values in the Ansible output.
To save Ansible output in separate logs, one on each managed node, set the no_target_syslog and syslog_facility configuration file settings. To save Ansible output to a secure database, use AWX or Red Hat Ansible Automation Platform. You can then review history based on hosts, projects, and particular inventories over time, using graphs and/or a REST API.

If you just want to output the result of the task on file, use the copy module on the localhost delegation

---

- name: list os version
  hosts: test
  become: true

  tasks:
    - name: hostname
      command: hostname
      register: command_output
    - name: cat /etc/redhat-release
      command: cat redhat-release chdir=/etc

    - name: Create your local file on master node
      ansible.builtin.file:
        path: /your/local/file
        owner: foo
        group: foo
        mode: '0644'
      delegate_to: localhost

    - name: Print output to file
      ansible.builtin.copy:
        content: "{{command_output.stdout}}"
        dest: /your/local/file
      delegate_to: localhost
like image 59
idriss Eliguene Avatar answered Dec 03 '25 13:12

idriss Eliguene