ansible 中的键值对映射以传递运行具有适当值的角色 [英] key value pair mapping in ansible to pass run the role with the appropriate value

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

问题描述

我是 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 作为 develop、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:

我需要基于分支构建我的图像,因此代码需要智能,当我发送开发时,它应该去搜索分配给开发的密钥并获取相关的 docker 名称并构建它.类似的东西:

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
 }
}

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

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

Var 符号

Ansible 有 2 个符号来访问 dict 项目:

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] }}

您的每个环境定义都是第一级变量.您通常可以直接在模板表达式中调用这些变量,例如{{ master.dockerName }}.但是这些变量也可以通过第一级 vars dict 获得,因此以下完全等效 {{ 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">、"devel" 甚至 "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"]

这篇关于ansible 中的键值对映射以传递运行具有适当值的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆