将Ansible变量传递到自定义Ansible模块中
我有一个自定义模块,该模块位于library/
我的Ansible角色目录中。我可以从剧本中调用该模块,并且代码执行正确,但是只有当它期望在模块代码本身中进行硬编码的值。如何将值传递到剧本中的模块?
我已经尝试了以下操作:
- name: Create repo and use specific KMS key
ecr_kms:
repositoryName: "new-ecr-repo"
encryptionConfiguration.kmsKey: 'my-kms-key-id"
我期望它
- name: Create repo and use specific KMS key
ecr_kms:
repositoryName: "{{ repo_name }}"
encryptionConfiguration.kmsKey: "{{ kms_key_id }}"
可以正常工作,但我却没有,我会收到以下错误:
botocore.exceptions.paramvalidationerror:参数验证失败:
参数repositoryName的无效长度,值:0,有效的最小长度:2
参数EncryptionConfiguration.kmskey,值:0,有效的最小长度:1
自定义模块的代码:
#!/usr/bin/python
from urllib import response
import boto3
from jinja2 import Template
from ansible.module_utils.basic import AnsibleModule
def create_repo():
client = boto3.client('ecr')
response = client.create_repository(
#registryId='',
repositoryName='',
imageTagMutability='IMMUTABLE',
imageScanningConfiguration={
'scanOnPush': True
},
encryptionConfiguration={
'encryptionType': 'KMS',
'kmsKey': ""
}
)
def main():
create_repo()
if __name__ in '__main__':
main()
I have a custom module that resides in the library/
directory of my Ansible role. I can call the module from within my playbook, and the code executes correctly, but only if the values it expects are hardcoded in the module code itself. How can I pass values to the module from the playbook?
I've tried the following:
- name: Create repo and use specific KMS key
ecr_kms:
repositoryName: "new-ecr-repo"
encryptionConfiguration.kmsKey: 'my-kms-key-id"
and
- name: Create repo and use specific KMS key
ecr_kms:
repositoryName: "{{ repo_name }}"
encryptionConfiguration.kmsKey: "{{ kms_key_id }}"
Which I would expect to work, but neither does and, I get the following errors:
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid length for parameter repositoryName, value: 0, valid min length: 2
Invalid length for parameter encryptionConfiguration.kmsKey, value: 0, valid min length: 1
The service module I'm trying to use
The code of the custom module:
#!/usr/bin/python
from urllib import response
import boto3
from jinja2 import Template
from ansible.module_utils.basic import AnsibleModule
def create_repo():
client = boto3.client('ecr')
response = client.create_repository(
#registryId='',
repositoryName='',
imageTagMutability='IMMUTABLE',
imageScanningConfiguration={
'scanOnPush': True
},
encryptionConfiguration={
'encryptionType': 'KMS',
'kmsKey': ""
}
)
def main():
create_repo()
if __name__ in '__main__':
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实需要使模块意识到您要接受的参数,因此,在
main
函数中:可以在相关文档中找到更多:
这样,您的攻击是:
ps,建议:避免在yaml钥匙中使用点,这只会使您的生活变得复杂,这绝不是没有真正的充分理由。
You do need to make your module aware of the arguments you want it to accept, so, in your
main
function:More can be found in the relevant documentation: Argument spec.
With this, your taks would be:
PS, word of advice: avoid using a dot in a YAML key, that would just be making your life complicated for no actual good reason.