个人笔记Ansible:facts变量

一、概念

shell变量:
		定义: Host=oldxu.com
		使用:${Host}


Ansible变量:
		定义: Host=oldxu.com
		使用:{{ Host }}

变量:以一个固定的字符串去表示一个不固定的值

那么在Ansible中定义变量分为如下三种方式:
  1. 通过命令行进行变量定义
  2. 在play文件中进行定义变量
  3. 通过inventory在主机组或单个主机中设置变量
变量的优先级( 变量读取的顺序 )

二、

1.通过play文件中进行变量定义:

通过vars来进行定义变量

[root@manager ansible_variables]# cat var1.yml
- hosts: webservers
  vars: #定义变量关键字
    - web_packages: httpd
    - ftp_packages: vsftpd

  tasks:
    - name: Installed Rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
      yum:
        name:
          - "{{ web_packages }}"
          - "{{ ftp_packages }}"
        state: present

注意:vars关键字定义的变量,无法与其他的playbook进行共享。

通过vars_files来进行定义变量

[root@manager ansible_variables]# cat test.yml
web_packages: httpd-tools
ftp_packages: vsftpd


[root@manager ansible_variables]# cat var1.yml
- hosts: webservers
  vars_files: test.yml

  tasks:
    - name: Installed Rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
      yum:
        name:
          - "{{ web_packages }}"
          - "{{ ftp_packages }}"
        state: present

[root@manager ansible_variables]# cat var2.yml
- hosts: webservers
  vars_files: test.yml

  tasks:
    - name: Installed Rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
      yum:
        name:
          - "{{ web_packages }}"
          - "{{ ftp_packages }}"
        state: present
2.通过inventory在主机组或单个主机中设置变量
[root@manager ansible_variables]# mkdir host_vars
[root@manager ansible_variables]# mkdir group_vars

[root@manager ansible_variables]# cat group_vars/webservers
web_packages: wget
ftp_packages: tree

[root@manager ansible_variables]# cat var4.yml
- hosts: webservers
  tasks:
    - name: Install Rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
      yum:
        name:
          - "{{ web_packages }}"
          - "{{ ftp_packages }}"
        state: present


[root@manager ansible_variables]# cat group_vars/all
web_packages: nfs-utils
ftp_packages: rsync

[root@manager ansible_variables]# cat var5.yml
- hosts: db
  tasks:
    - name: Install Rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
      yum:
        name:
          - "{{ web_packages }}"
          - "{{ ftp_packages }}"
        state: present
3.通过命令行进行变量定义
[root@manager ansible_variables]# cat var6.yml
- hosts: webservers
  tasks:
    - name: Install Rpm Packages "{{ test_packages }}"
      yum:
        name:
          - "{{ test_packages }}"
        state: present

[root@manager ansible_variables]# ansible-playbook var6.yml -e "test_packages=sl"

变量的查找顺序:
设定同一个变量,不同的值,去测试,看谁优先被使用。

filename=
  1)在plabook中定义vars变量
  2)在playbook中定义vars_files变量
  3)在host_vars中定义变量
  4)在group_vars中定义变量
  5)通过执行命令传递变量

变量的查找优先级:
1.外置传参  -e
2.playbook
vars_files
vars
3.host_vars
4.group_vars/组名
4.group_vars/all
NFS服务端和NFS客户端,要求灵活。
[root@manager ansible_variables]# cat group_vars/all
share_dir: /data3

[root@manager ansible_variables]# cat var8.yml
- hosts: webservers
  tasks:

    - name: Installed NFS Server
      yum:
        name: nfs-utils

    - name: Configure NFS Server	#配置文件中使用 了share_dir #共享的目录/data3
      template:
        src: ./exports.j2
        dest: /etc/exports
      notify: Restart NFS Server

    - name: Create Share Directory	#创建共享的目录使用share_dir  /data3
      file:
        path: "{{ share_dir }}"
        state: directory
        owner: www
        group: www
        mode: 755

    - name: Started NFS Server
      systemd:
        name: nfs
        state: started
        enabled: yes

  handlers:
    - name: Restart NFS Server
      systemd:
        name: nfs
        state: restarted

- hosts: db
  tasks:
    - name: Client Mount NFS Server
      mount:
        src: 172.16.1.7:{{ share_dir }}
        path: /ansible_mount
        fstype: nfs
        state: mounted
register变量注册,将执行的结果存储至变量中,后期可以通过结果进行判断的操作。
[root@manager ansible_variables]# cat var9.yml
- hosts: webservers
  tasks:
    - name: Get Network Status
      shell: netstat -lntp
      register: System_Net
      #将shell命令的执行输出结果,存储至System_Net变量中

    - name: Print Variables
      debug:
        msg: "{{ System_Net.stdout_lines }}"

{{ System_Net }} 能看到变量的所有信息

{{ System_Net.stderr }} #能捕获到错误,如果没有就是空,如果有错误就会限制在终端窗口

三、facts变量实战

1.根据IP地址生成不同的Redis配置
web01  bind 172.16.1.7
web02  bind 172.16.1.8 
[root@manager ansible_variables]# cat redis.yml
- hosts: webservers
  tasks:

    - name: Installed Redis Server
      yum:
        name: redis
        state: present

    - name: Configure Redis Server
      template:
        src: ./redis.conf.j2
        dest: /etc/redis.conf
      notify: Restart Redis Server

    - name: Started Redis Server
      systemd:
        name: redis
        state: started
        enabled: yes

  handlers:
    - name: Restart Redis Server
      systemd:
        name: redis
        state: restarted

[root@manager ansible_variables]# cat redis.conf.j2
bind 127.0.0.1 {{ ansible_eth1.ipv4.address }}
2.根据CPU核心生成不同的Nginx配置
web01 1核心 1GB	  2
web02 2核心 2GB    4
[root@manager ansible_variables]# cat nginx.yml

- hosts: webservers
  tasks:
    - name: Configure  Nginx.conf
      template:
        src: ./nginx.conf.j2
        dest: /tmp/nginx.conf

[root@manager ansible_variables]# cat nginx.conf.j2
worker {{ ansible_processor_vcpus * 2 }};
3.根据主机内存生成不同的Memcached配置 ( 使用物理内存的一半 )
web01 1G	512
web02 2G	1024
web03 4G	2048
  1. 找到物理内存的变量
  2. 将变量除以2
[root@manager ansible_variables]# yum install memcached -y
[root@manager ansible_variables]# cp /etc/sysconfig/memcached ./memcached.j2
[root@manager ansible_variables]# cat memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb //2 }}"
OPTIONS=""

[root@manager ansible_variables]# cat memcached.yml
- hosts: webservers
  tasks:
    - name: Installed Memcached Server
      yum:
        name: memcached
        state: present

    - name: Configure Memcached Server
      template:
        src: memcached.j2
        dest: /etc/sysconfig/memcached
      notify: Restart Memcached Server

    - name: Started Memcached Server
      systemd:
        name: memcached
        state: started
        enabled: yes

  handlers:
    - name: Restart Memcached Server
      systemd:
        name: memcached
        state: restarted
4.根据主机名称生成不同的zabbix配置
[root@manager ansible_variables]# cat zabbix_agent.yml
- hosts: all
  tasks:
    - name: Installed ZabbixAgent
      yum:
        name: https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm
        state: present

    - name: Configure ZabbixAgent
      template:
        src: ./zabbix_agentd.conf.j2
        dest: /etc/zabbix/zabbix_agentd.conf
      notify: Restart ZabbixAgent

    - name: Started ZabbixAgent
      systemd:
        name: zabbix-agent
        state: started
        enabled: yes

  handlers:
    - name: Restart ZabbixAgent
      systemd:
        name: zabbix-agent
        state: restarted


[root@manager ansible_variables]# grep "^Hostname" zabbix_agentd.conf.j2
Hostname={{ ansible_hostname }}

四、facts优化

facts变量开启会影响playbook运行的效率? 但是关闭又会造成无法提取被控端的状态。 最佳的方案使用缓存来解决。

  • 没有使用缓存时的时间: 4台机器同时操作: 20台

      real	0m28.738s
      user	0m12.064s
      sys	0m3.725s
    
  • 增加redis缓存:

      real	0m6.345s
      user	0m3.021s
      sys	0m1.039s
    

1.安装一个redis 172.16.1.51 安装过了,就不要需要安装,直接使用就行了。
2.配置ansible配置文件,让其支持redis缓存:

[root@manager ansible_variables]# cat ansible.cfg
[defaults]
inventory = ./hosts

gathering = smart
fact_caching_timeout = 86400
fact_caching = redis
fact_caching_connection = 172.16.1.7:6379



yum install python-pip
pip install redis

你可能感兴趣的:(linux)