1 Ansible Facts Variables

Ansible facts 用来自动采集,”被控端主机“ 自身的状态信息。

比如: 主机名、IP地址、系统版本、CPU数量、内存状态、磁盘状态等等。

1.1 facts的获取方法

被控节点的facts信息需要使用ansiblesetup模块进行获取,使用filter参数可以过滤特定的Facts变量:

facts中的变量可以直接引用,无需获取。

  • 示例一:获取被控主机的信息:

    #获取主机名:
    [root@xuzhichao ansible_var]# ansible 192.168.20.23 -m setup -a 'filter=ansible_fqdn'
    192.168.20.23 | SUCCESS => {
    "ansible_facts": {
    "ansible_fqdn": "nginx03.lan",
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
    } #模糊匹配,获取主机名:
    [root@xuzhichao ansible_var]# ansible 192.168.20.23 -m setup -a 'filter=*name'
    192.168.20.23 | SUCCESS => {
    "ansible_facts": {
    "ansible_hostname": "nginx03",
    "ansible_nodename": "nginx03",
    "ansible_product_name": "VMware Virtual Platform",
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
    } #查看系统版本信息:
    [root@xuzhichao ansible_var]# ansible 192.168.20.23 -m setup -a 'filter=*version*'
    192.168.20.23 | SUCCESS => {
    "ansible_facts": {
    "ansible_bios_version": "6.00",
    "ansible_distribution_major_version": "7", <==centos主版本
    "ansible_distribution_version": "7.8", <==centos版本
    "ansible_kernel_version": "#1 SMP Tue Mar 31 23:36:51 UTC 2020",
    "ansible_product_version": "None",
    "ansible_python_version": "2.7.5",
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
    }
  • 示例二:在playbook中直接调用facts变量:

    [root@xuzhichao playbook]# cat fact.yml
    - hosts: NginxWebs
    remote_user: root tasks:
    - name: Output ansible variables facts
    debug:
    msg:
    - this default IPv4 address "{{ ansible_fqdn}}" is "{{ ansible_default_ipv4.address }}"

    运行结果如下:

    [root@xuzhichao playbook]# ansible-playbook fact.yml 
    
    PLAY [NginxWebs] *********************************************************************************************************************************************
    
    TASK [Gathering Facts] ***************************************************************************************************************************************
    ok: [192.168.20.23]
    ok: [192.168.20.22] TASK [Output ansible variables facts] ************************************************************************************************************************
    ok: [192.168.20.22] => {
    "msg": [
    "this default IPv4 address \"nginx02.lan\" is \"192.168.2.149\""
    ]
    }
    ok: [192.168.20.23] => {
    "msg": [
    "this default IPv4 address \"nginx03.lan\" is \"192.168.2.158\""
    ]
    }

1.2 根据主机IP地址生成Redis配置文件

redis配置文件中,需要绑定主机的IP地址,可以通过fact变量获取主机地址,然后在redis配置文件中调用fact变量。

  • 编写安装redisplaybook文件:

    [root@xuzhichao playbook]# cat install_redis.yml
    - hosts: redissers
    remote_user: root tasks:
    - name: Install Redis Server
    yum:
    name: redis
    state: present - name: Configure Redis Server
    template:
    src: conf/redis.conf.j2
    dest: /etc/redis.conf
    owner: "redis"
    group: "root"
    mode: "0644"
    notify: Restart Redis Server - name: Start Redis Server
    service:
    name: redis
    state: started handlers:
    - name: Restart Redis Server
    service:
    name: redis
    state: restarted
  • 修改redis的配置模板文件:

    [root@xuzhichao playbook]# vim conf/redis.conf.j2
    ......
    bind 127.0.0.1 {{ ansible_eth1.ipv4.address }}
    ......
  • 运行playbook文件:

    [root@xuzhichao playbook]# ansible-playbook install_redis.yml

1.3 根据主机CPU核数生成Nginx配置

nginx的配置文件中启动的nginx进程数需要根据主机的CPU核心数进行设置,此时可以使用fact变量获取nginx主机的CPU核心数,然后在nginx配置文件中定义调用fact变量。

fact变量中关于CPU核心的值为:

  • ansible_processor_cores: 4 :每颗物理CPU的核心数
  • ansible_processor_count: 2 :CPU颗数 (有几个CPU )
  • ansible_processor_vcpus: 8 :CPU总核心数

配置示例如下:

  • 编写安装nginxplaybook文件:

    [root@xuzhichao playbook]# cat install_nginx.yml
    - hosts: NginxWebs
    remote_user: root tasks:
    - name: Install Nginx Server
    yum:
    name: nginx
    state: present - name: Configure Nginx Server
    template:
    src: conf/nginx.conf.j2
    dest: /etc/nginx/nginx.conf
    owner: "root"
    group: "root"
    mode: "0644"
    notify: Restart Nginx Server - name: Start Nginx Server
    service:
    name: nginx
    state: started handlers:
    - name: Restart Nginx Server
    service:
    name: nginx
    state: restarted
  • 修改redis的配置模板文件:

    [root@xuzhichao playbook]# vim conf/nginx.conf.j2
    ......
    worker_processes {{ ansible_processor_vcpus * 2 }}; <==nginx的进程数为CPU核心数*2,(支持+-*/运算);
    ......
  • 运行playbook文件:

    [root@xuzhichao playbook]# ansible-playbook install_nginx.yml

1.4 根据主机内存生成Memcached配置

Memcached服务需要根据主机的内存来设置启用缓存的大小,此时可以使用fact变量获取memcached主机的内存,然后在memcached配置文件中定义调用fact变量。

  • ansible_memtotal_mb:主机的内存大小

配置示例如下:

  • 编写安装memcachedplaybook文件:

    [root@xuzhichao playbook]# cat install_memcached.yml
    - hosts: memcachedsers
    remote_user: root tasks:
    - name: Install Memcached Server
    yum:
    name: nmemcached
    state: present - name: Configure Memcached Server
    template:
    src: conf//memcached.j2
    dest: /etc/sysconfig/memcached
    owner: "root"
    group: "root"
    mode: "0644"
    notify: Restart Memcached Server - name: Start Memcached Server
    service:
    name: memcached
    state: started handlers:
    - name: Restart Memcached Server
    service:
    name: memcached
    state: restarted
  • 修改memcached的配置模板文件:

    [root@xuzhichao playbook]# cat memcached.j2
    PORT="11211"
    USER="memcached"
    MAXCONN="1024"
    #根据内存状态生成不同的配置(支持+-*/运算)
    CACHESIZE="{{ ansible_memtotal_mb //2 }}"
    OPTIONS=""

1.5 facts变量优化方案

虽然facts变量非常有用,但是使用了facts变量后会大大增加ansible运行的时长,因为每次运行都需要去获取所有被控主机的facts变量,可以使用以下两种方法增加ansible的执行速度。

注意:在被控主机较多时才会明显感受到ansible执行速度的提升。

1.5.1 方式1-关闭facts采集加速执行

  • 编写 TASK 任务 sleep10 秒,针对 15 台机器同时执行,需要消耗的时间大概是1m54.980s

    [root@m01 ~]# cat ansible_facts.yml
    - hosts: all
    tasks:
    - name: sleep 10
    command: sleep 10
  • 使用 gather_facts: no 关闭 facts 信息采集,发现仅花费了 0m38.164s ,整个速度提升了3倍,如果调整 forks 操作的主机的数量,也可以得到非常大的提升;

    [root@m01 ~]# cat ansible_facts.yml
    - hosts: all
    gather_facts: no <==关闭facts采集功能
    tasks:
    - name: sleep 10
    command: sleep 10

1.5.2 方式2-使用Redis缓存facts加速

  • 当我们使用 gather_facts: no 关闭 facts ,确实能加速 Ansible 执行,但是有时候又需要使用 facts 中的内容,还希望执行的速度快一点,这时候可以设置facts 的缓存;

    [root@xuzhichao playbook]# yum install python2-redis
    [root@xuzhichao playbook]# yum install python2-pip
    [root@xuzhichao playbook]# pip install --upgrade pip
    [root@xuzhichao playbook]# pip install redis
    [root@xuzhichao playbook]# systemctl start redis [root@xuzhichao playbook]# cat /etc/ansible/ansible.cfg
    [defaults]
    # smart 表示默认收集 facts,但 facts 已有的情况下不会收集,即使用缓存 facts
    # implicit 表示默认收集 facts,要禁止收集,必须使用 gather_facts: False;
    # explicit 则表示默认不收集,要显式收集,必须使用 gather_facts: Ture。
    gathering = smart #在使用 facts 缓存时设置为smart
    fact_caching_timeout = 86400
    fact_caching = redis
    fact_caching_connection = 192.168.20.23:6379 # 若 redis 设置了密码
    # fact_caching_connection = localhost:6379:0:admin
  • 测试缓存:

    #发现已经不再收集facts信息了
    [root@xuzhichao playbook]# ansible-playbook install_redis.yml PLAY [NginxWebs] ********************************************************************************************************************************************** TASK [Install Redis Server] ***********************************************************************************************************************************
    changed: [192.168.20.23]
    changed: [192.168.20.22] TASK [Configure Redis Server] *********************************************************************************************************************************
    changed: [192.168.20.23]
    changed: [192.168.20.22] TASK [Start Redis Server] *************************************************************************************************************************************
    changed: [192.168.20.23]
    changed: [192.168.20.22] RUNNING HANDLER [Restart Redis Server] ************************************************************************************************************************
    changed: [192.168.20.23]
    changed: [192.168.20.22] PLAY RECAP ****************************************************************************************************************************************************
    192.168.20.22 : ok=4 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    192.168.20.23 : ok=4 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  • redis上也可以看到缓存信息:

    [root@nginx03 ~]# redis-cli
    127.0.0.1:6379> keys *
    1) "ansible_cache_keys"
    2) "ansible_facts192.168.20.23"
    3) "ansible_facts192.168.20.22"

ansible系列(22)--ansible的Facts Variables的更多相关文章

  1. Ansible 小手册系列 十二(Facts)

    Facts 是用来采集目标系统信息的,具体是用setup模块来采集得. 使用setup模块来获取目标系统信息 ansible hostname -m setup 仅显示与ansible相关的内存信息 ...

  2. Ansible系列(二):选项和常用模块

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  3. Ansible系列(六):各种变量定义方式和变量引用

    本文目录:1.1 ansible facts1.2 变量引用json数据的方式 1.2.1 引用json字典数据的方式 1.2.2 引用json数组数据的方式 1.2.3 引用facts数据1.3 设 ...

  4. Ansible系列(七):执行过程分析、异步模式和速度优化

    本文目录:1.1 ansible执行过程分析1.2 ansible并发和异步1.3 ansible的-t选项妙用1.4 优化ansible速度 1.4.1 设置ansible开启ssh长连接 1.4. ...

  5. Ansible系列(五):各种变量定义方式和变量引用

    Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.1 ansible facts facts组件是用来收集被管理节点信息的 ...

  6. Ansible系列(一):基本配置和使用

    本文目录:1.1 安装Ansible1.2 配置Ansible 1.2.1 环境配置 1.2.2 SSH互信配置 1.2.3 简单测试1.3 inventory Ansible是一种批量.自动部署工具 ...

  7. Ansible系列(五):playbook应用和roles自动化批量安装示例

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  8. Ansible系列(六):循环和条件判断

    本文目录:1. 循环 1.1 with_items迭代列表 1.2 with_dict迭代字典项 1.3 with_fileglob迭代文件 1.4 with_lines迭代行 1.5 with_ne ...

  9. Ansible系列(四):playbook应用和roles自动化批量安装示例

    Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html playbook是ansible实现批量自动化最重要的手段.在其中可以使用变 ...

  10. Ansible系列(三):YAML语法和playbook写法

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

随机推荐

  1. archlinux xfce禁用Alt+F打开终端文件菜单

    参照:https://superuser.com/questions/456024/how-to-disable-alt-f-shortcut-binding-from-file-menu-acces ...

  2. Web、Android等程序开发中src引入外部文件和资源的方法总结

    方法一:使用相对于当前文件(源文件)的相对路径 使用 ../ 对于这个例子来说 ../ 把路径带到了项目根目录的下一级目录 1 <script src="../static/js/wo ...

  3. #虚树,树形dp#洛谷 3233 [HNOI2014]世界树

    题目 分析 考虑建一棵虚树,倍增找到虚树上相邻两个点的中间点统计答案 记录每个虚树点最近的距离以及编号最小的点,主要是细节问题 代码 #include <cstdio> #include ...

  4. #树状数组、dp#JZOJ 5361 捕老鼠

    题目 农夫约的农庄里有\(N\)个仓库,排成了一排,编号为\(1-N\). 假设猫在第\(i\)个仓库点燃艾条,烟雾就会充满该仓库,并向左右扩散\(Ai\)的距离,接着所有\(|i-j|<=Ai ...

  5. 使用OHOS SDK构建assimp

    参照OHOS IDE和SDK的安装方法配置好开发环境. 从github下载源码. 执行如下命令: git clone https://github.com/assimp/assimp.git 进入源码 ...

  6. Push failed idea将项目发布到gitHub失败

    此时需要点击VCS --> inport into version..-->create git ...重新生成仓库

  7. Kubernetes Pod配置:从基础到高级实战技巧

    本文深入探讨了Kubernetes Pod配置的实战技巧和常见易错点. 关注[TechLeadCloud],分享互联网架构.云服务技术的全维度知识.作者拥有10+年互联网服务架构.AI产品研发经验.团 ...

  8. stack-c++

    #include <iostream> using namespace std; template <typename T> class Stack{ private: int ...

  9. Linux(CentOS7.2)安装cnpm

    1.安装cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 2.如下安装成功 /root/node-v10.16.3 ...

  10. EDA(Exploratory Data Analysis)数据探索性分析

    EDA目的:通过了解数据集的分布情况,数据之间的关系,来帮我们更好的后期进行特征工程和建立模型. 本文主要是一个根据coco数据集格式的json文件,来分析数据集中图片尺寸,宽高比,bbox尺寸,宽高 ...