0

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.

Randal
  • 363
  • 5
  • 21
  • 1
    Since this question comes up quite frequently, does [Ansible - Save registered variable to file](https://stackoverflow.com/a/26735551/6771046), [Ansible: Write variable to local file](https://stackoverflow.com/a/56049593/6771046), [Write variable to a file in Ansible](https://stackoverflow.com/a/26640778/6771046), etc. answer it? And many more answers can be found via a search, i.e. [Ansible register variable to log file](https://serverfault.com/a/998982/448950). – U880D Dec 17 '22 at 07:48

1 Answers1

3

as you can read on the ansible documentation, you can create a local configuration file ansible.cfg inside the directory vers 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
idriss Eliguene
  • 779
  • 4
  • 11
  • Ok this is what I wanted. Sometimes it is hard for newbies to find what we want. Sorry for beating down a dead horse on what it appears a common question. – Randal Dec 17 '22 at 21:22
  • There are no stupid questions, we all look or need to solve or fixe some kind of issues. I the purpose of life. – idriss Eliguene Dec 18 '22 at 01:09
  • @idrissEliguene, What is the best way to output only the result of multiple tasks to the same file on the control node ? – Aditya Siddarth Apr 12 '23 at 18:15