Ansible - 获取多个属性的列表并执行lineinfile

时间:2016-11-23 14:38:31

标签: ansible

我在mounts下有多个值,我希望拥有这些属性的所有“挂载点”。

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

如何在内存中注册三个挂载点供以后使用,所以我得到:

/my/point1, /my/point2, /my/point3 

我想将/etc/updatedb.conf中的值与lineinfile放在一起,因此应该没有间距。

我的最终结果看起来像; cat /etc/updatedb.conf

PRUNEPATHS = "/my/point1 /my/point2 /my/point3"

目前,我使用模板来复制预定义的变量。但这不够动态。

我正在玩弄如何获得正确的变量,但没有成功:

- debug: var=mount[all].mountpoints

2 个答案:

答案 0 :(得分:0)

编辑:

您可以像mounts这样加入密钥:

mounts: {{ mounts.keys()|join(', ') }}

(旧答案对此问题无效)

this所述,您可以

---
  - hosts: all

    tasks:
      - set_fact: mounts={{ ansible_mounts | map(attribute='device')|join(',')}}

      - debug: var=ansible_mounts
      - debug: var=mounts

示例输出

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

答案 1 :(得分:0)

获取清单:

- debug: msg="{{ mounts.values() | map(attribute='mountpoint') | list }}"

或字符串:

- debug: msg="{{ mounts.values() | map(attribute='mountpoint') | list | join(' ') }}"