第五十三天 Ansible variables
1.什么是变量
以一个固定的字符串,表示一个不固定的值。我们称之变量。
2.定义变量
1.在playbook中定义变量
vars 关键字
[root@manager project1]# vim f2.yml
- hosts: webservers
vars:
- file_name: playbook_vars
tasks:
- name: Create New file
file:
path: /tmp/{{ file_name }}
state: touch
vars_file:它属于一种共享式的方式
1570755668515.png[root@manager project1]# vim f2.yml
- hosts: webservers
#引用变量文件./vars_file.yml
vars_files: ./vars_file.yml
tasks:
- name: Installed Packages {{ web_packages }}
yum:
name: "{{ web_packages }}"
state: present
[root@manager project1]# vim ./vars_file.yml
web_packages: httpd
ftp_packages: vsftpd
2.在inventory主机清单中定义变量
多重定义变量的方式优先级
1.在playbook中定义vars变量
2.在playbook中定义vars_file 变量
3.在inventory主机定义变量
4.在inventory主机组定义变量
5.在host_vars中定义变量
6.在group_vars中定义变量
7.通过命令传参的方式定义变量
命令传参的优先级最高
其次是playbook中vars_file
在其次playbook中vars
然后是inventory单个主机host_vars
再然后hosts主机清单中单个主机变量
然后是inventory主机组group_vars
再其次是group_vars/all
最后是hosts主机清单中的主机组变量
1.在清单文件中直接定义 hosts文件定义
[webservers]
172.16.1.7
172.16.1.8
[webservers:vars]
file_name=hostsfile_group_vars
2.创建host_vars group_vars 目录
host_vars 固定目录,用于存放主机变量
group_vars 固定目录,用于存放主机组变量
定义单个主机变量(能分辨对不同的主机定义变量)
[root@manager project1]# vim host_vars/172.16.1.7
host_vars_name: 172.16.1.7
[root@manager project1]# vim host_vars/172.16.1.8
host_vars_name: 172.16.1.8
[root@manager project1]# vim f4.yml
- hosts: webservers
tasks:
- name: Create New file
file:
path: /opt/{{ host_vars_name }}
state: touch
定义主机组变量(可以定义单个主机组设定变量,其他组无法使用)
[root@manager project1]# vim group_vars/webservers
group_host_vars: webservers
[root@manager project1]# vim f5.yml
- hosts: webservers
tasks:
- name: Create New File {{ group_host_vars }}
file:
path: /opt/{{ group_host_vars }}
state: touch
定义主机组变量(定义所有主机组变量,所有主机都可以使用)
[root@manager project1]# vim group_vars/all
group_host_vars: all
3.通过外置传参定义变量
[root@manager project1]# ansible-playbook -i hosts f6.yml -e "web_vars=123"
3.变量注册
register关键字可以将某个task任务结果存储至变量中,最后使用debug输出变量内容,可以用于后续排障
[root@manager project1]# vim f8.yml
- hosts: webservers
tasks:
# System_Status=$(netstat -lntp)
- name: Get Network Status
shell: netstat -lntp | grep "nginx"
register: System_Status
# echo "$System_Status"
- name: Debug output Variables
debug:
msg: "{{ System_Status.stdout_lines }}"
4.facts变量
Ansible facts实在被关了主机上通过ansible自动采集发现的变量。facts包含每台特定的主机信息。比如,被控端主机的主机名、IP地址、系统版本、CPU数量、内存状态、磁盘状态等
facts使用场景
1.通过facts变量检查被控端硬件cpu信息,从而生成不同的Nginx配置文件
2.通过facts变量检测被控端主机名称信息,从而生成不同的Zabbix配置文件
3.通过facts变量检查被控端内存状态信息,从而生成不同的memcached的配置文件
……………………
1.facts基本用法,比如获取被控端的主机名与IP地址,然后通过debug输出
[root@manager project1]# vim f9.yml
- hosts: webservers
tasks:
- name: Output variables ansible facts
debug:
msg: >
this default IPv4 address " {{ ansible_fqdn }} " is " {{ ansible_default_ipv4.address }} "
1.根据主机的cpu信息,生成不同的配置
[root@manager project1]# vim f10.yml
- hosts: webservers
tasks:
- name: Config Nginx.conf
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
[root@manager project1]# vim file/nginx.conf.j2
user www;
worker_processes {{ ansible_processor_vcpus }};
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
#2.根据主机名称设定不同配置文件
zabbix_agent
Server: ===> 指向172.16.1.61
Hostname: web01 web02
[root@manager project1]# cat ./file/zabbix_agent.conf.j2
Server={{ zabbix_server_ip }}
ServerActive={{ zabbix_server_ip }}
Hostname={{ ansible_hostname }}
[root@manager project1]# cat f11.yml
- hosts: webservers
vars:
- zabbix_server_ip: 172.16.1.61
tasks:
- name: Configure zabbix-agent.conf
template:
src: ./file/zabbix_agent.conf.j2
dest: /tmp/zabbix-agent.conf
#3.根据主机的内存生成不同的配置文件,memcached
[root@manager project1]# cat f12.yml
- hosts: webservers
tasks:
- name: Installed Memcached Server
yum:
name: memcached
state: present
- name: Configure Memcached Server
template:
src: ./file/memcached.j2
dest: /etc/sysconfig/memcached
notify: Restart Memcached Server
- name: System Memcached Server
systemd:
name: memcached
state: started
enabled: yes
handlers:
- name: Restart Memcached Server
systemd:
name: memcached
state: restarted
[root@manager project1]# cat file/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb //2 }}"
OPTIONS=""
1.根据cpu
2.根据内存
3.根据主机名
4.Redis配置文件 bind本地地址
5.操作系统不统一
变量可以进行运算 + - * //
#1.定义变量
playbook
vars 私有
vars_files 共享
inventory
host_vars
group_vars
group_vars/group_name
group_vars/all
外置传参
-e
#2.测试优先级
在不改变playbook变量的情况下,使用新的值测试.
#3.变量注册register
1.将任务执行的结果存储至特定的变量中
2.可以使用debug模块将变量进行打印输出
python: 字典
json 格式化数据
{
k1: v1
k2: v2
}
#4.facts
[root@manager project1]# cat f13.yml
- hosts: webservers
tasks:
- name: RANDOM
shell: echo "$RANDOM"
register: System_SJ
- name: Debug
debug:
msg: "web_{{ System_SJ.stdout }}"
#1.提取facts变量中的IP地址 mac地址 UUID 等等 只要唯一
ansible_default_ipv4.address
[root@manager project1]# cat f14.yml
- hosts: webservers
tasks:
- name: Debug
debug:
msg: "web_{{ ansible_default_ipv4.address }}"
1570768637521.png
Ansible 流程控制
8.判断语句
- 1.centos和ubuntu系统都需要安装httpd, 判断系统.
- 2.安装软件仓库,只有web组的安装webtatic其他的主机全部跳过.
- 3.TASK任务, TASK1任务执行成功,才会执行TASK2
#根据不同的系统,安装不同的服务
- hosts: webservers
tasks:
- name: CentOS Installed Httpd Server
yum:
name: httpd
state: present
when: ( ansible_distribution == "CentOS" )
- name: Ubuntu Installed Httpd Server
yum:
name: httpd2
state: present
when: ( ansible_distribution == "Ubuntu" )
[root@manager project1]# cat f16.yml
- hosts: all
tasks:
- name: Add Nginx Yum Repository
yum_repository:
name: nginx
description: Nginx Repository
baseurl: http://nginx.org/packages/centos/7/$basearch/
when: ( ansible_hostname is match ("web*"))
[root@manager project1]# cat f17.yml
- hosts: webservers
tasks:
- name: Check Httpd Server
command: systemctl is-active httpd
register: Check_Httpd
ignore_errors: yes
#判断Check_Httpd.rc是否等于0,如果为0则执行任务,否则不执行
- name: Restart Httpd Server
systemd:
name: httpd
state: restarted
when: ( Check_Httpd.rc == 0 )
9.循环语句
#一次启动多个服务
[root@manager project1]# cat f18.yml
- hosts: webservers
tasks:
- name: Systemd Nginx Status
systemd:
name: "{{ item }}" #调用的变量也不变,也是固定
state: started
#固定的语法格式
with_items:
- nginx
- php-fpm
#一次拷贝多个文件
[root@manager project1]# cat f19.yml
- hosts: webservers
tasks:
- name: Configure nginx.conf
copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
mode: '{{ item.mode }}'
with_items:
- { src: ./file/nginx.conf.j2, dest: /etc/nginx/nginx.conf, mode: '0644' }
- { src: ./file/kold.oldxu.com.conf.j2, dest: /etc/nginx/conf.d/kold.oldxu.com.conf, mode: '0600' }
#创建多个用户,一次创建多个? 3个用户 TASK
[root@manager project1]# cat f20.yml
- hosts: webservers
tasks:
- name: Create User
user:
name: "{{ item }}"
with_items:
- test1
- test2
- test3
- test4
#1.创建tt1 --> bin tt2 -->root tt3 --->adm 附加组
[root@manager project1]# cat f20.yml
- hosts: webservers
tasks:
- name: Create User
user:
name: "{{ item.name }}"
groups: "{{ item.groups }}"
with_items:
- { name: tt1, groups: bin }
- { name: tt2, groups: root }
- { name: tt3, groups: adm }
1.标准循环 --->居多
item
with_items:
- test
2.字典循环: --->居多
itme.name
with_items:
- { name: test }
3.变量循环
- hosts: webservers
tasks:
- name: ensure a list of packages installed
yum: name={{ packages }} state=present
vars:
packages:
- httpd
- httpd-tools
10.handlers
[root@manager project1]# cat f22.yml
- hosts: webservers
tasks:
- name: Installed Nginx and PHP Packages
yum:
name: nginx
state: present
- name: Configure nginx.conf
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#监控-->changed状态-->通知-->handlers--->name-->Restart Nginx Server
notify: Restart Nginx Server
#notify:
# - Restart Nginx Server
# - Restart php Server
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
#当nginx或php配置文件发生变更才会触发此操作
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
#3.handlers注意事项
1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。
2.只有task发生改变了才会通知handlers,没有改变则不会触发handlers.
3.不能使用handlers替代tasks、因为handlers是一个特殊的tasks。
变量->facts-->判断-->循环
- 1.安装Rsyncd服务 (循环)
- 2.安装Redis (bind 本地IP地址) facts
- 3.安装NFS (配置文件,创建目录,客户端挂载) 变量
网友评论