how to send output of fail module to file [closed] - ansible

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have below part of my play for ansible fail module I want to redirect output of this module to file
- name:
fail:
msg: "SVRELOAD NOT DONE ON THIS PTS {{ ansible_hostname }}"
when: last_restart_date.stdout != ansible_date_time.date
register: failed_task

Here is how I would do it ( a little different approach). Just to demo the solution, I tried to download a package that does not exist. And I captured that message, and wrote in a file. you will have to adjust your play book with block and rescues.
Playbook ( with block, rescue )
---
- name: writing error to a file
hosts: localhost
become: true
tasks:
- name: block to create block-rescue-always
block:
- name: this module intentionally fails
yum: name=does_not_exist state=latest
register: failed_msg
rescue:
- name: write the error to a file
debug: msg="failed with yum block"
- name: create an error file
file:
path: /home/user1/ansible/error-msg.txt
owner: user1
group: user1
mode: '0755'
state: touch
- name: write to a file with lineinfile
lineinfile:
path: /home/user1/ansible/error-msg.txt
line: "{{ failed_msg }}"
and here is the response in the file error-msg.txt
{'msg': "No package matching 'does_not_exist' is available", 'failed': True, 'changed': False, 'ansible_facts': {'pkg_mgr': 'apt'}}
you don't have to create a new file if your error file already exists with write permissions.

Related

Unexpected behavior in ansible when using variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I have a playbook to add users to a system, and to sudoers. The output indicates ansible is not separating values out.
How would I do this properly?
Here is my main.yml.
tasks:
- name: Add User to sudoers
community.general.sudoers:
name: '{{ admin }}'
state: present
user: '{{ admin }}'
commands: ALL
nopassword: true
- name: Add User
ansible.builtin.user:
name: '{{ admin }}'
shell: /bin/bash
password: '*'
- name: Read variables
include_vars: '{{ item }}'
with_first_found:
- files:
- "{{admin + '_key.yml'"
paths: "./vars/"
Here is my Vars file (minus the ssh keys which are separate yml files)
---
admin:
- user1
- user2
- user3
Ansible takes this and creates
['user1', 'user2', 'user3']'\n", "name": "['user1', 'user2', 'user3']"

writing ansible facts to a file from a playbook

I know I can use the command below to save all ansible facts to a file:
ansible all -m setup --tree facts.d/
But I want to do this within a playbook. Currently I have:
- name: Collect facts
setup:
fact_path: facts.d
But nothing is collected when I run the task. Am I missing something?
- name: save all facts to host specific file
copy:
content: "{{ ansible_delegated_vars[inventory_hostname].vars | to_nice_json }}"
dest: "{{ playbook_dir }}/{{ ansible_fqdn }}"
delegate_to: localhost
This will create a file per host at playbook dir.
Reference : https://groups.google.com/g/ansible-project/c/HI65wOUIrx4?pli=1
If you just want to write a variable into a file you could use something like
- name: Copy Ansible facts to a file
copy:
content: "{{ ansible_facts }}"
dest: /tmp/ansible_facts_details.json
Thanks to
Cache Plugins, since that can be depending on the requirements already be a solution
Ansible: Facts in a single file
Ansible: Write whole facts to file

How to create an assertion on my playbook? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would like to use the assert function in ansible for a test.
The goal is to check if they are some files and directories that contains the permission 1500, if it is the case than I would like to show the message fail
- hosts: localhost
tasks:
- name: Check assertion
shell: find . -perm 1500
register: list_files_directories
- name: Check if file testfile.txt exists
assert:
that:
- mode =! '1500' in list_files_directories
After multiple test,cannot find the right syntax to execute the assertion...
Use find module. For example, given the files
shell> find . -perm 1500
./file2
The tasks below
- find:
paths: .
register: result
- debug:
msg: "{{ result.files|json_query(query) }}"
vars:
query: "[?mode=='1500'].path"
give the list of the files with permissions '1500'
"msg": [
"file2"
]
To check if there are any files and directories with the permission 1500 test the length of the list
- assert:
that: result.files|json_query(query)|length > 0
vars:
query: "[?mode=='1500'].path"

Ansible - print gathered facts for debugging purposes [duplicate]

This question already has answers here:
Where can I get a list of Ansible pre-defined variables?
(10 answers)
Closed 4 years ago.
Is there exists some way to print on console gathered facts ?
I mean gatering facts using setup module. I would like to print gathered facts. Is it possible ? If it is possible can someone show example?
Use setup module as ad-hoc command:
ansible myhost -m setup
You can simply dump the hostvars:
dump.yml
---
- name: Dump
hosts: "{{ target|default('localhost') }}"
tasks:
- name: Facts
setup:
- name: Dump
delegate_to: localhost
run_once: true
copy:
content: "{{ hostvars[inventory_hostname] | to_nice_json }}"
dest: /tmp/setup-dump.json
Call this playbook with ansible-playbook dump.yml -e target=hostname or simply without hostname.

Ansible: Write multiple lines to a file using local_action or another method

I am using the command:
local_action: copy content="The installation failed" dest=~/ansible/ansible_log.txt
However, when I do it again:
local_action: copy content="Contact me for assistance" dest=~/ansible/ansible_log.txt
It overwrites the old text with the new text. What I want to do is append to the file instead of replacing the previous text.
I tried adding in a /n to the end of the original string to no avail.
What about the lineinfile module:
local_action:
module: lineinfile
dest: "~/ansible/ansible_log.txt"
line: "The installation failed"
create: yes
local_action:
module: lineinfile
dest: "~/ansible/ansible_log.txt"
line: "Contact me for assistance"
Tried multiple posts and this is that finally helped what I wanted to do. Collect various facts and finally output it to a local file for review. Posting it out here, hoping to help someone starting on ansbile :)
---
- name: "Collect host OS Version information for the host"
vars:
- output_path: "/tmp"
- filename: "osinfo_{{date}}.csv"
vars_prompt:
- name: input_hostname
prompt: What is the set of hosts you want connect ?
private: no
hosts: "{{ input_hostname }}"
tasks:
- name: CSV Generate output filename
set_fact: date="{{lookup('pipe','date +%Y%m%d_%H%M%S')}}"
run_once: true
- name: CSV - Create file and set the header
local_action: copy content="Hostname,SID,OSVersion,KernelVersion\n" dest="{{output_path}}/{{filename }}"
run_once: true
- name: OS Version info for {{ input_hostname }} hosts
set_fact:
csv_tmp: >
{{ inventory_hostname }},{{SID}},{{ ansible_distribution_version }},{{ ansible_kernel }}
- name: CSV - Write information into .csv file
local_action:
module: lineinfile
dest: "{{output_path}}/{{filename }}"
line: "{{csv_tmp}}"
- name: CSV - Blank lines removal
local_action:
module: lineinfile
dest: "{{output_path}}/{{filename }}"
state: absent
regex: '^\s*$'

Resources