AMBER
AMBER

Reputation: 25

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

Upvotes: 2

Views: 186

Answers (1)

Zeitounator
Zeitounator

Reputation: 44615

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 }}"

Upvotes: 2

Related Questions