ansible-community / molecule-plugins

Collection on molecule plugins
MIT License
101 stars 67 forks source link

Error creating EC2 image if passing in an AMI id instead of having create.yml look it up #45

Open isuftin opened 2 years ago

isuftin commented 2 years ago

If I pass an image id along with the platform configuration, I get the following error:

The task includes an option with an undefined variable. The error was: {{ item.image or platform_generated_image_id }}: {{ (ami_info.results[index].images | sort(attribute='creation_date', reverse=True))[0].image_id }}: 'dict object' has no attribute 'images'

The problem seems to be that if I do pass in an image, the lookup play is skipped: https://github.com/ansible-community/molecule-ec2/blob/0.4/molecule_ec2/cookiecutter/%7B%7Bcookiecutter.molecule_directory%7D%7D/%7B%7Bcookiecutter.scenario_name%7D%7D/create.yml#L138-L147

As a result, Ansible hits this line: https://github.com/ansible-community/molecule-ec2/blob/0.4/molecule_ec2/cookiecutter/%7B%7Bcookiecutter.molecule_directory%7D%7D/%7B%7Bcookiecutter.scenario_name%7D%7D/create.yml#L221

platform_image_id: "{{ item.image or platform_generated_image_id }}"

And even though item.image exists, Ansible still tries to run the line above it: https://github.com/ansible-community/molecule-ec2/blob/0.4/molecule_ec2/cookiecutter/%7B%7Bcookiecutter.molecule_directory%7D%7D/%7B%7Bcookiecutter.scenario_name%7D%7D/create.yml#L220

platform_generated_image_id: "{{ (ami_info.results[index].images | sort(attribute='creation_date', reverse=True))[0].image_id }}"

And that's where the error happens.

If I set platform_image_id to just platform_image_id: "{{ item.image }}" everything works fine

isuftin commented 2 years ago

I think this runs into this issue: https://stackoverflow.com/questions/34621799/ansible-how-do-i-avoid-registering-a-variable-when-a-when-condition-is-not

isuftin commented 2 years ago

Setting the line to platform_image_id: '{{ item.image if item.image is defined else (ami_info.results[index].images | sort(attribute="creation_date", reverse=True))[0].image_id }}' like it was in 0.3 ( https://github.com/ansible-community/molecule-ec2/blob/0.3/molecule_ec2/cookiecutter/%7B%7Bcookiecutter.molecule_directory%7D%7D/%7B%7Bcookiecutter.scenario_name%7D%7D/create.yml#L82-L84 ) fixes things

danielpodwysocki commented 1 year ago