从AWS SSM中使用ansible检索参数。

huangapple go评论70阅读模式
英文:

Retrieve parameters from AWS SSM with ansible

问题

- 名称: 获取环境变量
  本地操作:  set_fact "{{ lookup('aws_ssm', '/myi-homemade-apps-config/{{ service_environment }}/{{ stack_name }}', region='eu-west-1', shortnames=True, bypath=True, recursive=True, on_missing='skip' ) }}"

- 名称: 创建 .env 文件
  ansible.builtin.template:
    src: env.file.j2
    dest: /opt/docker-swarm-stack/{{ stack_name }}/.env
    mode: "0600"
  成为: true

模板内容如下:

{% for key, value in env_content.items() %}
{{ key }}={{ value }}
{% endfor %}

错误信息:

致命: [remote_host -> localhost]: 失败! => {"msg": "在模板化字符串时发生模板错误: 意外的模板结束,预期 ','.. 字符串: \"{{ lookup('aws_ssm', '/myi-homemade-apps-config/{{ service_environment }}/{{ stack_name }}', region"}

尝试过的任务:

- 名称: 获取环境变量
  本地操作:  set_fact "{{ lookup('aws_ssm', '/myi-homemade-apps-config/{{ service_environment }}/{{ stack_name }}', region='eu-west-1', shortnames=True, bypath=True, recursive=True, on_missing='skip' ) }}"

但是在lookup前没有{{,最后没有}},但是出现了以下错误:

TASK [docker-swarm-stack : 获取环境变量] **************************************************************************************************************************************************************
在执行任务时发生了异常。要查看完整的回溯信息,请使用 -vvv。错误是: NoneType: None
致命: [remote_host -> localhost]: 失败! => {"changed": false, "msg": "变量名 '\" lookup('aws_ssm', '/myi-homemade-apps-config/development/service', region' 不是有效的。变量必须以字母或下划线字符开头,并且只能包含字母、数字和下划线。"}
英文:

I need to retrieve some parameters from AWS SSM with ansible to create an env file. I tried this tasks:

- name: Get enviroment vars
  local_action:  set_fact "{{ lookup('aws_ssm', '/myi-homemade-apps-config/{{ service_environment }}/{{ stack_name }}', region='eu-west-1', shortnames=true, bypath=true, recursive=true, on_missing='skip' ) }}"


- name: Create .env file
  ansible.builtin.template:
    src: env.file.j2
    dest: /opt/docker-swarm-stack/{{ stack_name }}/.env
    mode: "0600"
  become: true

I set local_action because I need this task being executed in the local host, not in remote.

The template has:

{% for key, value in env_content.items() %}
{{key}}={{value}}
{% endfor %}

When I play the playbook, I get this error:


fatal: [remote_host -> localhost]: FAILED! => {"msg": "template error while templating string: unexpected end of template, expected ','.. String: \"{{ lookup('aws_ssm', '/myi-homemade-apps-config/{{ service_environment }}/{{ stack_name }}', region"} 

What I'm doing wrong?

Thanks

I tried this task:

  local_action:  set_fact "{{ lookup('aws_ssm', '/myi-homemade-apps-config/{{ service_environment }}/{{ stack_name }}', region='eu-west-1', shortnames=true, bypath=true, recursive=true, on_missing='skip' ) }}"

Without the {{ before lookup and without the last }} but I get this error:

TASK [docker-swarm-stack : Get enviroment vars] **************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NoneType: None
fatal: [remote_host -> localhost]: FAILED! => {"changed": false, "msg": "The variable name '\" lookup('aws_ssm', '/myi-homemade-apps-config/development/service', region' is not valid. Variables must start with a letter or underscore character, and contain only letters, numbers and underscores."}

答案1

得分: 0

以下是您要翻译的部分:

主要问题是set_fact没有被提供一个父级事实来存储来自SSM的所有参数。

这是一个更新后的代码,适用于您:

- name: 获取SSM参数
  local_action:
    module: ansible.builtin.set_fact
    env_content: "{{ lookup('aws_ssm', '/myi-homemade-apps-config/{{ service_environment }}/{{ stack_name }}', region='eu-west-1', shortnames=true, bypath=true, recursive=true, on_missing='skip' ) }}"
      shortnames=true, bypath=true,
      recursive=true)

- name: 创建.env文件
  ansible.builtin.template:
    src: env.file.j2
    dest: /opt/docker-swarm-stack/{{ stack_name }}/.env
    mode: "0600"
  become: true

这个解决方案将提取与您路径匹配的所有SSM参数,并将它们存储为env_content事实的目录条目。您的模板已经在迭代env_content以提取键/值对。

希望能对您有所帮助!

英文:

The main problem is that set_fact is not being given a parent fact to store all your parameters from SSM.

Here's an updated code that will work for you.

- name: Get SSM parameters
  local_action:
    module: ansible.builtin.set_fact
    env_content: "{{ lookup('aws_ssm', '/myi-homemade-apps-config/{{ service_environment }}/{{ stack_name }}', region='eu-west-1', shortnames=true, bypath=true, recursive=true, on_missing='skip' ) }}"
      shortnames=true, bypath=true,
      recursive=true) }}"

- name: Create .env file
  ansible.builtin.template:
    src: env.file.j2
    dest: /opt/docker-swarm-stack/{{ stack_name }}/.env
    mode: "0600"
  become: true

This solution will pull all parameters from SSM that match your path, and store them as directory entries to the env_content fact. Your template is already iterating over the env_content to pull the key/value pairs.

Hope that helps!

huangapple
  • 本文由 发表于 2023年6月27日 17:50:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定