ansible-community / molecule-plugins

Collection on molecule plugins
MIT License
109 stars 73 forks source link

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: list object has no element 0 #53

Open geoffo-dev opened 3 years ago

geoffo-dev commented 3 years ago

When running a test in molecule I get the following error:

TASK [Populate instance config dict] *** fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: list object has no element 0\n\nThe error appears to be in '/home/geoff/working/ansible/dynatrace-deploy/molecule/aws/create.yml': line 114, 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: Populate instance config dict\n ^ here\n"}

having looked through the create.yml file and debugged the result of ec2-jobs it looks like the instances object is empty

- name: Populate instance config dict
  set_fact:
    instance_conf_dict: {
      'instance': "{{ item.instances[0].tags.instance }}",
      'address': "{{ item.instances[0].public_ip }}",
      'user': "{{ ssh_user }}",
      'port': "{{ ssh_port }}",
      'identity_file': "{{ key_pair_path }}",
      'instance_ids': "{{ item.instance_ids }}", }
  with_items: "{{ ec2_jobs.results }}"
  register: instance_config_dict
  when: server.changed | bool

It appears the data is now in the 'tagged_instances' object so have made the change.

- name: Populate instance config dict
  set_fact:
    instance_conf_dict: {
      'instance': "{{ item.tagged_instances[0].tags.instance }}",
      'address': "{{ item.tagged_instances[0].public_ip }}",
      'user': "{{ ssh_user }}",
      'port': "{{ ssh_port }}",
      'identity_file': "{{ key_pair_path }}",
      'instance_ids': "{{ item.instance_ids }}", }
  with_items: "{{ ec2_jobs.results }}"
  register: instance_config_dict
  when: server.changed | bool

Whilst changing this locally fixed the issue, a further issue occurs later:

TASK [Dump instance config] ****************************************************
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: no filter named 'molecule_header'. String: {{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"}

I will be honest, this is where I start to get a little lost... I am not sure where any of those variables come from so I cannot check whether they are still valid...

Can anyone help?

geoffo-dev commented 3 years ago

I should have mentioned I was having issues prior to this so installed the molecule_ec2-0.4.dev1 version.

geoffo-dev commented 3 years ago

subsequently I have tried on version 0.3 - I still had issues with 'tagged_instance' and the same issue with molecule header.

dogfish182 commented 3 years ago

@geoffo-dev it's because the ec2 module is run async so it always shows change. but it doesn't create ec2 instances all the time so when it dumps config there is nothing to dump.

I think you can just change

when: server.changed |bool 

to

when: ec2_jobs.changed |bool

but I just started looking at this myself and have to stop. Will pick it up again maybe later in the week. Essentially, it should only dump config if something really does change but it's picking that up on every task run, this will also break your destroy command so you can manually go delete your instances and start the create again and it will work once. Checking the async status for 'real change' is whats needed here. if you can figure that out you should be good.

also these filters

{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"}

(molecule_to_yaml and molecule_header) all got dropped some versions back

this PR seems to fix it but misses some stuff https://github.com/ansible-community/molecule-ec2/pull/34/files/f4f4185db69692630667e190a35a55efd16b1a9e