0
votes

I want to force Ansible to gather facts about hosts inside playbook (to use those data inside role) regardless --limit, but don't know how.

I have playbook like this:

- hosts: postgres_access
  tasks:
  - name: Gathering info
    action: setup

- hosts: postgres
  roles:
    - postgres

Inside 'postgres' role I have template which iterates over default IPs:

{% for host in groups['postgres_access'] %}
host all all {{hostvars[host].ansible_default_ipv4.address}}/32 md5
{% endfor %}

This works like magic, but only if I run my playbook without --limit. If I use --limit it breaks, because some hosts in hostgroup have no gathered facts.

ansible-playbook -i testing db.yml --limit postgres

failed: [pgtest] (item=pg_hba.conf) => {"failed": true, "item": "pg_hba.conf", "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_default_ipv4'"}

How can I have --limit to reconfigure only postgres host, and have network data from other hosts (without doing all other configuration stuff?).

4
Have you tried a different approach like tagging your postgres tasks and specifying --tags postgres? That way you are not running a limit by inventory groups but by specific tasksuser5507598
yes, I think I will use tags. It still would not help with 'configure single host' case (like it often used).George Shuklin
Made a task for solving this, I put it within the roles/postgres/tasks/main.yml file but I bet the location is not relevant if you run it from the playbook instead. See my answer at the bottom.user5507598

4 Answers

2
votes

Try this please !

- hosts: postgres

  pre_tasks:
  - setup:
    delegate_to: "{{item}}"
    with_items: "{{groups['postgres_access']}}"

  roles:
    - postgres
1
votes

You can run setup for the hosts in the postgres_access group as a task and save the facts using register:

  - name: setup hosts
  action: "setup {{ item }} filter=ansible_default_ipv4"
  with_items: groups.postgres_access
  register: ip_v4
- name: create template template: src=[your template] dest=[your dest file]

Just keep in mind that the template needs to change how you are referencing the hosts ipv4 address, I tried with something like this:

{% for item in ip_v4.results %}
    host all all {{ item.ansible_facts.ansible_default_ipv4.address }}/32 md5
{% endfor %}

For printing just the IP of each host in the group

0
votes

Try this:

- hosts: postgres
  pre_tasks:
  - setup:
     delegate_to: postgres_access
  roles:
    - postgres
0
votes

Use the same role which you have defined as it is with ignore_errors: true . So that for hosts which do not have gathered data will not fail.

And if you want data to be gathered for all the hosts in both groups postgres_access and postgres, then add gather_facts: true to get facts for postgres group and for postgres_access you already have a task written.

- hosts: postgres_access
  tasks:
  - name: Gathering info
    action: setup

- hosts: postgres
  gather_facts: true
  roles:
    - postgres
  ignore_errors: true