Bo's Oracle Station

查看: 1088|回复: 0

ANSIBLE5-ANSIBLE FACTS变量

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2020-8-8 16:24:25 | 显示全部楼层 |阅读模式
  1. ---
  2. - name: Fact Dump
  3.   hosts: srvgroup
  4.   tasks:
  5.   - name: Print All Facts
  6.     debug:
  7.       #var: ansible_facts.fqdn
  8.       msg: >
  9.         LONG HOSTNAME IS "{{ ansible_facts.fqdn }}".
  10.         SHORT HOSTNAME IS "{{ ansible_hostname }}".
  11.         DEFAULT IP ADDRESS IS "{{ ansible_facts.default_ipv4.address }}".
  12.         DEFAULT IP ADDRESS IS "{{ ansible_default_ipv4.address }}".
复制代码
  1. ---
  2. - name: Fact Dump
  3.   hosts: srvgroup
  4.   tasks:
  5.   - name: Print All Facts
  6.     debug:
  7.       #var: ansible_facts.fqdn
  8.       msg: >
  9.         LONG HOSTNAME IS "{{ ansible_facts.fqdn }}".
  10.         SHORT HOSTNAME IS "{{ ansible_hostname }}".
  11.         DEFAULT IP ADDRESS IS "{{ ansible_facts.default_ipv4.address }}".
  12.         DEFAULT IP ADDRESS IS "{{ ansible_default_ipv4.address }}".
  13.         DEFAULT IP ADDRESS IS "{{ ansible_facts.default_ipv4.address }}".
  14. ~                                                                          
复制代码

  1. ---
  2. - name: Fact Dump
  3.   hosts: srvgroup
  4.   gather_facts: no
  5.   tasks:
  6.   - name: Print All Facts
  7.     debug:
  8.       var: ansible_facts
复制代码
  1. ---
  2. - name: Fact Dump
  3.   hosts: srvgroup
  4.   gather_facts: no
  5.   tasks:
  6.   - name: Print All Facts
  7.     setup:
  8.     register: v_setup

  9.   - debug:
  10.       var: v_setup
复制代码
  1. ---
  2. - name: Local Var
  3.   hosts: srvgroup
  4.   gather_facts: yes
  5.   tasks:
  6.   - name: var
  7.     debug:
  8.       msg: >
  9.         The value to know "{{ ansible_facts['fqdn'] }}".
  10.         The value to know "{{ ansible_facts.fqdn }}".
  11.         The value to know "{{ ansible_fqdn }}".
  12.         is "{{ ansible_facts['ansible_local'] }}".
  13.         The ip addresses is "{{ ansible_all_ipv4_addresses }}".
复制代码
  1. ---
  2. - name: Local Var
  3.   hosts: srvgroup
  4.   gather_facts: yes
  5.   tasks:
  6.   - name: var
  7.     debug:
  8.       msg: |
  9.         The value to know "{{ ansible_facts['fqdn'] }}".
  10.         The value to know "{{ ansible_facts.fqdn }}".
  11.         The value to know "{{ ansible_fqdn }}".
  12.         is "{{ ansible_facts['ansible_local'] }}".
  13.         The ip addresses is "{{ ansible_all_ipv4_addresses }}".
  14.         The ip addresses is "{{ ansible_facts['all_ipv4_addresses'] }}".
复制代码
补充默认的事实,添加本地事实变量:
  1. ---
  2. - name: Local Facts
  3.   hosts: srvgroup
  4.   vars:
  5.     remote_dir: /etc/ansible/facts.d
  6.     facts_file: ./v3.d/local.fact
  7.   tasks:
  8.   - name: Create Remote Directory
  9.     file:
  10.       path: "{{ remote_dir }}"
  11.       recurse: yes
  12.       state: directory

  13.   - name: Copy Fact
  14.     copy:
  15.       src: "{{ facts_file }}"
  16.       dest: "{{ remote_dir }}"
复制代码
./v3.d/local.fact:
  1. [a]
  2. v1: 1
  3. v2: 2
复制代码
  1. ansible srvgroup -m setup  | grep -A 20 -B 20 v1
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-5-10 00:25 , Processed in 0.031007 second(s), 24 queries .

快速回复 返回顶部 返回列表