无法在 Ansible 中获取 ~/.bash_aliases

Hea*_*ren 3 ansible

~/.bash_aliaseswhile 中使用别名$HOME=/home/ubuntu作为:

alias k="kubectl -s http:xxxx"
Run Code Online (Sandbox Code Playgroud)

k这里代表不同服务器中的不同命令,我必须使用此功能,但我不能source在 Ansible shell 模块中使用它。

有没有其他方法可以解决它?

我读了几篇文章:

并尝试:

没有 - 1

  shell: ". /home/ubuntu/.bash_aliases && k get pods --all-namespaces | grep {{ serviceName }}"
Run Code Online (Sandbox Code Playgroud)

没有 - 2

  shell: ". /home/ubuntu/.bashrc && k get pods --all-namespaces | grep {{ serviceName }}"
Run Code Online (Sandbox Code Playgroud)

结果

以上两种尝试都给了我这个错误:

"/bin/sh: 1: k: not found"
Run Code Online (Sandbox Code Playgroud)

没有 - 3

  shell: "source /home/ubuntu/.bash_aliases && k get pods --all-namespaces | grep {{ serviceName }}"
  args:
    executable: /bin/bash
Run Code Online (Sandbox Code Playgroud)

没有 - 4

  shell: "source /home/ubuntu/.bashrc && k get pods --all-namespaces | grep {{ serviceName }}"
  args:
    executable: /bin/bash
Run Code Online (Sandbox Code Playgroud)

结果

以上两种尝试都给了我这个错误:

"/bin/bash: k: command not found"
Run Code Online (Sandbox Code Playgroud)

没有 - 5

   shell: "sudo -u ubuntu -i k get pods --all-namespaces | grep {{ serviceName }}"
Run Code Online (Sandbox Code Playgroud)

结果

"-bash: k: command not found"
Run Code Online (Sandbox Code Playgroud)

没有 - 6

shell: ssh -t localhost /bin/bash -ci 'k get pods --all-namespaces | grep {{ serviceName }}'
Run Code Online (Sandbox Code Playgroud)

结果

"Pseudo-terminal will not be allocated because stdin is not a terminal.", "Host key verification failed."
Run Code Online (Sandbox Code Playgroud)

没有 - 7

  shell: ssh -tt localhost /bin/bash -ci 'k get pods --all-namespaces | grep {{ serviceName }}'
  register: result
Run Code Online (Sandbox Code Playgroud)

结果

"Host key verification failed."
Run Code Online (Sandbox Code Playgroud)

适合我的步行解决方案

  vars:
    - kubeCommand: ""

  tasks:

    - name: Get Command Alias
      shell: cat ~/.bash_aliases  |  awk -F[\"] '{print $2}'
      register: result

    - set_fact: kubeCommand={{ result.stdout }}

    - name: Get service 
      shell: "{{ kubeCommand }} get pods --all-namespaces | grep {{ serviceName }}"
Run Code Online (Sandbox Code Playgroud)

但问题仍然存在。

任何帮助将不胜感激 :)

JGK*_*JGK 5

这有点难看,但是您可以alias k="kubectl -s http:xxxx"直接将您的内容放入.bashrcsource .bash_aliases来自.bashrc您的游戏,并且您必须编写:

shell: '/bin/bash -i -c "k get pods --all-namespaces | grep {{ serviceName }}"'
Run Code Online (Sandbox Code Playgroud)