botang 发表于 2020-8-8 16:24:25

ANSIBLE5-ANSIBLE FACTS变量

---
- name: Fact Dump
hosts: srvgroup
tasks:
- name: Print All Facts
    debug:
      #var: ansible_facts.fqdn
      msg: >
      LONG HOSTNAME IS "{{ ansible_facts.fqdn }}".
      SHORT HOSTNAME IS "{{ ansible_hostname }}".
      DEFAULT IP ADDRESS IS "{{ ansible_facts.default_ipv4.address }}".
      DEFAULT IP ADDRESS IS "{{ ansible_default_ipv4.address }}".

---
- name: Fact Dump
hosts: srvgroup
tasks:
- name: Print All Facts
    debug:
      #var: ansible_facts.fqdn
      msg: >
      LONG HOSTNAME IS "{{ ansible_facts.fqdn }}".
      SHORT HOSTNAME IS "{{ ansible_hostname }}".
      DEFAULT IP ADDRESS IS "{{ ansible_facts.default_ipv4.address }}".
      DEFAULT IP ADDRESS IS "{{ ansible_default_ipv4.address }}".
      DEFAULT IP ADDRESS IS "{{ ansible_facts.default_ipv4.address }}".
~                                                                        

---
- name: Fact Dump
hosts: srvgroup
gather_facts: no
tasks:
- name: Print All Facts
    debug:
      var: ansible_facts

---
- name: Fact Dump
hosts: srvgroup
gather_facts: no
tasks:
- name: Print All Facts
    setup:
    register: v_setup

- debug:
      var: v_setup

---
- name: Local Var
hosts: srvgroup
gather_facts: yes
tasks:
- name: var
    debug:
      msg: >
      The value to know "{{ ansible_facts['fqdn'] }}".
      The value to know "{{ ansible_facts.fqdn }}".
      The value to know "{{ ansible_fqdn }}".
      is "{{ ansible_facts['ansible_local'] }}".
      The ip addresses is "{{ ansible_all_ipv4_addresses }}".

---
- name: Local Var
hosts: srvgroup
gather_facts: yes
tasks:
- name: var
    debug:
      msg: |
      The value to know "{{ ansible_facts['fqdn'] }}".
      The value to know "{{ ansible_facts.fqdn }}".
      The value to know "{{ ansible_fqdn }}".
      is "{{ ansible_facts['ansible_local'] }}".
      The ip addresses is "{{ ansible_all_ipv4_addresses }}".
      The ip addresses is "{{ ansible_facts['all_ipv4_addresses'] }}".
补充默认的事实,添加本地事实变量:
---
- name: Local Facts
hosts: srvgroup
vars:
    remote_dir: /etc/ansible/facts.d
    facts_file: ./v3.d/local.fact
tasks:
- name: Create Remote Directory
    file:
      path: "{{ remote_dir }}"
      recurse: yes
      state: directory

- name: Copy Fact
    copy:
      src: "{{ facts_file }}"
      dest: "{{ remote_dir }}"
./v3.d/local.fact:

v1: 1
v2: 2

ansible srvgroup -m setup| grep -A 20 -B 20 v1

页: [1]
查看完整版本: ANSIBLE5-ANSIBLE FACTS变量