本文介绍了关键值对映射无法通过适当的值运行角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的ansible.我有一个小场景.因此,在运行它时,我会使用ansible传递分支的名称.在我的剧本中,我有:

I am new to ansible. I have a small scenario.So I pass the name of the branch using ansible when I run it.In my play book I have:

# Deploy docker container
- include_role:
    name: devops/deployment.docker
  vars:
    docker_name: "docker name is based on the branch id develop dev if UAT test if master then it should be prod"
  when: build_type in "develop master UAT"

在devops/deployment.docker中,我有一个部署docker映像的角色.

and in devops/deployment.docker I have a role which deploy a docker image.

- name: push docker container
  docker_image:
    name: "{{ docker.registery }}/docker_name"
    source: build
    build: 
      nocache: true
      path: "{{ travis.base_build_path }}"
    tag: "{{ ucd.component_version }}"
    push: yes

因此,当我运行ansible时,我将build_type作为dev,UAT或master发送给该角色.现在有一个问题:

So I send build_type as develop,UAT or master when I run the ansible this role will be called. Now there is an issue:

我需要基于分支构建映像,因此代码需要智能化,并且在发送开发代码时,它应该搜索分配给开发的密钥,并获取相关的码头工人名称并进行构建.类似于以下内容:

I need to build my image based on the branch so the code needs to be intelligent and when I send develop it should go search for the key assigned to develop and get the related docker name and build it. something similar to this:

{
 develop: {
  dockerName:dev
 },
 master: {
  dockerName: prod
  },
 UAT: {
   dockerName: test
 }
}

如果有人帮助我如何做到这一点,我真的很感激

If anyone help me how to do it in ansible I really appreciate it

推荐答案

TLDR;

- name: push docker container
  vars:
    docker_name: "{{ vars[build_type].dockerName }}"
  docker_image:
    name: "{{ docker.registery }}/{{ docker_name }}"
    source: build
    build: 
      nocache: true
      path: "{{ travis.base_build_path }}"
    tag: "{{ ucd.component_version }}"
    push: yes

变量符号

Ansible具有2种表示法来访问字典项:

Vars notation

Ansible has 2 notations to access dict items:

  • 点表示法: {{my_var.my_key.my_subkey}}
  • 括号表示法: {{my_var ['my_key'] ['my_subkey']}}

您可以根据需要混合使用这两种符号

You can mix those two notations as you wish

  • {{my_var.my_key ['my_subkey']}}
  • {{my_var ['my_key'].my_subkey}}

请注意,可以使用列表的索引以相同的方式访问列表:

Note that lists can be accessed the same way using their index:

  • {{my_list.0}}
  • {{my_list [0]}}

您的每个环境定义都是一级变量.您通常可以直接在模板表达式中调用这些var,例如 {{master.dockerName}} .但是这些var也可以通过第一级 vars 字典获得,因此以下内容完全等效于 {{vars.master.dockerName}} .

Each of your environment definition is a first level var. You can normally call those vars in a template expression directly, e.g. {{ master.dockerName }}. But those vars are also available through the first level vars dict so the following is exactly equivalent {{ vars.master.dockerName }}.

了解了以上内容,并且您正在通过 build_type 变量发送相关键的名称,则可以使用以下方法获得期望的值: {{vars [build_type] .dockerName}}

Knowing the above and that you are sending the name of the relevant key in the build_type variable, you can get the expected value with: {{ vars[build_type].dockerName }}

关于您的 include_role 条件:

when: build_type in "develop master UAT"

请注意,这将适用于您期望的值"develop" ,但也将与"AT" "mast" 开发" 甚至是"op mas" .如果出现错字,这可能会中断您的其他操作.

Note that this will work for the value "develop" as you expect, but will also match for "AT", "mast", "devel" or even "op mas". That might break the rest of your operations in case of a typo.

为确保您匹配正确的单词,将您的匹配项放在列表中,比较将从是" 的子字符串移动到是" <

To make sure you match the exact word, put your matches in a list and the comparison will move from "is it a substring of" to "is it an element of"

when: build_type in ["develop", "master", "UAT"]

这篇关于关键值对映射无法通过适当的值运行角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:45