Kubernetes仪表板-尝试生成管理员令牌时出现ansible任务错误

Posted

技术标签:

【中文标题】Kubernetes仪表板-尝试生成管理员令牌时出现ansible任务错误【英文标题】:Kubernetes dashboard - ansible task error when trying to generate admin token 【发布时间】:2019-10-16 13:36:17 【问题描述】:

我正在尝试安装 kubernetes 集群,部署仪表板并在 ansible 中为管理员用户生成令牌,但是当我将命令放入任务时出现错误。

我尝试转义引号但没有成功,也尝试用 \$ 转义美元符号但没有成功。

- name: 19/23 Get dashboard token for admin user
  become: false
  command: " item "
  register: output
  with_items:
    - kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk 'print $1')

- debug: msg=" output.stdout_lines "

这是错误输出,我想在 ansible 任务中执行此命令,然后使用调试打印以捕获令牌并稍后保存到 txt 文件。

TASK [19/23 Get Dashboard token] 
***********************************************
failed: [master] (item=kubectl -n kube-system describe secret \$(kubectl -n kube-system get secret | grep admin-user | awk ''print $1'')) => "ansible_loop_var": "item", "changed": true, "cmd": ["kubectl", "-n", "kube-system", "describe", "secret", "$(kubectl", "-n", "kube-system", "get", "secret", "|", "grep", "admin-user", "|", "awk", "print", "$1)"], "delta": "0:00:02.220054", "end": "2019-05-30 21:18:44.331480", "item": "kubectl -n kube-system describe secret \\$(kubectl -n kube-system get secret | grep admin-user | awk ''print $1'')", "msg": "non-zero return code", "rc": 1, "start": "2019-05-30 21:18:42.111426", "stderr": "Error from server (NotFound): secrets \"$(kubectl\" not found\nError from server (NotFound): secrets \"get\" not found\nError from server (NotFound): secrets \"secret\" not found\nError from server (NotFound): secrets \"|\" not found\nError from server (NotFound): secrets \"grep\" not found\nError from server (NotFound): secrets \"admin-user\" not found\nError from server (NotFound): secrets \"|\" not found\nError from server (NotFound): secrets \"awk\" not found\nError from server (NotFound): secrets \"print\" not found\nError from server (NotFound): secrets \"$1)\" not found", "stderr_lines": ["Error from server (NotFound): secrets \"$(kubectl\" not found", "Error from server (NotFound): secrets \"get\" not found", "Error from server (NotFound): secrets \"secret\" not found", "Error from server (NotFound): secrets \"|\" not found", "Error from server (NotFound): secrets \"grep\" not found", "Error from server (NotFound): secrets \"admin-user\" not found", "Error from server (NotFound): secrets \"|\" not found", "Error from server (NotFound): secrets \"awk\" not found", "Error from server (NotFound): secrets \"print\" not found", "Error from server (NotFound): secrets \"$1)\" not found"], "stdout": "", "stdout_lines": []


#<Thread:0x0000560b5b822500@/usr/share/rubygems-integration/all/gems/vagrant-2.0.2/lib/vagrant/batch_action.rb:71 run> terminated with exception (report_on_exception is true):
`execute_ansible_playbook_from_host'
/usr/share/rubygems-integration/all/gems/vagrant-2.0.2/plugins/provisioners/ansible/provisioner/host.rb:104:in `execute_command_from_host': Ansible failed to complete successfully. 
Any error output should be (VagrantPlugins::Ansible::Errors::AnsibleCommandFailed) visible above. Please fix these errors and try again.
Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.

【问题讨论】:

是这个调试输出,即带有 -vvvv 选项 【参考方案1】:

使用shell 模块而不是command。如您所见,post的答案有所不同

类似于下面的 ansible-playbook:

---
- hosts: local
  tasks:
  - name: get secret name
    shell: 'kubectl -n kube-system get secret -o=name| grep admin-user'
    register: secret

  - name: get token for kubernetes-dashboard user
    shell: "kubectl -n kube-system get  secret.stdout  -o=jsonpath='..token'"
    register: token

  - debug: 
      var: token.stdout

【讨论】:

以上是关于Kubernetes仪表板-尝试生成管理员令牌时出现ansible任务错误的主要内容,如果未能解决你的问题,请参考以下文章

如何从外部网络访问Kubernetes仪表板

使用 OAuth2 服务帐户生成访问令牌时出现问题

使用 Knock gem 生成 JWT 令牌时出现 500 内部服务器错误

Uber Eats API 范围

为可编辑的Power BI Embedded仪表板生成嵌入令牌时出错

Microsoft 文档在以编程方式生成 SAS 令牌时出现问题。错误:“签名字段格式不正确”