Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cloudwatchevent_rule: module breaks when trying to put Jinja2 variables to "input" parameter #201

Closed
fayaaz opened this issue Aug 25, 2020 · 4 comments · Fixed by #1355
Closed
Labels
affects_2.10 bug This issue/PR relates to a bug module module plugins plugin (any type) traceback

Comments

@fayaaz
Copy link

fayaaz commented Aug 25, 2020

Copied from here:
ansible/ansible#35224

SUMMARY
ISSUE TYPE
  • Bug Report
COMPONENT NAME

cloudwatchevent_rule

ANSIBLE VERSION
2.9.6
CONFIGURATION

OS / ENVIRONMENT
STEPS TO REPRODUCE

Template file base.yml

- name: "Deploy {{ command_id }}"
  cloudwatchevent_rule:
    name: "{{ command_id }}"
    schedule_expression: "{{ schedule }}"
    state: present
    targets:
      - id: "{{ command_id }}"
        arn: "{{ cluster_id }}"
        role_arn: "{{ iam_role_id }}"
        input: |
          {
            "containerOverrides":[
              {
                "name": "{{ command_id }}",
                "command":[
                  "php", "-f", "{{ file }}", "{{ task }}"
                ]
              }
            ]
          }
        ecs_parameters:
          task_count: 1
          task_definition_arn: "{{ task_definition_id }}"

Execution playbook tasks.yml

- hosts: localhost

  vars:
    cluster_id: arn:aws:ecs:us-east-1:1234567890:cluster/mycluster
    iam_role_id: arn:aws:iam::1234567890:role/ecsEventsRole
    task_definition_id: "arn:aws:ecs:us-east-1:1234567890:task-definition/mytask:{{ revision }}"
    file: tasks/run.php

  vars_prompt:
    - name: revision
      prompt: Please, enter the task definition for cron jobs

  tasks:
    - include_tasks: base.yml
      command_id: 'hello_world'
      task: 'hello-world'
      schedule: 'rate(1 minute)'

This is how I run this playbook.

ansible-playbook tasks.yml -e revision=1

Probably, can't be fully reproduced, because you have to make your own ECS cluster, task definition and so on.

To make it work without errors - just append the variables file and task directly to the base.yml and run it.

EXPECTED RESULTS

Ansible will create a new Cloudwatch event rule as a ECS scheduled task.

ACTUAL RESULTS
TASK [Deploy hello_world] *******************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: Invalid type for parameter Targets[0].Input, value: {'containerOverrides': [{'comman
d': ['php', '-f', 'tasks/run.php', 'hello-world'], 'name': 'hello_world'}]}, type: <type 'dict'>, valid types: <type 'basestring'>
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ans
ible_celAWz/ansible_module_cloudwatchevent_rule.py\", line 426, in <module>\n    main()\n  File \"/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ansible_celAWz/ansible_module_clou
dwatchevent_rule.py\", line 414, in main\n    cwe_rule_manager.ensure_present()\n  File \"/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ansible_celAWz/ansible_module_cloudwatchev
ent_rule.py\", line 270, in ensure_present\n    self._sync_targets()\n  File \"/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ansible_celAWz/ansible_module_cloudwatchevent_rule.py
\", line 321, in _sync_targets\n    self.rule.put_targets(targets_to_put)\n  File \"/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ansible_celAWz/ansible_module_cloudwatchevent_ru
le.py\", line 207, in put_targets\n    response = self.client.put_targets(**request)\n  File \"/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/site-packages/botocore/client.py\", line 312, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n  File \"/usr/local/Cellar/python/2.7.14/Frameworks/Python.frame
work/Versions/2.7/lib/python2.7/site-packages/botocore/client.py\", line 575, in _make_api_call\n    api_params, operation_model, context=request_context)\n  File \"/usr/local/Cella
r/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/botocore/client.py\", line 630, in _convert_to_request_dict\n    api_params, operation_model)\n
 File \"/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/botocore/validate.py\", line 291, in serialize_to_request\n    raise Par
amValidationError(report=report.generate_report())\nbotocore.exceptions.ParamValidationError: Parameter validation failed:\nInvalid type for parameter Targets[0].Input, value: {'con
tainerOverrides': [{'command': ['php', '-f', 'tasks/run.php', 'hello-world'], 'name': 'hello_world'}]}, type: <type 'dict'>, valid types: <type 'basestring'>\n", "module_stdout": "", "msg":
 "MODULE FAILURE", "rc": 0}
Traceback (most recent call last):
  File "/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ansible_ikw_Uz/ansible_module_cloudwatchevent_rule.py", line 426, in <module>
    main()
  File "/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ansible_ikw_Uz/ansible_module_cloudwatchevent_rule.py", line 414, in main
    cwe_rule_manager.ensure_present()
  File "/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ansible_ikw_Uz/ansible_module_cloudwatchevent_rule.py", line 270, in ensure_present
    self._sync_targets()
  File "/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ansible_ikw_Uz/ansible_module_cloudwatchevent_rule.py", line 321, in _sync_targets
    self.rule.put_targets(targets_to_put)
  File "/var/folders/sw/3qy0_mhx3v5dvhcztbc08nm40000gn/T/ansible_ikw_Uz/ansible_module_cloudwatchevent_rule.py", line 207, in put_targets
    response = self.client.put_targets(**request)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/botocore/client.py", line 312, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/botocore/client.py", line 575, in _make_api_call
    api_params, operation_model, context=request_context)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/botocore/client.py", line 630, in _convert_to_request_dict
    api_params, operation_model)
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/botocore/validate.py", line 291, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Targets[0].Input, value: {'containerOverrides': [{'command': ['php', '-f', 'tasks/run.php', 'hello-world'], 'name': 'hello_world'}]}, type: <type 'dict'>, valid types: <type 'basestring'>
@fayaaz
Copy link
Author

fayaaz commented Aug 25, 2020

I would like to add that we are also finding this error with version 2.9.6. We either get the error above or if you force it to a string, you get the error.

"msg": "Could not create/update rule targets for rule myrulename: An error occurred (ValidationException) when calling the PutTargets operation: JSON syntax error in input for target clone_lambda: [Source: (String)\"{'db_name':..<removed_extra_data>\"; line: 1, column: 3]"

@tremble
Copy link
Contributor

tremble commented Aug 26, 2020

CC: @jsdalton (module author)

Hi @fayaaz,

thank you for taking the time to raise this issue.

Where I've seen this before it's been a result of the argument parser trying to be clever and converting this in the background. The fix is likely to be explicitly defining the 'suboptions' for the "targets" argument so that input can be explicitly defined as a type 'json'. Unfortunately I don't have much experience with the cloudwatchevent_rule module so were I to start digging into this it may take a while.

Mark

@ansibullbot
Copy link

@ansibullbot ansibullbot added affects_2.10 bug This issue/PR relates to a bug module module plugins plugin (any type) traceback labels Aug 27, 2020
@jhosteny
Copy link

jhosteny commented Oct 9, 2020

Hi @fayaaz, did you find any workaround for this?

alinabuzachis pushed a commit to alinabuzachis/community.aws that referenced this issue May 25, 2022
…e-collections#201)

* support nested secrets for secrets lookup plugin and test coverage

* pep8 and pylint fixes
softwarefactory-project-zuul bot pushed a commit that referenced this issue Jul 14, 2022
cloudwatchevent_rule - Add argspec validation for targets

SUMMARY
fixes: #201
Targets currently has minimal validation applied.
Because of the way Ansible converts JSON strings to dicts/lists, then back to the Python format string representing the dicts/lists, unless we explicitly define a parameter is a JSON string they get corrupted.
This also moves the new input_paths_map/input_template parameters under input_transformer.  Because we've not released 4.1.0 yet this doesn't cause any breakage.  This will make adding other target parameters simpler further down the road.  (There's a lot that we don't support today)
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
cloudwatchevent_rule
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>
patchback bot pushed a commit that referenced this issue Jul 15, 2022
cloudwatchevent_rule - Add argspec validation for targets

SUMMARY
fixes: #201
Targets currently has minimal validation applied.
Because of the way Ansible converts JSON strings to dicts/lists, then back to the Python format string representing the dicts/lists, unless we explicitly define a parameter is a JSON string they get corrupted.
This also moves the new input_paths_map/input_template parameters under input_transformer.  Because we've not released 4.1.0 yet this doesn't cause any breakage.  This will make adding other target parameters simpler further down the road.  (There's a lot that we don't support today)
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
cloudwatchevent_rule
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>
(cherry picked from commit 5f0e10e)
softwarefactory-project-zuul bot pushed a commit that referenced this issue Jul 15, 2022
[PR #1355/5f0e10e7 backport][stable-4] cloudwatchevent_rule - Add argspec validation for targets

This is a backport of PR #1355 as merged into main (5f0e10e).
SUMMARY
fixes: #201
Targets currently has minimal validation applied.
Because of the way Ansible converts JSON strings to dicts/lists, then back to the Python format string representing the dicts/lists, unless we explicitly define a parameter is a JSON string they get corrupted.
This also moves the new input_paths_map/input_template parameters under input_transformer.  Because we've not released 4.1.0 yet this doesn't cause any breakage.  This will make adding other target parameters simpler further down the road.  (There's a lot that we don't support today)
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
cloudwatchevent_rule
ADDITIONAL INFORMATION

Reviewed-by: Mark Chappell <None>
abikouo pushed a commit to abikouo/community.aws that referenced this issue Oct 24, 2023
cloudwatchevent_rule - Add argspec validation for targets

SUMMARY
fixes: ansible-collections#201
Targets currently has minimal validation applied.
Because of the way Ansible converts JSON strings to dicts/lists, then back to the Python format string representing the dicts/lists, unless we explicitly define a parameter is a JSON string they get corrupted.
This also moves the new input_paths_map/input_template parameters under input_transformer.  Because we've not released 4.1.0 yet this doesn't cause any breakage.  This will make adding other target parameters simpler further down the road.  (There's a lot that we don't support today)
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
cloudwatchevent_rule
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections@5f0e10e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects_2.10 bug This issue/PR relates to a bug module module plugins plugin (any type) traceback
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants