gpt4 book ai didi

ansible - 无法在 Ansible 中获取 ~/.bash_aliases

转载 作者:行者123 更新时间:2023-12-03 22:12:49 25 4
gpt4 key购买 nike

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

alias k="kubectl -s http:xxxx"
k这里代表 不同的命令 在不同的服务器中,我必须使用此功能,但我不能 source它在 Ansible shell 模块中。

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

我读了几篇文章:
  • Sourcing a file before executing commands in Ansible
  • can not source ~/.bashrc file with ansible
  • Not possible to source .bashrc with Ansible

  • 并尝试:

    没有 - 1
      shell: ". /home/ubuntu/.bash_aliases && k get pods --all-namespaces | grep {{ serviceName }}"

    没有 - 2
      shell: ". /home/ubuntu/.bashrc && k get pods --all-namespaces | grep {{ serviceName }}"

    结果

    以上两种尝试都给了我这个错误:
    "/bin/sh: 1: k: not found"

    没有 - 3
      shell: "source /home/ubuntu/.bash_aliases && k get pods --all-namespaces | grep {{ serviceName }}"
    args:
    executable: /bin/bash

    没有 - 4
      shell: "source /home/ubuntu/.bashrc && k get pods --all-namespaces | grep {{ serviceName }}"
    args:
    executable: /bin/bash

    结果

    以上两种尝试都给了我这个错误:
    "/bin/bash: k: command not found"

    没有 - 5
       shell: "sudo -u ubuntu -i k get pods --all-namespaces | grep {{ serviceName }}"

    结果
    "-bash: k: command not found"

    没有 - 6
    shell: ssh -t localhost /bin/bash -ci 'k get pods --all-namespaces | grep {{ serviceName }}'

    结果
    "Pseudo-terminal will not be allocated because stdin is not a terminal.", "Host key verification failed."

    没有 - 7
      shell: ssh -tt localhost /bin/bash -ci 'k get pods --all-namespaces | grep {{ serviceName }}'
    register: result

    结果
    "Host key verification failed."

    适合我的解决方案
      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 }}"

    但问题仍然存在。

    任何帮助将不胜感激 :)

    最佳答案

    这有点难看,但你可以把你的alias k="kubectl -s http:xxxx"直接在 .bashrcsource .bash_aliases来自 .bashrc在你的剧本中你必须写:

    shell: '/bin/bash -i -c "k get pods --all-namespaces | grep {{ serviceName }}"'

    关于ansible - 无法在 Ansible 中获取 ~/.bash_aliases,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53060336/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com