Suppress Assert output in Ansible - ansible

I have an Ansible script that checks Linux file systems for capacity and issues a message if it is over a certain threshold.
- hosts: all
gather_facts: yes
tasks:
- name: Collect only facts about hardware
setup:
gather_subset:
- 'hardware'
- name: Test for available disk space
setup: filter=ansible_mounts
- name: Ensure that free space on the tested volume is greater than 15%
assert:
that:
- mount.size_available > mount.size_total|float * 0.05
fail_msg: Disk space on {{ item.mount }} has reached 95% threshold. Total size is {{ mount.size_total }} bytes and the available size is {{ mount.size_available }}
quiet: yes
vars:
mount: "{{ ansible_mounts | selectattr('mount','equalto',item.mount) | list | first }}"
with_items:
- "{{ ansible_mounts }}"
The problem is that even though I added quiet: yes I still get this as output on drives that are OK:
{
"changed": false,
"msg": "All assertions passed",
"_ansible_no_log": false,
"item": {
"mount": "/boot",
"device": "/dev/sda2",
"fstype": "ext4",
"options": "rw,nodev,relatime,data=ordered",
"size_total": 1023303680,
"size_available": 729440256,
"block_size": 4096,
"block_total": 249830,
"block_available": 178086,
"block_used": 71744,
"inode_total": 65536,
"inode_available": 65223,
"inode_used": 313,
"uuid": "75c887d9-e8a9-45a6-9bb8-8e7ffe4b9e68"
},
"ansible_loop_var": "item",
"_ansible_item_label": {
"mount": "/boot",
"device": "/dev/sda2",
"fstype": "ext4",
"options": "rw,nodev,relatime,data=ordered",
"size_total": 1023303680,
"size_available": 729440256,
"block_size": 4096,
"block_total": 249830,
"block_available": 178086,
"block_used": 71744,
"inode_total": 65536,
"inode_available": 65223,
"inode_used": 313,
"uuid": "75c887d9-e8a9-45a6-9bb8-8e7ffe4b9e68"
}
}
And this on the failed:
{
"evaluated_to": false,
"assertion": "mount.size_available > mount.size_total|float * 0.05",
"msg": "Disk space on /snap/core/8268 has reached 95% threshold. Size 0 Total size 93454336",
"_ansible_no_log": false,
"changed": false,
"item": {
"mount": "/snap/core/8268",
"device": "/dev/loop0",
"fstype": "squashfs",
"options": "ro,nodev,relatime",
"size_total": 93454336,
"size_available": 0,
"block_size": 131072,
"block_total": 713,
"block_available": 0,
"block_used": 713,
"inode_total": 12842,
"inode_available": 0,
"inode_used": 12842,
"uuid": "N/A"
},
"ansible_loop_var": "item",
"_ansible_item_label": {
"mount": "/snap/core/8268",
"device": "/dev/loop0",
"fstype": "squashfs",
"options": "ro,nodev,relatime",
"size_total": 93454336,
"size_available": 0,
"block_size": 131072,
"block_total": 713,
"block_available": 0,
"block_used": 713,
"inode_total": 12842,
"inode_available": 0,
"inode_used": 12842,
"uuid": "N/A"
}
}
I would like to suppress everything but the assert failed message.

The short answer is to set an explicit label in a loop_control directive, which will suppress the default behavior of outputting the entire loop variable for each loop iteration.
But you're also doing something odd in the vars section of your task.
If we fix both of those, we get:
- hosts: all
gather_facts: false
tasks:
- name: Collect only facts about hardware
setup:
gather_subset:
- hardware
- name: Test for available disk space
setup:
filter: ansible_mounts
- name: Ensure that free space on the tested volume is greater than 15%
assert:
that:
- item.size_available|float > item.size_total|float * 0.05
fail_msg: Disk space on {{ item.mount }} has reached 95% threshold. Total size is {{ item.size_total }} bytes and the available size is {{ item.size_available }}
quiet: yes
loop: "{{ ansible_mounts }}"
loop_control:
label: "{{ item.mount }}"
Which will output something like this for healthy filesystems:
ok: [host0] => (item=/)
And something like this for filesystems that fail your assert:
failed: [host1] (item=/vol/failed) => {"ansible_loop_var": "item", "assertion": "item.size_available|float > item.size_total|float * 0.05", "changed": false, "evaluated_to": false, "item": {"block_available": 0, "block_size": 131072, "block_total": 444, "block_used": 444, "device": "/dev/loop4", "fstype": "squashfs", "inode_available": 0, "inode_total": 10809, "inode_used": 10809, "mount": "/vol/failed", "options": "ro,nodev,relatime", "size_available": 0, "size_total": 58195968, "uuid": "N/A"}, "msg": "Disk space on /vol/failed has reached 95% threshold. Total size is 58195968 bytes and the available size is 0"}
Note that the contents of ansible_mounts may contain filesystems that legitimately have no free space (e.g., read-only squashfs or isofs mounts), or that don't have size_total and size_available keys. You might want to add a filter onto your task, for example:
- name: Ensure that free space on the tested volume is greater than 15%
assert:
that:
- item.size_available|float > item.size_total|float * 0.05
fail_msg: Disk space on {{ item.mount }} has reached 95% threshold. Total size is {{ item.size_total }} bytes and the available size is {{ item.size_available }}
quiet: yes
when: >
"size_total" in item and item.fstype in ['xfs', 'ext4']
loop: "{{ ansible_mounts }}"
loop_control:
label: "{{ item.mount }}"
With that filter in place, the output on my system is:
TASK [Ensure that free space on the tested volume is greater than 15%] **************************************
ok: [localhost] => (item=/)
skipping: [localhost] => (item=/var/lib/snapd/snap/mame/2287)
ok: [localhost] => (item=/boot)
skipping: [localhost] => (item=/var/lib/snapd/snap/gtk-common-themes/1514)
ok: [localhost] => (item=/home)
skipping: [localhost] => (item=/var/lib/snapd/snap/kde-frameworks-5-core18/32)
skipping: [localhost] => (item=/var/lib/snapd/snap/snapd/10707)
skipping: [localhost] => (item=/var/lib/snapd/snap/core18/1944)
skipping: [localhost] => (item=/var/lib/snapd/snap/mame/2235)
ok: [localhost] => (item=/var/lib/libvirt)
ok: [localhost] => (item=/var/lib/containers)
ok: [localhost] => (item=/var/lib/packages)
ok: [localhost] => (item=/var/lib/mock)
ok: [localhost] => (item=/vol/log)
ok: [localhost] => (item=/scratch)
ok: [localhost] => (item=/var/cache)
skipping: [localhost] => (item=/var/lib/containers/storage/overlay)

Related

Select specific cluster with prefix in VMware cluster from Ansible

I'm using the below playbook to list all the VMware cluster belonging to my datacenter, but I need to select a specific cluster containing LAB in the cluster name.
- hosts: localhost
vars_files: 1credentials.yml
tasks:
- name: Gather cluster info from given datacenter
community.vmware.vmware_cluster_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: SDx-CoE-T7
validate_certs: no
delegate_to: localhost
register: cluster_info
- debug:
msg: "{{ cluster_info }}"
- debug:
msg: "{{ cluster_info.clusters.keys() | list | to_yaml }}"
I'm trying to use selectattr(), but, I am not able to get the exact syntax. Can you please suggest the best option to filter the cluster name containing LAB.
Below is the playbook output:
PLAY [localhost] *******************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [localhost]
TASK [Gather cluster info from given datacenter] ***********************************************************************************************************************
ok: [localhost]
TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": false,
"clusters": {
"TEST_LAB": {
"datacenter": "TEST_CoE-T7",
"drs_default_vm_behavior": "fullyAutomated",
"drs_enable_vm_behavior_overrides": true,
"drs_vmotion_rate": 3,
"enable_ha": false,
"enabled_drs": false,
"enabled_vsan": false,
"ha_admission_control_enabled": true,
"ha_failover_level": 1,
"ha_host_monitoring": "enabled",
"ha_restart_priority": [
"medium"
],
"ha_vm_failure_interval": [
30
],
"ha_vm_max_failure_window": [
-1
],
"ha_vm_max_failures": [
3
],
"ha_vm_min_up_time": [
120
],
"ha_vm_monitoring": "vmMonitoringDisabled",
"ha_vm_tools_monitoring": [
"vmMonitoringDisabled"
],
"hosts": [],
"moid": "domain-c28615",
"resource_summary": {
"cpuCapacityMHz": 0,
"cpuUsedMHz": 0,
"memCapacityMB": 0,
"memUsedMB": 0,
"pMemAvailableMB": 0,
"pMemCapacityMB": 0,
"storageCapacityMB": 0,
"storageUsedMB": 0
},
"tags": [],
"vsan_auto_claim_storage": false
},
"TEST_LaaS": {
"datacenter": "TEST_CoE-T7",
"drs_default_vm_behavior": "fullyAutomated",
"drs_enable_vm_behavior_overrides": true,
"drs_vmotion_rate": 3,
"enable_ha": true,
"enabled_drs": true,
"enabled_vsan": false,
"ha_admission_control_enabled": true,
"ha_failover_level": 1,
"ha_host_monitoring": "enabled",
"ha_restart_priority": [
"medium"
],
"ha_vm_failure_interval": [
30
],
"ha_vm_max_failure_window": [
-1
],
"ha_vm_max_failures": [
3
],
"ha_vm_min_up_time": [
120
],
"ha_vm_monitoring": "vmMonitoringDisabled",
"ha_vm_tools_monitoring": [
"vmMonitoringDisabled"
],
"hosts": [
{
"folder": "/TEST_CoE-T7/host/TEST_LaaS",
"name": "172.17.65.84"
},
{
"folder": "/TEST_CoE-T7/host/TEST_LaaS",
"name": "172.17.65.85"
},
{
"folder": "/TEST_CoE-T7/host/TEST_LaaS",
"name": "172.17.168.202"
}
],
"moid": "domain-c861",
"resource_summary": {
"cpuCapacityMHz": 158040,
"cpuUsedMHz": 17494,
"memCapacityMB": 786073,
"memUsedMB": 361592,
"pMemAvailableMB": 0,
"pMemCapacityMB": 0,
"storageCapacityMB": 22707456,
"storageUsedMB": 14326118
},
"tags": [],
"vsan_auto_claim_storage": false
}
},
"failed": false
}
}
TASK [debug] ***********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "[TEST_Automation, TEST_LAB, TEST_LaaS,]\n"
}
If I use ansible version 2.9.27, I'm getting below error:
fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ cluster_info.clusters | dict2items | selectattr('key','contains','Common')| items2dict | list }}): items2dict requires a list, got <class 'generator'> instead."}
I tried with below one removing items2dict:
- debug:
msg: "{{ cluster_info.clusters | dict2items | selectattr('key','contains','TEST') | list }}"
It is giving the below output but I need only the key: TEST_Lab
ok: [localhost] => {
"msg": [
{
"key": "TEST_LAB",
"value": {
"drs_default_vm_behavior": "partiallyAutomated",
"drs_enable_vm_behavior_overrides": true,
"drs_vmotion_rate": 3,
"enable_ha": false,
"enabled_drs": true,
"enabled_vsan": false,
"ha_admission_control_enabled": true,
"ha_failover_level": 1,
"ha_host_monitoring": "enabled",
"ha_restart_priority": [
"medium"
],
"ha_vm_failure_interval": [
30
],
"ha_vm_max_failure_window": [
-1
],
"ha_vm_max_failures": [
3
],
"ha_vm_min_up_time": [
120
],
"ha_vm_monitoring": "vmMonitoringDisabled",
"ha_vm_tools_monitoring": [
"vmMonitoringDisabled"
],
"tags": [],
"vsan_auto_claim_storage": false
}
}
]
}
In order to use selectattr, you need to have a value to filter on, not a key. But transforming a dictionary into a list, containing the key and value is as simple as using the dict2items filter, in Ansible.
Then, to come back to your dictionary, just use the "antonym" filter, items2dict.
So, your debug task ends up being:
- debug:
var: >-
cluster_info.clusters
| dict2items
| selectattr('key','contains','LAB')
| items2dict
Given the task, with a reduced dictionary for readability:
- debug:
var: |-
cluster_info.clusters
| dict2items
| selectattr('key','contains','LAB')
| items2dict
vars:
cluster_info:
clusters:
TEST_LAB:
datacenter: TEST_CoE-T7
drs_default_vm_behavior: fullyAutomated
TEST_LaaS:
datacenter: TEST_CoE-T7
drs_default_vm_behavior: fullyAutomated
This will yield:
ok: [localhost] =>
? |-
cluster_info.clusters
| dict2items
| selectattr('key','contains','LAB')
| items2dict
: TEST_LAB:
datacenter: TEST_CoE-T7
drs_default_vm_behavior: fullyAutomated

Touching a file on each mount point declared in `ansible_mounts`

I want to get info about mount points so that I can touch a file on them.
I can get mount points through item.mount but I want to store those mount points inside a variable so I can use it to form a path like {{mountpoint}}/test.txt.
But I don't know how to store that info to variable from the result of my debug task. And even if I get those mount points, I don't know how to create a file inside each mount point directories.
Should I use a loop again for that?
This is the playbook I have so far:
---
- hosts: all
gather_facts: false
tasks:
- setup:
filter: ansible_mounts
- debug:
var: item.mount
loop: "{{ ansible_mounts }}"
register: result
Which gives
TASK [setup] ***********************************************************************************************************
ok: [ambertest]
TASK [debug] ***********************************************************************************************************
ok: [ambertest] => (item={u'block_used': 258025, u'uuid': u'55555-c55c-55-555-0555a', u'size_total': 55584, u'block_total': 13104379, u'mount': u'/', u'block_available': 12846354, u'size_available': 5554, u'fstype': u'xfs', u'inode_total': 25524, u'inode_available': 558, u'device': u'/dev/vda1', u'inode_used': 30796, u'block_size': 4096, u'options': u'rw,seclabel,relatime,attr2,inode64,noquota'}) => {
"item": {
"block_available": 5554,
"block_size": 4096,
"block_total": 13559,
"block_used": 2555,
"device": "/dev/vda1",
"fstype": "xfs",
"inode_available": 2558,
"inode_total": 255824,
"inode_used": 355,
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 5554,
"size_total": 5554,
"uuid": "55555"
},
"item.mount": "/"
}
ok: [ambertest] => (item={u'block_used': 672774, u'uuid': u'55dc9f5589e', u'size_total': 55, u'block_total': 155558, u'mount': u'/amber', u'block_available': 554, u'size_available': 49954627584, u'fstype': u'ext4', u'inode_total': 355, u'inode_available': 559, u'device': u'/dev/vdb', u'inode_used': 11, u'block_size': 4096, u'options': u'rw,seclabel,relatime,data=ordered'}) => {
"item": {
"block_available": 1554,
"block_size": 4556,
"block_total": 558,
"block_used": 6554,
"device": "/dev/vdb",
"fstype": "ext4",
"inode_available": 559,
"inode_total": 550,
"inode_used": 11,
"mount": "/amber",
"options": "rw,seclabel,relatime,data=ordered",
"size_available": 45584,
"size_total": 55,
"uuid": "5555"
},
"item.mount": "/amber"
}
PLAY RECAP *************************************************************************************************************
ambertest : ok=2 changed=0 unreachable=0 failed=0
The following is really far from optimized and bullet proofed but will at least give you an idea of how to start. Basically, you don't need to store your mount points inside a variable since the ansible_mounts variable already has them (as brilliantly demonstrated in your question)
---
- hosts: all
tasks:
- name: touch a test.txt file on each existing mountpoint
file:
path: "{{ item.mount }}/test.txt"
state: touch
loop: "{{ ansible_mounts }}"

Ansible set_fact from dictionary based on item type

I am reading a dictionary from a Kubernetes config map (cm_config below) and am using it to replace variables set in defaults/main.yml like this:
- name: 'Overwrite defaults'
set_fact: "{{ item.key }}={{ item.value }}"
with_dict: "{{ cm_config }}"
This works fine as long as the items are simple variables. But as soon as an item is another dictionary, I'd like to combine the values.
How can I integrate this into the above task? I thought about running the loop twice, with some kind of type check. Not sure how this would work. Additionally, I believe there might be a better way?
One solution below to achieve your requirement in a single task whit just a bit of jinja2 templating and a vars lookup to get existing dict content. The key is to calculate the value based on the variable type.
Note that this does not take into account the situations when the var is a list which will be replaced as all other type of values. This will not either deal with type mismatch between existing vars and config map. e.g. if your existing var is string and the corresponding one in config map a dict it will break.
The following playbook:
---
- hosts: localhost
gather_facts: false
vars:
cm_config:
label1: toto
label2:
a_value: 1
other_value: 2
label3:
a_value: 3
other_value: 4
label4: tata
label1: I am set in play
label3:
some_value: I'm a poor lonesome cowboy
tasks:
- name: show initial state
debug:
var: "{{ item.key }}"
with_dict: "{{ cm_config }}"
- name: Process values from config map
vars:
my_value: >-
{% if item.value is mapping %}
{{ lookup('vars', item.key, default={}) | combine(item.value) }}
{% else %}
{{ item.value }}
{% endif %}
set_fact:
"{{ item.key }}": "{{ my_value }}"
with_dict: "{{ cm_config }}"
- name: Show the result after processing config map
debug:
var: "{{ item.key }}"
with_dict: "{{ cm_config }}"
gives the following result:
PLAY [localhost] ****************************************************************************************************************************************************************************************************************************
TASK [show initial state] *******************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=label1) => {
"ansible_loop_var": "item",
"item": {
"key": "label1",
"value": "toto"
},
"label1": "I am set in play"
}
ok: [localhost] => (item=label2) => {
"ansible_loop_var": "item",
"item": {
"key": "label2",
"value": {
"a_value": 1,
"other_value": 2
}
},
"label2": "VARIABLE IS NOT DEFINED!"
}
ok: [localhost] => (item=label3) => {
"ansible_loop_var": "item",
"item": {
"key": "label3",
"value": {
"a_value": 3,
"other_value": 4
}
},
"label3": {
"some_value": "I'm a poor lonesome cowboy"
}
}
ok: [localhost] => (item=label4) => {
"ansible_loop_var": "item",
"item": {
"key": "label4",
"value": "tata"
},
"label4": "VARIABLE IS NOT DEFINED!"
}
TASK [Process values from config map] *******************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'label1', 'value': 'toto'})
ok: [localhost] => (item={'key': 'label2', 'value': {'a_value': 1, 'other_value': 2}})
ok: [localhost] => (item={'key': 'label3', 'value': {'a_value': 3, 'other_value': 4}})
ok: [localhost] => (item={'key': 'label4', 'value': 'tata'})
TASK [Show the result after processing config map] ******************************************************************************************************************************************************************************************
ok: [localhost] => (item=label1) => {
"ansible_loop_var": "item",
"item": {
"key": "label1",
"value": "toto"
},
"label1": " toto "
}
ok: [localhost] => (item=label2) => {
"ansible_loop_var": "item",
"item": {
"key": "label2",
"value": {
"a_value": 1,
"other_value": 2
}
},
"label2": " {'a_value': 1, 'other_value': 2} "
}
ok: [localhost] => (item=label3) => {
"ansible_loop_var": "item",
"item": {
"key": "label3",
"value": {
"a_value": 3,
"other_value": 4
}
},
"label3": " {'some_value': \"I'm a poor lonesome cowboy\", 'a_value': 3, 'other_value': 4} "
}
ok: [localhost] => (item=label4) => {
"ansible_loop_var": "item",
"item": {
"key": "label4",
"value": "tata"
},
"label4": " tata "
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Ansible: how to print var name while looping?

I have this inside setup/tasks/apps.yml:
- name: Mac App Store | Install apps.
shell: mas list | grep {{ item.id }} || mas install {{ item.id }}
with_items: "{{ mac_store_apps }}"
register: result
changed_when: result.stdout.find('Installed') != -1
I know I can use:
loop_control:
label: '{{ item.name }}'
But I want to print something like:
Attempting to Install {{ item.name }}
before each execution. How can that be done?
There is no way to generate output before each iteration of your loop; Ansible only produces output when a task (or an iteration of a looping task) is complete. If you are concerned that package installation can take a while and you want to provide some feedback, you could just print out a notice in advance, as in:
- debug:
msg: "Note: package installation may take several minutes to complete"
You can sort of get what you want by placing your install task in a separate file, and then calling include_tasks in a loop. For example, if we have the following playbook:
---
- hosts: localhost
gather_facts: false
vars:
mac_store_apps:
- name: foo
- name: bar
- name: baz
- name: qux
tasks:
- name: "Mac App Store | Install apps."
include_tasks: ./install.yml
with_items: "{{ mac_store_apps }}"
- debug:
var: all_results
And the following tasks in install.yml:
---
- name: "Mac App Store | Install {{ item.name }}"
shell: "true"
register: result
- name: Store result
set_fact:
all_results: "{{ all_results|default([]) + [{'item': item, 'result': result}] }}"
We will see as output:
PLAY [localhost] ******************************************************************************
TASK [Mac App Store | Install apps.] **********************************************************
included: /home/lars/tmp/ansible/install.yml for localhost => (item={u'name': u'foo'})
included: /home/lars/tmp/ansible/install.yml for localhost => (item={u'name': u'bar'})
included: /home/lars/tmp/ansible/install.yml for localhost => (item={u'name': u'baz'})
included: /home/lars/tmp/ansible/install.yml for localhost => (item={u'name': u'qux'})
TASK [Mac App Store | Install foo] ************************************************************
changed: [localhost]
TASK [Store result] ***************************************************************************
ok: [localhost]
TASK [Mac App Store | Install bar] ************************************************************
changed: [localhost]
TASK [Store result] ***************************************************************************
ok: [localhost]
TASK [Mac App Store | Install baz] ************************************************************
changed: [localhost]
TASK [Store result] ***************************************************************************
ok: [localhost]
TASK [Mac App Store | Install qux] ************************************************************
changed: [localhost]
TASK [Store result] ***************************************************************************
ok: [localhost]
TASK [debug] **********************************************************************************
ok: [localhost] => {
"all_results": [
{
"item": {
"name": "foo"
},
"result": {
"changed": true,
"cmd": "true",
"delta": "0:00:00.002398",
"end": "2019-04-23 11:19:25.146497",
"failed": false,
"rc": 0,
"start": "2019-04-23 11:19:25.144099",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
},
{
"item": {
"name": "bar"
},
"result": {
"changed": true,
"cmd": "true",
"delta": "0:00:00.002245",
"end": "2019-04-23 11:19:25.285859",
"failed": false,
"rc": 0,
"start": "2019-04-23 11:19:25.283614",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
},
{
"item": {
"name": "baz"
},
"result": {
"changed": true,
"cmd": "true",
"delta": "0:00:00.002406",
"end": "2019-04-23 11:19:25.426909",
"failed": false,
"rc": 0,
"start": "2019-04-23 11:19:25.424503",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
},
{
"item": {
"name": "qux"
},
"result": {
"changed": true,
"cmd": "true",
"delta": "0:00:00.002232",
"end": "2019-04-23 11:19:25.574214",
"failed": false,
"rc": 0,
"start": "2019-04-23 11:19:25.571982",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
}
]
}
PLAY RECAP ************************************************************************************
localhost : ok=13 changed=4 unreachable=0 failed=0

Ansible - get list of multiple attributes and do lineinfile

I've got multiple values under mounts, I want to have all 'mountpoints' of these attributes.
"mounts": {
"/dev/sdb": {
"fstype": "xfs",
"mountpoint": "/my/point1",
"opts": "defaults,_netdev",
"partition": "/dev/sdb1",
"state": "mounted"
},
"/dev/sdc": {
"fstype": "xfs",
"mountpoint": "/my/point2",
"opts": "defaults,_netdev",
"partition": "/dev/sdc1",
"state": "mounted"
},
"/dev/sdd": {
"fstype": "xfs",
"mountpoint": "/my/point3",
"opts": "defaults,_netdev",
"partition": "/dev/sdd1",
"state": "mounted"
How do I register the three mountpoints in memory for later use, so I get:
/my/point1, /my/point2, /my/point3
I want to place the values in /etc/updatedb.conf with lineinfile, so there should be no spacing.
My end result would look like; cat /etc/updatedb.conf
PRUNEPATHS = "/my/point1 /my/point2 /my/point3"
Currently, I use a template to copy pre-defined variables. But this is not dynamic enough.
I'm playing around how to get the right variables, but no success:
- debug: var=mount[all].mountpoints
To get a list:
- debug: msg="{{ mounts.values() | map(attribute='mountpoint') | list }}"
Or a string:
- debug: msg="{{ mounts.values() | map(attribute='mountpoint') | list | join(' ') }}"
EDIT:
You can get the join of the keys from mounts like this:
mounts: {{ mounts.keys()|join(', ') }}
(old answer that is not valid for this question)
as this describes, you can do
---
- hosts: all
tasks:
- set_fact: mounts={{ ansible_mounts | map(attribute='device')|join(',')}}
- debug: var=ansible_mounts
- debug: var=mounts
sample output
ap test.yml -i hosts -l server
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [server]
TASK [set_fact] ****************************************************************
ok: [server]
TASK [debug] *******************************************************************
ok: [server] => {
"ansible_mounts": [
{
"device": "/dev/sda1",
"fstype": "ext4",
"mount": "/",
"options": "rw,errors=remount-ro",
"size_available": 2890289152,
"size_total": 9376751616,
"uuid": "N/A"
},
{
"device": "/dev/sdb1",
"fstype": "ext4",
"mount": "/mnt/data1",
"options": "rw",
"size_available": 50684461056,
"size_total": 200674758656,
"uuid": "N/A"
}
]
}
TASK [debug] *******************************************************************
ok: [server] => {
"mounts": "/dev/sda1,/dev/sdb1"
}
PLAY RECAP *********************************************************************
server : ok=4 changed=0 unreachable=0 failed=0

Resources