<p>I'm trying to install kubernetes cluster, deploy dashboard and generate a token for admin user in ansible, but when I put the command in the task I get an error.</p>

<p>I've tried to escape the quotes with no success, also tried to escape dollar sign with \$ with no success.</p>

<pre><code>- 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 }}"
</code></pre>

<p>Here is the error output, I want to execute this command in ansible task and print then with debug to capture the token and save to a txt file later.</p>

<pre><code>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}'')) =&gt; {"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": []}


#&lt;Thread:0x0000560b5b822500@/usr/share/rubygems-integration/all/gems/vagrant-2.0.2/lib/vagrant/batch_action.rb:71 run&gt; 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.
</code></pre>

jhuamanchumo
<p>Use <code>shell</code> module instead of <code>command</code>. As you can see difference in the answer of this <a href="https://stackoverflow.com/questions/24679591/ansible-command-module-says-that-is-illegal-character">post</a></p>

<p>Similar to below ansible-playbook:</p>

<pre><code>---
- 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
</code></pre>
