Alright I have no idea what I am missing here but this is not working.

I added everything from the role into the same playbook that launches the 
server and it still has no clue what the variable is when I try to use it 
in a j2 template.

Keep in mind here there are two stages to this playbook. The local actions 
that LAUNCH the ec2 instance and then I configure the remote instance that 
was launched. All in the same play. Whereas before I was just launching 
then calling a role to run on it. I create groupname=launched then when it 
comes time to configure I use hosts: launched and everything else works but 
my vars don't carry over.

Here are the relevant bits:

    - name: Associate a new Elastic IP to instance
      ec2_eip: region=us-east-1 in_vpc=yes "instance_id={{ item }}"
      with_items: ec2.instance_ids
      register: eiptest

    - name: Set elastic IP fact
      set_fact: elasticIP="{{ eiptest.results[0].public_ip }}"

    - name: Output the IP
      debug: msg="The elastic IP is {{ elasticIP }}"  <--- This prints out 
the IP properly. No problem. Example: "msg": "The elastic IP is 
104.104.104.104"

    - name: Add new instance to host group
      local_action: add_host hostname={{ item.private_ip }} 
groupname=launched
      with_items: ec2.instances

    - name: Wait for SSH to come up
      local_action: wait_for host={{ item.private_ip }} port=22 delay=60 
timeout=320 state=started
      with_items: ec2.instances

After SSH comes up the play continues with the following:

- name: Install and configure server
  hosts: launched
  remote_user: ec2-user
  sudo: yes
  tasks:
        - name: Copy rpm packages for installation
        copy: src=packages/{{ item }} dest=/tmp/{{ item }}
        with_items:
        - a-linux-rpm-el6.x86_64.rpm
        - another-rpm-el6.x86_64.rpm

      - name: Copy configuration file
        template: src=config.conf.j2 dest=/etc/linuxstuff/config.conf
        notify:
        - restart server

And finally IN the config file itself is this line:

external-ip="{{ elasticIP }}"  NOTE: I have tried many different quote 
styles here

And I get this error:

TASK: [Copy server configuration] 
***********************************************
fatal: [10.10.10.10 => {'msg': "AnsibleUndefinedVariable: One or more 
undefined variables: 'elasticIP' is undefined", 'failed': True}
fatal: [10.10.10.10] => {'msg': "AnsibleUndefinedVariable: One or more 
undefined variables: 'elasticIP' is undefined", 'failed': True}



On Sunday, December 14, 2014 2:01:26 PM UTC-6, [email protected] wrote:
>
> Looks good to me. But I am still fighting through this on my end.
>
> It seems like it should be pretty straightforward. 
>
> - Launch the instance
> - Allocate elastic IP then use the register keyword to save it to a var. 
> (I even tried set_fact with no luck)
> - In the template add {{ var.public_ip }}.
>
> It will print the correct IP during a playbook run using debug but when it 
> gets to the point of building the config from the template it seems the 
> variable no longer exists.
>
> I am going to try some other things or just put everything in the same 
> one-off play.
>
> Thanks for the replies.
>
> On Wednesday, December 10, 2014 2:11:41 PM UTC-6, [email protected] wrote:
>>
>> I am launching one-off ec2 instance for development purposes and in the 
>> main playbook I am launching an EC2 instance then assigning an elastic IP. 
>> Once all of that is done I am configuring the server via a role. I am able 
>> to capture the elastic IP via register but how do I call it from the role? 
>> Or more specifically how can I call it in a configuration template from 
>> within the role that is applied?
>>
>> Here's an example:
>>
>> launch-server.yml:
>>
>> - name: Launch ec2 instance
>>   hosts: localhost
>>   gather_facts: False
>>   vars:
>>     key_name: ssh-key
>>     instance_type: t2.medium
>>     group_id: sg-f834234
>>     image: ami-b234234
>>     region: us-east-1
>>     vpc_subnet_id: subnet-65wer234
>>   tasks:
>>
>>     - name: Launch instance
>>       local_action: ec2 key_name={{ key_name }} group_id={{ group_id }} 
>> instance_type={{ instance_type }} image={{ image }} wait=true region={{ 
>> region }} vpc_subnet_id={{ vpc_subnet_id }} 
>> instance_tags='{"Name":"ec2-instance"}'
>>       register: ec2
>>
>>     - name: allocate a new elastic IP without associating it to anything
>>       action: ec2_eip region=us-east-1
>>       register: eiptest
>>
>>     - name: output the IP
>>       debug: msg="Allocated IP is {{ eiptest.public_ip }}" <--- this 
>> prints the elastic IP to the screen.
>>
>>     - name: Add new instance to host group
>>       local_action: add_host hostname={{ item.private_ip }} 
>> groupname=launched
>>       with_items: ec2.instances
>>
>>     - name: Wait for SSH to come up
>>       local_action: wait_for host={{ item.private_ip }} port=22 delay=60 
>> timeout=320 state=started
>>       with_items: ec2.instances
>>
>> - name: Configure instance
>>   hosts: launched
>>   sudo: True
>>   gather_facts: True
>>   roles:
>>     - myrole
>>
>> In the template from within the role it doesn't know about 
>> eiptest.public_ip so it fails and reading the docs it seems that var will 
>> be destroyed at the end of the play that calls the role. If I run ansible 
>> hostname -m setup I don't see any entries for the elastic IP assigned to 
>> the instance.
>>
>> I've tried a number of things and read through the docs but not having 
>> much luck. I would like to keep the roles separate from the plays that 
>> launch the instances if at all possible.
>>
>> Thanks!
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/27c93297-051c-4055-a7a0-b70885f1d85c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to