我正在尝试创建一个Ansible剧本,这将允许我在多个区域中创建一个AWS Lambda,并且对于每个Lambda,还需要向Lambda注册一个snS作为触发器。 (如果我对流程有误解,请随时指出我的错误,我不是100%在做的事情是正确的)
我在Ansible中有以下代码:
lambda:
name: "{{ item[0] }}"
state: present
s3_bucket: "my-devops-lambdas-{{ item[1] }}"
s3_key: "{{ item[0] }}.zip"
runtime: python3.6
role: arn:aws:iam::****:role/devops-lambdas
handler: lambda.lambda_handler
region: "{{ item[1] }}"
register: lambda_config
with_items:
- "{{ projects_to_build.stdout | from_json }}"
- name: Register snS
sns_topic:
name: "{{ item.0[2] }}"
region: "{{ item.0[0] }}"
subscriptions:
- protocol: LAMbda
- endpoint: "{{ item.1.configuration.function_arn }}"
with_together:
- "{{ projects_to_build.stdout | from_json }}"
- "{{ lambda_config.results }}"
我的projects_to_build
如下:
"failed": false,"projects_to_build.stdout": [
[
"cloudwatch-to-slack","eu-west-2","arn:aws:sns:eu-west-2:****:slack_devops"
],[
"cloudwatch-to-slack","eu-west-1","arn:aws:sns:eu-west-1:****:slack_devops"
]
]
}
我认为我应该使用with_subelements
来访问lambda_config
结果中的项目,但是我看不到如何与with_together
同时使用
我会在此处发布lambda_config
,但是它很大,配置的格式类似于this,除了因为我创建了多个lambda,结果是带有{{1 }}是创建的每个lambda的结果列表。
我正在使用Ansible 2.6,所以我可以使用新的loops方法而不是with_together,但是到目前为止,我仍然找不到任何可行的方法。
尝试按原样运行时出现此错误:
results
我尝试了许多其他解决方案,我认为可行的方法是使用:
TASK [Register snS] ************************************************************
fatal: [localhost]: FAILED! => {"failed": true,"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'configuration'\n\nThe error appears to have been in '/var/lib/jenkins/workspace/Global/devops-lambdas/aws/shared/deploy-devops-lambdas.yml': line 46,column 7,but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Register snS\n ^ here\n"}
to retry,use: --limit @/var/lib/jenkins/workspace/Global/devops-lambdas/aws/shared/deploy-devops-lambdas.retry
这将导致相同的错误。如何遍历 with_together:
- "{{ projects_to_build.stdout | from_json }}"
- "{{ lambda_config.results | list }}"
中包含的lambda结果列表?
====编辑调试====
我已经有一个调试步骤,并且确实如我所期望的那样出现了,但是@larsks到目前为止并没有帮助我,也许它可以为您提供帮助?
lambda_config.results