Fix gathering facts in run_once play (#39453)

* Fix gathering facts in run_once play

Fixes https://github.com/ansible/ansible/issues/39312

* Check that run_once doesn't prevent fact gathering
pull/38152/merge
Mickaël Guérin 7 years ago committed by Brian Coca
parent 05d5d21d1c
commit 8ac69b0a5f

@ -169,6 +169,9 @@ class PlayIterator:
fact_path = self._play.fact_path fact_path = self._play.fact_path
setup_block = Block(play=self._play) setup_block = Block(play=self._play)
# Gathering facts with run_once would copy the facts from one host to
# the others.
setup_block.run_once = False
setup_task = Task(block=setup_block) setup_task = Task(block=setup_block)
setup_task.action = 'setup' setup_task.action = 'setup'
setup_task.name = 'Gathering Facts' setup_task.name = 'Gathering Facts'

@ -5,3 +5,5 @@ set -eux
# ANSIBLE_CACHE_PLUGINS=cache_plugins/ ANSIBLE_CACHE_PLUGIN=none ansible-playbook test_gathering_facts.yml -i ../../inventory -v "$@" # ANSIBLE_CACHE_PLUGINS=cache_plugins/ ANSIBLE_CACHE_PLUGIN=none ansible-playbook test_gathering_facts.yml -i ../../inventory -v "$@"
ansible-playbook test_gathering_facts.yml -i ../../inventory -v "$@" ansible-playbook test_gathering_facts.yml -i ../../inventory -v "$@"
#ANSIBLE_CACHE_PLUGIN=base ansible-playbook test_gathering_facts.yml -i ../../inventory -v "$@" #ANSIBLE_CACHE_PLUGIN=base ansible-playbook test_gathering_facts.yml -i ../../inventory -v "$@"
ANSIBLE_GATHERING=smart ansible-playbook test_run_once.yml -i ../../inventory -v "$@"

@ -0,0 +1,28 @@
---
- hosts: facthost1
gather_facts: no
tasks:
- name: install test local facts
copy:
src: uuid.fact
dest: /etc/ansible/facts.d/
mode: 0755
- hosts: facthost1,facthost2
gather_facts: yes
run_once: yes
tasks:
- block:
- name: 'Check the same host is used'
assert:
that: 'hostvars.facthost1.ansible_fqdn == hostvars.facthost2.ansible_fqdn'
msg: 'This test requires 2 inventory hosts referring to the same host.'
- name: "Check that run_once doesn't prevent fact gathering (#39453)"
assert:
that: 'hostvars.facthost1.ansible_local.uuid != hostvars.facthost2.ansible_local.uuid'
msg: "{{ 'Same value for ansible_local.uuid on both hosts: ' ~ hostvars.facthost1.ansible_local.uuid }}"
always:
- name: remove test local facts
file:
path: /etc/ansible/facts.d/uuid.fact
state: absent

@ -0,0 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import uuid
# return a random string
print(json.dumps(str(uuid.uuid4())))
Loading…
Cancel
Save