How do you register variables from imported playbooks?

Hello,

I’m new to ansible and am trying to use import_playbook and register but it is not working. I have a playbook called vpc.yml:

  • hosts: localhost
    tasks:
  • import_role:
    name: vpc
    vars:
    aws_region: “{{aws_region}}”
    register: public_subnet

within another playbook:

  • import_playbook: vpc.yml

register: public_subnet

  • hosts: localhost
    connection: local
    gather_facts: False
    name: set up EC2 instances
    tasks:
  • name: Provision a set of instances
    ec2:
    key_name: AnsibleTest

group: test

instance_type: t2.micro
image: ami-900ae6e8
wait: true
exact_count: “{{ node_count }}”
count_tag:
Name: Test
instance_tags:
Name: Test
region: “{{aws_region}}”
vpc_subnet_id: public_subnet

register: ec2

If I run the above I get an error saying the subnet ID public_subnet is undefined, but if I try to change the import_playbook to user the register: public_subnet line commented above I get a syntax error.

I understand I could rather easily just use the VPC role in my top level playbook but I’m trying to learn. From what I understand this should be possible, or at least was possible before 2.4. What am I missing here?

Thanks!
Dave

You are getting an error because you are passing a string, the
'register' keyword is 'host specific' since the host in both plays is
'localhost' it is directly accessible, you should have:

vpc_subnet_id: "{{public_subnet}}"