首页 > 解决方案 > ansible aws ecr 登录而不使用 docker 命令

问题描述

我想使用 ansible 登录 aws docker ecr 注册表

    # return  docker login -u AWS -p <token> 
   -name: dget docker command
    shell: "aws ecr get-login --region {{ aws_region }}"
    register: docker_login_command
    
   -name: docker login 
    shell: "{{docker_login_command.output}}"
   

这将需要在我们的机器上安装 docker cli。但我们正在使用 docker 容器通过共享 docker 套接字运行 ansible。有没有办法不为此使用docker cli?

标签: dockeransibleamazon-ecr

解决方案


尝试这个。这对我有用。

  - name: ecr docker get-authorization-token
    shell: "aws ecr get-authorization-token  \
    --profile {{ envsettings.infra.aws_profile }} --region {{ envsettings.infra.aws_region }}"
    register: ecr_command
  
  - set_fact:
      ecr_authorization_data: "{{ (ecr_command.stdout | from_json).authorizationData[0] }}"
  
  - set_fact:
      ecr_credentials: "{{ (ecr_authorization_data.authorizationToken | b64decode).split(':') }}"
  
  - name: docker_repository - Log into ECR registry and force re-authorization
    docker_login:
      registry_url: "{{ ecr_authorization_data.proxyEndpoint.rpartition('//')[2] }}"
      username: "{{ ecr_credentials[0] }}"
      password: "{{ ecr_credentials[1] }}"
      reauthorize: yes

它需要 docker pip python 模块。在上述代码之前安装

  - name: install required packages for this role
    pip:
      state: present
      name: docker
      executable: /usr/bin/pip3

推荐阅读