ansible系列(22)--ansible的Facts Variables



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"
    
posted @ 2021-08-18 17:22  向往自由的独行者  阅读(839)  评论(0编辑  收藏  举报