Setting Variable across multiple hosts blocks

26 views
Skip to first unread message

Andrew Morgan

unread,
May 30, 2020, 5:47:10 PM5/30/20
to Ansible Project
Hello All,

I am having an issue when I set a fact, I am not able to see that fact after I have another host block in my playbook. I get the error variable is undefined. The issue occurs in my  - name: Launch instance


---
- hosts: all
 become: yes
 #connection: local

  tasks:

  - name: Get Instance info
   register: ec2_facts


  - name: setting fact for Security group
   set_fact:
      SERCURITYGROUP : "{{ item.group_name }}"
   with_items: "{{ ec2_facts.instances[0].network_interfaces[0].groups }}"  


- hosts: localhost
 gather_facts: False
 vars:
   keypair: ansible
   instance_type: m1.small
   image: "{{ ami_id }}"
   region: us-east-1
   subnet: "{{ SUBNETID }}"

  tasks:

  - name: Launch instance
   ec2:
     key_name: "{{ keypair }}"
     group: "{{ SERCURITYGROUP }}"
     instance_type: "{{ instance_type }}"
     image: "{{ image }}"
     wait: true
     region: "{{ region }}"
     vpc_subnet_id: "{{ subnet}}"
     assign_public_ip: yes
   register: ec2


TASK [Launch instance] *********************************************************************************************
fatal
: [localhost]: FAILED! =>
  msg
: |-
   
The task includes an option with an undefined variable. The error was: 'SERCURITYGROUP' is undefined


I have set the variable as a fact, so I am not sure why I cannot call it with another --host stanza . 

Dick Visser

unread,
May 30, 2020, 11:35:14 PM5/30/20
to ansible...@googlegroups.com
Facts are set per host. You mix all with localhost.

What are you trying to do?


--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5f6a9570-cd45-430b-b4c3-0a7e5afa1d25%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Andrew Morgan

unread,
May 31, 2020, 8:05:12 PM5/31/20
to Ansible Project
Thank you for the reply.. I thought if I set host all at the top and then set the facts for host all it would work. I guess I would have to have localhost in my inventory file correct?

I am just trying to launch a ec2 instance in my playbook
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Brian Coca

unread,
Jun 1, 2020, 11:49:27 AM6/1/20
to Ansible Project
The 'implicit localhost' is not part of ALL
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b416d54d-9d94-4d6f-8c24-023bc5d4162e%40googlegroups.com.



--
----------
Brian Coca

Brian Coca

unread,
Jun 1, 2020, 11:50:26 AM6/1/20
to Ansible Project
The 'implicit localhost' is not part of ALL:
https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html



--
----------
Brian Coca

James Cassell

unread,
Jun 1, 2020, 5:16:24 PM6/1/20
to Ansible List

On Mon, Jun 1, 2020, at 11:49 AM, Brian Coca wrote:
> The 'implicit localhost' is not part of ALL:
> https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html
>

Documentation gold!

Thanks!

V/r,
James Cassell

Andrew Morgan

unread,
Jun 1, 2020, 6:51:50 PM6/1/20
to Ansible Project
Thank you for all your assistance! :)
Reply all
Reply to author
Forward
0 new messages