将Ansible变量传递到自定义Ansible模块中

发布于 2025-02-05 17:22:14 字数 1638 浏览 3 评论 0原文

我有一个自定义模块,该模块位于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

养猫人 2025-02-12 17:22:15

您确实需要使模块意识到您要接受的参数,因此,在main函数中:

#!/usr/bin/env python
from ansible.module_utils.basic import AnsibleModule


def create_repo(repositoryName, kmsKey):
  # Call to the API comes here


def main():
  module = AnsibleModule(
    argument_spec = dict(
      repositoryName = dict(type = 'str', required = True),
      kmsKey = dict(type = 'str', required = True),
    )
  )

  params = module.params
  
  create_repo(
    params['repositoryName'], 
    params['kmsKey']
  )


if __name__ == '__main__':
    main()

可以在相关文档中找到更多:


这样,您的攻击是:

- name: Create repo and use specific KMS key
  ecr_kms:
    repositoryName: "{{ repo_name }}"
    kmsKey: "{{ kms_key_id }}"

ps,建议:避免在yaml钥匙中使用点,这只会使您的生活变得复杂,这绝不是没有真正的充分理由。

You do need to make your module aware of the arguments you want it to accept, so, in your main function:

#!/usr/bin/env python
from ansible.module_utils.basic import AnsibleModule


def create_repo(repositoryName, kmsKey):
  # Call to the API comes here


def main():
  module = AnsibleModule(
    argument_spec = dict(
      repositoryName = dict(type = 'str', required = True),
      kmsKey = dict(type = 'str', required = True),
    )
  )

  params = module.params
  
  create_repo(
    params['repositoryName'], 
    params['kmsKey']
  )


if __name__ == '__main__':
    main()

More can be found in the relevant documentation: Argument spec.


With this, your taks would be:

- name: Create repo and use specific KMS key
  ecr_kms:
    repositoryName: "{{ repo_name }}"
    kmsKey: "{{ kms_key_id }}"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文