filmov
tv
Getting and Using IP addresses in Ansible Playbooks
Показать описание
An example playbook describing how you can fetch the IP addresses of different ansible managed hosts, using Ansible itself, and then use it in subsequent Ansible plays.
The script that I ran:
- name: Set Cluster variables
hosts: cluster
tasks:
cluster_ips: []
cluster_ips: "{{ cluster_ips + [ hostvars[item]['ansible_default_ipv4']['address'] ] }}"
with_items: "{{ groups['cluster'] }}"
run_once: true
cluster_names: "{{ groups['cluster'] }}"
run_once: true
# Variables for cluster set in the above three plays
- name: Print Variables
hosts: cluster
become: yes
run_once: true
tasks:
- name: Add UFW Rules
hosts: cluster
become: yes
tasks:
name: ufw
state: present
rule: allow
name: OpenSSH
rule: allow
src: '{{ item }}'
with_items: '{{ cluster_ips }}'
state: enabled
With Inventory
[cluster]
node-00
node-01
node-02
This example should scale to any number of nodes. Although Ansible itself will get significantly slower because the number of rules would increase non-linearly (O(n^2))
The script that I ran:
- name: Set Cluster variables
hosts: cluster
tasks:
cluster_ips: []
cluster_ips: "{{ cluster_ips + [ hostvars[item]['ansible_default_ipv4']['address'] ] }}"
with_items: "{{ groups['cluster'] }}"
run_once: true
cluster_names: "{{ groups['cluster'] }}"
run_once: true
# Variables for cluster set in the above three plays
- name: Print Variables
hosts: cluster
become: yes
run_once: true
tasks:
- name: Add UFW Rules
hosts: cluster
become: yes
tasks:
name: ufw
state: present
rule: allow
name: OpenSSH
rule: allow
src: '{{ item }}'
with_items: '{{ cluster_ips }}'
state: enabled
With Inventory
[cluster]
node-00
node-01
node-02
This example should scale to any number of nodes. Although Ansible itself will get significantly slower because the number of rules would increase non-linearly (O(n^2))