1.变量
变量:以一个固定的字符串表示一个不固定的值。
ansible中定义变量的三种方式:
(1) 通过命令行进行变量定义
(2) 在play文件中进行定义变量
(3) 通过inventory在主机组或单个主机中设置变量
变量的查找优先级:
(1).外置传参 -e
(2).playbook
vars_files
vars
(3).host_vars
(4).group_vars/组名
(4).group_vars/all
1.1通过vars来进行定义变量
mkdir ansible_vars
cd ansible_vars
vim ansible.cfg
-------------------------ansible.cfg---------------------------------
[defaults]
inventory = ./hosts
------------------------ansible.cfg结束-------------------------------
vim hosts
------------------------hosts----------------------------------------
[webservers]
172.16.1.7
172.16.1.8
------------------------hosts结束-------------------------------------
vim var1.yml
------------------------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
--------------------------var1.yml结束-----------------------------------
ansible-playbook var1.yml
1.2通过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
[root@manager ansible_variables]#ansible-playbook var1.yml
[root@manager ansible_variables]#ansible-playbook var2.yml
1.3通过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]#ansible-playbook var4.yml
[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
[root@manager ansible_variables]#ansible-playbook var5.yml
1.4通过命令行进行变量定义
[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"
2.ansible配置nfs
[root@manager ansible_variables]# cat exports.j2
{{ share_dir }} 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[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
3.register变量注册
将执行的结果存储至变量中,后期可以通过结果进行判断的操作。
#查看主机的所有信息
[root@manager ansible_variables]#ansible localhost - m setup
[root@manager ansible_variables]#
[root@manager ansible_variables]# cat var9.yml
- hosts: webservers
tasks:
- name: Get Network Status
#shell: netstat -lntp
shell: ss -lntp
register: System_Net
#将shell命令的执行输出结果,存储至System_Net变量中
- name: Print Variables
debug:
msg: "{{ System_Net.stdout_lines }}"
#{{ System_Net }} 能看到变量的所有信息
#{{ System_Net.stderr }} 能捕获到错误,如果没有就是空,如果有错误就会限制在终端窗口
4.实例
4.1根据IP地址生成不同的Redis配置
[root@manager ansible_variables]# cat redis.conf.j2
bind 127.0.0.1 {{ ansible_eth1.ipv4.address }}
[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]# ansible-playbook redis.yml
4.2根据CPU核心生成不同的Nginx配置
[root@manager ansible_variables]# cat nginx.conf.j2
worker {{ ansible_processor_vcpus * 2 }};
[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]# ansible-playbook nginx.yml
4.3根据主机内存生成不同的Memcached配置(使用物理内存的一半)
[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
[root@manager ansible_variables]# ansible-playbook memcached.yml
4.4根据主机名称生成不同的zabbix配置
[root@manager ansible_variables]# grep "^Hostname" zabbix_agentd.conf.j2
Hostname={{ ansible_hostname }}
[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]# ansible-playbook zabbix_agent.yml
5.facts优化
但是关闭又会造成无法提取被控端的状态。 最佳的方案使用缓存来解决。(redis)
5.1在172.16.1.51安装一个redis
5.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
[root@manager ansible_variables]# yum install python-pip
[root@manager ansible_variables]# pip install redis