如何以用户身份运行list_vms(似乎以root用户身份运行)?

时间:2019-01-22 10:18:45

标签: ansible

我检查了ansible中现有的VM,但似乎是以root身份而不是以user身份检查VM。我尝试过包括成为begin_user,但这没什么区别。

剧本如下。

df = 
     p   f   c     a
 0   1   2   1    16.32
 1   1   2   2    48
 2   1   2   3    60
 3   1   2   4   112
 4   1   2   5    52
 5   1   3   6   288
 6   1   4   7   201
 7   1   4   8    52
 8   1   4   4    44
 9   1   5   7   251.2
10   1   5   9   220
11   1   5   8    83
12   1   5  10   142
13   2   1  11   100
14   2   1  12   110
15   2   2  11   120
16   2   2  13   130
17   2   3  13   140
18   2   3  14   150
19   2   4  12   160

结果如下。

df_result = 
    p   f   c      a
0   1   2   5    288.32
1   1   3   6    576.32
2   1   4   8    873.92
3   1   5  10   1570.12
4   2   1   2    210
5   2   2   3    460
6   2   3   4    750
7   2   4   4    910

为什么返回这些?我没有以root身份运行。删除支票后,将以我的用户等身份创建虚拟机。

1 个答案:

答案 0 :(得分:0)

virt命令以root身份运行,也许可以更改吗?我可以通过以下方法使用shell来解决。

  - name: Check VMs
    #virt:
    #  command: "list_vms"
    shell: virsh list --all
    register: vms
    delegate_to: '{{ groups["RU"][play_hosts.index(inventory_hostname) % groups["RU"]|length] }}'

结果:

TASK [Check VMs] ***************************************************************
changed: [test01 -> RU5]

TASK [debug] *******************************************************************
ok: [test01] => {
    "vms": {
        "changed": true,
        "cmd": "virsh list --all",
        "delta": "0:00:00.023960",
        "end": "2019-01-22 14:31:09.909886",
        "rc": 0,
        "start": "2019-01-22 14:31:09.885926",
        "stderr": "",
        "stdout": "test01",
        "stdout_lines": [
            "test01"
        ],
        "warnings": []
    }
}

条件检查将是:

when: inventory_hostname not in vms.stdout

如果有人可以建议如何以用户身份使用virt,我将不胜感激。

相关问题