# Retaining information about hosts after add\_hosts

**URL:** <https://forum.ansible.com/t/retaining-information-about-hosts-after-add-hosts/16164>\
**Category:** Ansible Project\
**Created:** [September 9, 2014, 7:36pm UTC](https://forum.ansible.com/t/retaining-information-about-hosts-after-add-hosts/16164 "2014-09-09T19:36:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kurt\_Yoder](https://avatars.discourse-cdn.com/v4/letter/k/da6949/32.png) [@Kurt\_Yoder](https://forum.ansible.com/u/Kurt_Yoder)\
**Post date:** [September 9, 2014, 7:36pm UTC](https://forum.ansible.com/t/retaining-information-about-hosts-after-add-hosts/16164/1 "2014-09-09T19:36:55Z")

</div>

Hi list,

I posted a while back about a way to parallelize Openstack node creation. To recap, I have a role with the following task:

`

- name: Set up API connections for all Openstack nodes  
add\_host:  
name: “os\_api\_{{ item }}”  
ansible\_ssh\_host: 127.0.0.1  
groups: os\_api  
ansible\_connection: local  
oshost: “{{ item }}”  
with\_items: cluster

`

This gives me a bunch of API connections which I run in parallel in another role and task:

`

- name: Launch cluster VM on Openstack  
nova\_compute:  
name: “{{ os\_username }}\_{{ oshost }}”  
state: present  
login\_username: “{{ os\_username }}”  
login\_tenant\_name: “{{ os\_tenant }}”  
login\_password: “{{ os\_password }}”  
image\_id: “{{ os\_image\_id }}”  
key\_name: “{{ os\_username }}\_controller\_key”  
wait\_for: 200  
flavor\_id: “{{ os\_flavor\_id }}”  
auth\_url: “{{ os\_url }}”  
user\_data: “{{ lookup(‘template’, ‘…/templates/cloud-config.j2’) }}”

- name: Assign IP address to cluster VM  
quantum\_floating\_ip:  
state: present  
login\_username: “{{ os\_username }}”  
login\_password: “{{ os\_password }}”  
login\_tenant\_name: “{{ os\_tenant }}”  
network\_name: “{{ os\_network\_name }}”  
instance\_name: “{{ os\_username }}\_{{ oshost }}”  
internal\_network\_name: “{{ os\_internal\_network\_name }}”  
auth\_url: “{{ os\_url }}”  
register: quantum\_info

- name: Wait for cluster SSH to become available  
wait\_for:  
port: 22  
host: “{{ quantum\_info.public\_ip }}”  
timeout: 180  
state: started

- name: Retrieve cluster public SSH host key  
shell: “ssh-keyscan {{ quantum\_info.public\_ip }}”  
register: scanned\_key  
`

Now I have a list of IPs for the configured hosts. I want to record their SSH host key, as captured by “scanned\_key”. If I add the following, will it safely serialize access to the local “known\_hosts” file:

`

- name: Set SSH known\_hosts entry  
lineinfile:  
dest: ~/.ssh/known\_hosts  
line: “{{ scanned\_key.stdout }}”  
state: present  
regexp: "^{{ quantum\_info.public\_ip }} "  
delegate\_to: localhost

`

Is this the recommended way to do it?

Thanks,

-Kurt

---

<div class="post-metadata">

**Author:** ![Kurt\_Yoder](https://avatars.discourse-cdn.com/v4/letter/k/da6949/32.png) [@Kurt\_Yoder](https://forum.ansible.com/u/Kurt_Yoder)\
**Post date:** [September 9, 2014, 8:17pm UTC](https://forum.ansible.com/t/retaining-information-about-hosts-after-add-hosts/16164/2 "2014-09-09T20:17:43Z")

</div>

I just did some more testing with this. The behavior is:

Ansible writes sometimes two, sometimes three entries to the file.

To me, this indicates that file access is not exclusive. So the parallel Ansible processes are all opening and closing the file, and last-one-in wins. I also tried with both

`delegate_to: localhost`

and

`connection: local`

Neither of these fixed the file consistency problem outlined above.

Other approaches:

- I can’t use “add\_host”, because it does not work in parallel (see [github issue](https://github.com/ansible/ansible/issues/2963)).

- I could retain “register” in variables and reuse it in a later role, but I can’t see a way to do it ([group thread](https://groups.google.com/forum/#!topic/ansible-project/SyH-bL8rJIA) about this is unanswered ATM).

- I guess I’ll have to write each host’s variables to a local yaml file, then read those files as variables in a later role. Seems clunky, but I see no other way.

Any other suggestions?

---

<div class="post-metadata">

**Author:** ![Kurt\_Yoder](https://avatars.discourse-cdn.com/v4/letter/k/da6949/32.png) [@Kurt\_Yoder](https://forum.ansible.com/u/Kurt_Yoder)\
**Post date:** [September 10, 2014, 1:08pm UTC](https://forum.ansible.com/t/retaining-information-about-hosts-after-add-hosts/16164/3 "2014-09-10T13:08:54Z")

</div>

I found a workaround which I am now using:

- Break out the ssh\_known\_hosts code into a new role

- Execute the new role with “serial: 1”

-Kurt
