Get a list of --limit servers

52 views
Skip to first unread message

bmcc...@blurb.com

unread,
Nov 22, 2016, 6:32:36 PM11/22/16
to Ansible Project
I'm using the icinga2-ansible-add-hosts role, which creates config files for each host.
My playbook starts like this:
  ---
  - hosts: all
    gather_facts: True

  - hosts: "{{ hosts | default('monitoring_servers') }}"
  - strategy: debug
    - role: icinga2-ansible-add-hosts

Inside the add-hosts role is the following task:
--
- name: Copy Host Definitions
  template: src=hosts_template.j2
            dest={{ icinga2_hosts_dir }}/{{ hostvars[item]['ansible_fqdn'] }}.conf
            owner=root
            group=root
            mode=0644
  with_items: "{{ groups['all'] }}"
  notify:
   - restart icinga2

When I run `ansible-playbook -i inventory/ --limit <GROUP> icinga2.yml -vvv` I notice that gather_facts is only done on the hosts in <GROUP>, but the template tries to generate config files for all of my hosts, because "{{ groups['all'] }}" contains all of the hosts in the inventory directory, not just those in <GROUP>.

Checking Google and IRC it seems like there is no way to programmatically get the list of machines specified by --limit.  Is that correct?  Is there a way to iterate over a list of hosts that were specified on the command line?

Brian Coca

unread,
Nov 22, 2016, 6:42:45 PM11/22/16
to ansible...@googlegroups.com
​try the 'play_hosts' variable.​


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

bmcc...@blurb.com

unread,
Nov 22, 2016, 7:05:59 PM11/22/16
to Ansible Project
'play_hosts' doesn't work because the 'icinga2-ansible-add-hosts' role is only applied to "{{ hosts | default('monitoring_servers') }}"

What I'm trying to get is the list of hosts that 
- hosts: all
  gather_facts: yes
runs against, which is not all of the hosts in my inventory.

J Hawkesworth

unread,
Nov 23, 2016, 1:52:37 PM11/23/16
to Ansible Project
If you just want to make sure you are going to hit the right hosts before you run the playbook, use the --list-hosts command line parameter option of ansible-playbook.


I use --list-hosts and --list-tasks a lot.

Hope this helps,

Jon

Reply all
Reply to author
Forward
0 new messages