强制Ansible从集团收集事实

时间:2016-05-10 15:56:27

标签: postgresql ansible ansible-playbook ansible-facts

我想强迫Ansible收集关于playbook内部主机的事实(在角色中使用这些数据),不管是限制,但不知道如何。

我有这样的剧本:

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

- hosts: postgres
  roles:
    - postgres

内部' postgres'角色我有模板,它迭代默认IP:

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

这就像魔法一样,但只有在没有--limit的情况下运行我的剧本。如果我使用--limit就会中断,因为hostgroup中的某些主机没有收集到的事实。

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

失败:[pgtest](item = pg_hba.conf)=> {"失败":true," item":" pg_hba.conf"," msg":" AnsibleUndefinedVariable:&#39 ; dict对象'没有属性' ansible_default_ipv4'"}

我如何才能 - 仅限制重新配置postgres主机,并从其他主机获取网络数据(不进行所有其他配置工作?)。

4 个答案:

答案 0 :(得分:1)

请试试这个!

- hosts: postgres

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

  roles:
    - postgres

答案 1 :(得分:1)

您可以将setup组中的主机postgres_access作为任务运行,并使用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]

请记住,模板需要更改引用主机ipv4地址的方式,我试过这样的事情:

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

仅打印组中每个主机的IP

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:0)

使用您使用ignore_errors: true定义的相同角色。因此,对于没有收集数据的主机,不会失败。

如果您希望为postgres_access和postgres两个组中的所有主机收集数据,则添加gather_facts: true以获取postgres组的事实,并为postgres_access添加已编写的任务。

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

- hosts: postgres
  gather_facts: true
  roles:
    - postgres
  ignore_errors: true
相关问题