Ansible-playbook 试图 运行 命令中的命令来获取变量。或者更好的方法

Ansible-playbook trying to run a command inside a command to grab the variable. OR a better way to do it

似乎 uname -r 没有按照我认为应该的方式执行。我尝试了几种变体。

- name: install nvidia driver with kernel module
  command: /root/{{ nvidia_driver }} -a -s --kernel-source-path /usr/src/kernels/'{uname -r}'

错误:

TASK [portal : install nvidia driver with kernel module] 

*****************************************************************************************
fatal: [192.168.188.115]: FAILED! => {"changed": true, "cmd": ["/root/NVIDIA-Linux-x86_64-450.66.run", "-a", "-s", "--kernel-source-path", "/usr/src/kernels/{uname -r}"], "delta": "0:00:12.120560", "end": "2020-09-22 14:59:06.201662", "msg": "non-zero return code", "rc": 1, "start": "2020-09-22 14:58:54.081102", "stderr": "./nvidia-installer: unrecognized option: \"-r}\"\n\nERROR: Invalid commandline, please run `./nvidia-installer --help` for usage information.", "stderr_lines": ["./nvidia-installer: unrecognized option: \"-r}\"", "", "ERROR: Invalid commandline, please run `./nvidia-installer --help` for usage information."], "stdout": "Verifying archive integrity... OK\nUncompressing NVIDIA Accelerated Graphics Driver for Linux-x86_64 450.66................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................", "stdout_lines": ["Verifying archive integrity... OK", "Uncompressing NVIDIA Accelerated Graphics Driver for Linux-x86_64 450.66................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................"]}

这里的问题是 uname -r 没有返回您期望的内核版本。

您应该在命令中使用 ansible_kernel Ansible fact。

- name: install nvidia driver with kernel module
  command: "/root/{{ nvidia_driver }} -a -s --kernel-source-path /usr/src/kernels/{{ ansible_kernel }}"

一个问题是 '{uname -r}'command substitution 的不正确语法。
这就是为什么错误说 -r 是无法识别的选项。
你可以试试 $(uname -r).