How to create an assertion on my playbook? [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 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"

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']"

Substraction within an Ansible space calculation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I try to subtract 2 variables in Ansible, but what I get is zero in the result.
- name: ansible calculate space
hosts: pghosts
gather_facts: false
tasks:
- name: Check /srv freespace
shell: df /srv --output\=avail | tail -1
register: srv_freespace
- name: Check postgres data dir size
shell: du -ks /srv/postgresql/data |awk '{ print $1 }'
register: postgres_data
- debug:
msg: "substraction {{ srv_freespace|int - postgres_data|int }}"
- copy: content="{{ srv_freespace|int - postgres_data|int }}" dest=/tmp/results.txt
Try changing the last line to:
- copy: content="{{ srv_freespace.stdout|int - postgres_data.stdout|int }}" dest=/tmp/results.txt
Explanation: the registered variable is not a string, its a dict. The dict includes several keys like the command name, command start and end time, exit code and other good stuff. The actual value i.e. what is printed to standard output is under the stdout key.

how to send output of fail module to file [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 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.

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 mount and 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 2 years ago.
Improve this question
ansible --version
ansible 2.3.1.0
from playbook
- name: add mount point1 to /etc/fstab
mount: path="{{base_dir}}"/"{{mpoint1}}" src="{{dev}}"/"{{vg_name}}"/"{{lv_name1}}" fstype=xfs opts=defaults state=present
/etc/fstab:
/dev"/"vg_gluster"/"brick1 /bricks"/"brick1 xfs defaults 0 0
What does it mean?
What you're asking is not clear. Please edit your question to be more precise.
I guess you're asking why " are written to /etc/fstab
The answer is that you have too much double-quotes in your task.
Try this:
- name: add mount point1 to /etc/fstab
mount: path="{{base_dir}}/{{mpoint1}}" src="{{dev}}/{{vg_name}}/{{lv_name1}}" fstype=xfs opts=defaults state=present
Or even better:
- name: add mount point1 to /etc/fstab
mount:
path: "{{ base_dir }}/{{ mpoint1 }}"
src: "{{ dev }}/{{ vg_name }}/{{ lv_name1 }}"
fstype: xfs
opts: defaults
state: present

Resources