Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

Trying to register an ec2 instance in AWS with Ansible's ec2_ami module, and using current date/time as a version (we'll end up making a lot of AMIs in the future).

This is what I have:

- name: Create new AMI

  hosts: localhost

  connection: local

  gather_facts: false

  vars:

  tasks:

    - include_vars: ami_vars.yml

    - debug: var=ansible_date_time

    - name: Register ec2 instance as AMI

      ec2_ami: aws_access_key={{ ec2_access_key }}

               aws_secret_key={{ ec2_secret_key }}

               instance_id={{ temp_instance.instance_ids[0] }}

               region={{ region }}

               wait=yes

               name={{ ami_name }}

      with_items: temp_instance

      register: new_ami

From ami_vars.yml:

ami_version: "{{ ansible_date_time.iso8601 }}"

ami_name: ami_test_{{ ami_version }}

When I run the full playbook, I get this error message:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! ERROR! ERROR! 'ansible_date_time' is undefined"}

However, when run the debug command separately, from a separate playbook, it works fine:

- name: Test date-time lookup

  hosts: localhost

  connection: local

  tasks:

    - include_vars: ami_vars.yml

    - debug: msg="ami version is {{ ami_version }}"

    - debug: msg="ami name is {{ ami_name }}"

Result:

TASK [debug] *******************************************************************

ok: [localhost] => {

    "msg": "ami version is 2016-02-05T19:32:24Z"

}

TASK [debug] *******************************************************************

ok: [localhost] => {

    "msg": "ami name is ami_test_2016-02-05T19:32:24Z"

}

Any idea what's going on?

1 Answer

0 votes
by (44.4k points)

Just remove this:

gather_facts: false

This should work because ansible_date_time is part of the facts.

 

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
+1 vote
1 answer
0 votes
2 answers

Browse Categories

...