在我的头在 table 上撞了几天之后。这就是我所在的地方。
目标:使用“UUID=”作为我的挂载点在/etc/fstab 文件中创建一个条目。
In this case there will be x number of disks on y number of hosts so just putting the information in a vars file unfortunately not possible. Also there will not be any partitions or lvms, for this application we need JBOD with filesystems.
我的方法:经过反复试验后,我的磁盘都已格式化并且具有 UUID。所以我试图遍历我的设备,然后将我的 UUID 返回到我可以使用挂载模块附加到 fstab。
停留在: 在获得我的设备列表后,我发现创 build 备 UUID 列表的最佳方法是使用 blkid 命令并注册结果。 (见下文)我的想法是我是否可以访问“results2”中的嵌套 UUID。然后我可以将设备名称/UUID 与新事实配对,以便以后更容易访问。
#######
#get new facts
#######
- command: "/sbin/blkid -o device"
register: "result1"
- shell: '/sbin/blkid "{{ item }}" -o value'
register: "result2"
with_items: "{{ result1.stdout_lines }}"
#- debug: var="{{ result2.results }}"
- debug: "{{item}}"
with_together:
- "{{result1.stdout_lines}}"
- "{{result2.results}}"
输出
PLAY [disk-test-node] **********************************************************
TASK [setup] *******************************************************************
ok: [31.212.212.123]
TASK [common : set_fact] *******************************************************
task path: /ansible/roles/common/tasks/main.yml:5
ok: [31.212.212.123] => (item={u'uuid': u'', u'size_total': 8319852544, u'mount': u'/', u'size_available': 7187308544, u'fstype': u'ext4', u'device': u'/dev/xvda1', u'options': u'rw'}) => {"ansible_facts": {"mounted_drives": "/dev/xvda1"}, "changed": false, "item": {"device": "/dev/xvda1", "fstype": "ext4", "mount": "/", "options": "rw", "size_available": 7187308544, "size_total": 8319852544, "uuid": ""}}
TASK [common : command] ********************************************************
task path: /ansible/roles/common/tasks/main.yml:39
changed: [31.212.212.123] => {"changed": true, "cmd": ["/sbin/blkid", "-o", "device"], "delta": "0:00:00.003729", "end": "2016-11-15 22:11:13.348422", "rc": 0, "start": "2016-11-15 22:11:13.344693", "stderr": "", "stdout": "/dev/xvda1\n/dev/xvdd\n/dev/xvde\n/dev/xvdb\n/dev/xvdc\n/dev/xvdh\n/dev/xvdi\n/dev/xvdf\n/dev/xvdg", "stdout_lines": ["/dev/xvda1", "/dev/xvdd", "/dev/xvde", "/dev/xvdb", "/dev/xvdc", "/dev/xvdh", "/dev/xvdi", "/dev/xvdf", "/dev/xvdg"], "warnings": []}
TASK [common : command] ********************************************************
task path: /ansible/roles/common/tasks/main.yml:42
changed: [31.212.212.123] => (item=/dev/xvda1) => {"changed": true, "cmd": "/sbin/blkid \"/dev/xvda1\" -o value", "delta": "0:00:00.002619", "end": "2016-11-15 22:11:15.693323", "item": "/dev/xvda1", "rc": 0, "start": "2016-11-15 22:11:15.690704", "stderr": "", "stdout": "cdbab22a-45d6-4cce-95a3-681f42187a46\next4", "stdout_lines": ["cdbab22a-45d6-4cce-95a3-681f42187a46", "ext4"], "warnings": []}
changed: [31.212.212.123] => (item=/dev/xvdd) => {"changed": true, "cmd": "/sbin/blkid \"/dev/xvdd\" -o value", "delta": "0:00:00.002580", "end": "2016-11-15 22:11:17.984917", "item": "/dev/xvdd", "rc": 0, "start": "2016-11-15 22:11:17.982337", "stderr": "", "stdout": "61b23af1-44dd-46c4-8e4b-2af2bd928f98\next4", "stdout_lines": ["61b23af1-44dd-46c4-8e4b-2af2bd928f98", "ext4"], "warnings": []}
就在那里! 您可以在“stdout”和“stdout_lines[0]”中看到 UUID
配对后的最终调试循环
TASK [common : debug] **********************************************************
task path: /ansible/roles/common/tasks/main.yml:48
ok: [31.212.212.123] => (item=[u'/dev/xvda1', {'_ansible_parsed': True, '_ansible_item_result': True, u'stdout': u'cdbab22a-45d6-4cce-95a3-681f42187a46\next4', '_ansible_no_log': False, u'warnings': [], u'changed': True, u'rc': 0, u'end': u'2016-11-15 22:11:15.693323', u'start': u'2016-11-15 22:11:15.690704', u'cmd': u'/sbin/blkid "/dev/xvda1" -o value', 'item': u'/dev/xvda1', u'delta': u'0:00:00.002619', 'invocation': {'module_name': u'command', u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'/sbin/blkid "/dev/xvda1" -o value', u'removes': None, u'creates': None, u'chdir': None}}, 'stdout_lines': [u'cdbab22a-45d6-4cce-95a3-681f42187a46', u'ext4'], u'stderr': u''}]) => {
"<type 'list'>": "VARIABLE IS NOT DEFINED!",
"item": [
"/dev/xvda1",
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/sbin/blkid \"/dev/xvda1\" -o value",
"delta": "0:00:00.002619",
"end": "2016-11-15 22:11:15.693323",
"invocation": {
"module_args": {
"_raw_params": "/sbin/blkid \"/dev/xvda1\" -o value",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": "/dev/xvda1",
"rc": 0,
"start": "2016-11-15 22:11:15.690704",
"stderr": "",
"stdout": "cdbab22a-45d6-4cce-95a3-681f42187a46\next4",
"stdout_lines": [
"cdbab22a-45d6-4cce-95a3-681f42187a46",
"ext4"
],
"warnings": []
}
]
}
ok: [31.212.212.123] => (item=[u'/dev/xvdd', {'_ansible_parsed': True, '_ansible_item_result': True, u'stdout': u'61b23af1-44dd-46c4-8e4b-2af2bd928f98\next4', '_ansible_no_log': False, u'warnings': [], u'changed': True, u'rc': 0, u'end': u'2016-11-15 22:11:17.984917', u'start': u'2016-11-15 22:11:17.982337', u'cmd': u'/sbin/blkid "/dev/xvdd" -o value', 'item': u'/dev/xvdd', u'delta': u'0:00:00.002580', 'invocation': {'module_name': u'command', u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'_raw_params': u'/sbin/blkid "/dev/xvdd" -o value', u'removes': None, u'creates': None, u'chdir': None}}, 'stdout_lines': [u'61b23af1-44dd-46c4-8e4b-2af2bd928f98', u'ext4'], u'stderr': u''}]) => {
"<type 'list'>": "VARIABLE IS NOT DEFINED!",
"item": [
"/dev/xvdd",
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/sbin/blkid \"/dev/xvdd\" -o value",
"delta": "0:00:00.002580",
"end": "2016-11-15 22:11:17.984917",
"invocation": {
"module_args": {
"_raw_params": "/sbin/blkid \"/dev/xvdd\" -o value",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": "/dev/xvdd",
"rc": 0,
"start": "2016-11-15 22:11:17.982337",
"stderr": "",
"stdout": "61b23af1-44dd-46c4-8e4b-2af2bd928f98\next4",
"stdout_lines": [
"61b23af1-44dd-46c4-8e4b-2af2bd928f98",
"ext4"
],
"warnings": []
}
]
}
最佳答案
我可以建议另一种解决方案吗?
- shell: "blkid | sed 's/:.*UUID=\"\\([0-9a-f-]\\+\\).*/ \\1/'"
register: devs
- debug: msg="dev={{ item.split()[0] }}, uuid={{ item.split()[1] }}"
with_items: "{{ devs.stdout_lines }}"
第一个任务将返回以空格分隔的device_name UUID
对。
第二个任务使用 python split()
函数来获取 device_name
和 UUID
作为单独的字符串。
关于json - 访问 Ansible 命令的嵌套结果会生成一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40620780/