1

我正在使用以下工作模板:

 apiVersion: batch/v1
kind: Job
metadata:
  name: rotatedevcreds2
spec:
  template:
    metadata:
      name: rotatedevcreds2
    spec:
      containers:
      - name: shell
        image: akanksha/dsserver:v7
      env:
      - name: DEMO
        value: "Hello from the environment"
      - name: personal_AWS_SECRET_ACCESS_KEY
        valueFrom:
          secretKeyRef:
            name: rotatecreds-env
            key: personal_aws_secret_access_key
      - name: personal_AWS_SECRET_ACCESS_KEY_ID
        valueFrom:
          secretKeyRef:
            name: rotatecreds-env
            key: personal_aws_secret_access_key_id
      - name: personal_GIT_TOKEN
        valueFrom:
          secretKeyRef:
            name: rotatecreds-env
            key: personal_git_token
        command:
         - "bin/bash"
         - "-c"
         - "whoami; pwd; /root/rotateCreds.sh"
      restartPolicy: Never
      imagePullSecrets:
      - name: regcred

shell 脚本运行一些 ansible 任务,结果如下:

    TASK [Get the existing access keys for the functional backup ID] ***************
    fatal: [localhost]: FAILED! => {"changed": false, "cmd": "aws iam list-access-keys --user-name ''", "failed_when_result": true, "msg": "[Errno 2] No such file or directory", "rc": 2}

但是,如果我使用以下相同的 iamge 旋转 pod

apiVersion: batch/v1
kind: Job
metadata:
  name: rotatedevcreds3
spec:
  template:
    metadata:
      name: rotatedevcreds3
    spec:
      containers:
      - name: shell
        image: akanksha/dsserver:v7
        env:
        - name: DEMO
          value: "Hello from the environment"
        - name: personal_AWS_SECRET_ACCESS_KEY
          valueFrom:
            secretKeyRef:
              name: rotatecreds-env
              key: personal_aws_secret_access_key
        - name: personal_AWS_SECRET_ACCESS_KEY_ID
          valueFrom:
            secretKeyRef:
              name: rotatecreds-env
              key: personal_aws_secret_access_key_id
        - name: personal_GIT_TOKEN
          valueFrom:
            secretKeyRef:
              name: rotatecreds-env
              key: personal_git_token
        command:
         - "bin/bash"
         - "-c"
         - "whoami; pwd; /root/rotateCreds.sh"
      restartPolicy: Never
      imagePullSecrets:
      - name: regcred

这会创建一个 POD,我可以登录到该 pod 并运行/root/rotateCreds.sh

在运行作业时,它似乎无法识别 aws cli。我试过调试whoami,分别pwd等于root/,这很好。任何指针缺少什么?我是新来的工作。

为了在作业模板中进一步调试,我添加了10000几秒钟的睡眠,以便我可以登录到容器并查看发生了什么。我注意到登录后我也可以手动运行脚本。aws命令被正确识别。

4

2 回答 2

2

很可能您PATH的设置不正确,快速解决方法是/usr/local/bin/aws/root/rotateCreds.sh脚本中定义 aws-cli 的绝对路径

于 2019-04-03T21:16:48.423 回答
0

好的,所以我添加了一个导出命令来更新路径并解决了这个问题。问题是:我使用的是命令资源,所以它不在 bash 环境中。因此,我们可以使用带有 bash 参数的 shell 资源,如下所述: https ://docs.ansible.com/ansible/latest/modules/shell_module.html 或导出新的 PATH。

于 2019-04-04T00:33:46.907 回答