fact简介

image.png
一般playbook会通过setup模块收集数据
image.png

fact变量引用

使用 ansible_eth0.ipv4.address 来引用被控端的ip地址

开启和关闭fact

  1. -hosts: test
  2. gather_facts: false #关闭自动收集facts,默认是开启的
  3. tasks:
  4. -name: test gather facts #用setup模块收集
  5. setup:

自定义fact

  1. [admin@ansible ansible]$ cat costom.fact
  2. [general]
  3. package = httpd
  4. service = httpd
  5. state = started
  6. [admin@ansible ansible]$ cat fact2.yml
  7. - hosts: node1
  8. tasks:
  9. - file:
  10. path: /etc/ansible/facts.d
  11. state: directory
  12. - name: copy fact file to remote hosts
  13. copy:
  14. src: costom.fact
  15. dest: /etc/ansible/facts.d #放到目标文件夹/etc/ansible/facts.d
  16. - name: get facts
  17. debug:
  18. msg: "{{ ansible_local.costom.general.package }}"

使用set_fact模块定义新的变量

  1. [admin@ansible ansible]$ vim fact3.yml
  2. - hosts: node1
  3. vars:
  4. version: "{{ ansible_os_family }}-{{ ansible_distribution_major_version }}"
  5. tasks:
  6. - name: set facts
  7. set_fact:
  8. osversion: "{{ ansible_os_family }}-{{ ansible_distribution_major_version }}"
  9. oscomment: "this is a rhel8 os"
  10. - name: debug set fact
  11. debug:
  12. msg: "{{ osversion }}"
  13. - debug:
  14. msg: "{{ oscomment }}"
  15. - debug:
  16. msg: "{{ version }}"
  1. - hosts: node1
  2. tasks:
  3. - debug:
  4. msg: "{{ osversion }}"
  5. - debug:
  6. msg: "{{ oscomment }}"

使用lookup生成变量

lookup可以从主控端获取相关信息赋值给一个变量
下面是一个ssh 免密的例子

  1. [admin@ansible ansible]$ cat lookup.yml
  2. - hosts: node1
  3. tasks:
  4. - name: create user3
  5. user:
  6. name: user3
  7. state: present
  8. - name: create .ssh directory
  9. file:
  10. path: /home/user3/.ssh
  11. owner: user3
  12. group: user3
  13. mode: "700"
  14. state: directory
  15. - name: copy pub key to user3
  16. copy:
  17. content: "{{ lookup('file','/home/admin/.ssh/id_rsa.pub') }}"
  18. dest: /home/user3/.ssh/authorized_keys
  19. - name: exec shell command
  20. debug:
  21. msg: "{{ lookup('pipe','hostname') }}"
  22. - name: get env vars
  23. debug:
  24. msg: "{{ lookup('env','PWD')}}"

ansible魔法变量

hostvars:

获取某台指定的主机的相关变量。如果有一台web服务器的配置文件中需要指定db服务器的ip地址,我们假定这台db服务器的hostname为db.example.com,ip地址绑定再eth0网卡上,我们通过如下方法在web服务器上调用db服务器的ip地址:

  1. {{hostvars['db.example.com'].ansible_eth0.ipv4.address }}

需要注意db.example.com不能使用ip地址来代替,只能使用主机名

inventory_hostname:

是Ansible所识别的当前正在运行task的主机的主机名:

  1. {{hostvars[inventory_hostname].ansible_eth0.ipv4.address }}

groups:

groups是inventory中所有主机组的列表,可用以枚举主机组中的所有主机

  1. {{groups}} 列出所有的主机组
  2. {{groups.test}}列出test组内的所有主机,可用于循环主机组内的主机
  3. {{ groups.web }} 获取主机清单中web组内的所有主机,值是一个列表,跟在哪个节点上运行无关
  4. {{group_names}} 在哪个主机上运行,就获取该主机所在的组

例子

  1. [admin@ansible ansible]$ cat magic.yml
  2. - hosts: all
  3. tasks:
  4. - name: "获取node1的ip地址"
  5. debug:
  6. msg: "{{ hostvars['node1'].ansible_ens160.ipv4.address }}"
  7. - name: "获取主机清单中的主机名"
  8. debug:
  9. msg: "{{ inventory_hostname }}"
  10. - name: "获取各自的ip地址"
  11. debug:
  12. msg: "{{ hostvars[inventory_hostname].ansible_ens160.ipv4.address }}"
  13. - name: "获取各自的ip地址"
  14. debug:
  15. msg: "{{ ansible_ens160.ipv4.address }}"
  16. - name: "获取主机清单中定义的组名"
  17. debug:
  18. msg: "{{ groups }}"
  19. - name: "获取web组的所有主机"
  20. debug:
  21. msg: "{{ groups.web }}"
  22. - name: "获取主机所在的组"
  23. debug:
  24. msg: "{{ group_names }}"
  25. - name: display some infomation
  26. debug:
  27. msg: "{{ inventory_hostname }} is in db"
  28. when: '"db" in group_names'