文章目录
- Ansible varialbes
- 1.用vars_file定义变量
- 2.在inventory主机清单中定义变量?
- 通过单个主机或主机组定义变量
- 通过外置传参定义变量? -e
- 变量注册
- facts变量
Ansible varialbes
1.什么是变量?
以一个固定的字符串,表示一个不固定的值 version: 1.12
2.定义变量?
- 在playbook中定义变量?
- vars 关键字
3.查看变量
#查看变量
[root@manage project]# ansible webservers -m setup -i hosts
#可以将变量重定向到1.txt中然后搜索
[root@manager project1]# cat vars_file.yml
file_naem: 123
[root@manager project1]# cat f2.yml
- hosts: webservers
vars:
- file_name: playbook_vars
tasks:
- name: Create New File
file:
path: /tmp/{{ file_name }}
state: touch
[root@manage project]# ansible-playbook f2.yml -i hosts
- vars_file 属于一种共享的方式
1.用vars_file定义变量
[root@manager project1]# cat vars_file.yml (名字随意起,但起一个和playbook相对应的)
web_packages: httpd
ftp_packages: vsftpd
[root@manager project1]# cat f2.yml
- hosts: webservers
vars:
- file_name: playbook_vars
#调用共享vars_file文件,只不过刚好文件名叫vars_file
vars_files: ./vars_file.yml
tasks:
- name: Create New File
file:
path: /tmp/{{ file_name }}
state: touch
- name: Installed Packages {{ web_packages }}
yum:
name: "{{ web_packages }}"
state: present
2.在inventory主机清单中定义变量?
- 1.清单文件中直接定义 hosts文件定义–
[webservers]
172.16.1.7
172.16.1.8
[webservers:vars]
file_name=hostsfile_group_vars
通过单个主机或主机组定义变量
创建host_vars group_vars 目录
[root@manager project1]# mkdir host_vars #单个主机
[root@manager project1]# mkdir group_vars #主机组
#1.单个主机定义和使用方式 (host_vars能分别对不同的主机定义变量)
[root@manager project1]# cat host_vars/172.16.1.7
host_vars_name: 172.16.1.7
[root@manager project1]# cat host_vars/172.16.1.8
host_vars_name: 172.16.1.8
[root@manager project1]# cat f4.yml
- hosts: webservers
tasks:
- name: Create New File
file:
path: /opt/{{ host_vars_name }}
state: touch
[root@manager project1]# ansible-playbook -i hosts f4.yml
针对主机组定义的方式
#给指定的webserver组设定变量.其他组主机无法使用该变量
[root@manager project1]# cat group_vars/webservers
group_host_vars: webservers
[root@manager project1]# cat f5.yml
- hosts: webservers
tasks:
- name: Create New File {{ group_host_vars }}
file:
path: /opt/{{ group_host_vars }}
state: touch
[root@manager project1]# ansible-playbook -i hosts f5.yml
### 3.针对主机组定义的方式 (给所有的主机和主机组设定变量)
[root@manager project1]# cat group_vars/all
group_host_vars: all
[root@manager project1]# cat f5.yml
- hosts: webservers
tasks:
- name: Create New File {{ group_host_vars }}
file:
path: /opt/{{ group_host_vars }}
state: touch
通过外置传参定义变量? -e
[root@manage project]# cat f6.yml
- hosts: webservers
tasks:
- name: Create New File {{ web_vars }}
file:
path: /tmp/{{ web_vars }}
state: touch
[root@manager project1]# ansible-playbook -i hosts f6.yml -e "web_vars=123"
3.变量冲突,优先级?
定义相同的变量不同的值,来测试变量的优先级。操作步骤如下 file_name:
1)在plabook中定义vars变量
2)在playbook中定义vars_files变量
3)在inventory主机定义变量
4)在inventory主机组定义变量
5)在host_vars中定义变量
6)在group_vars中定义变量 组 all组
7)通过执行命令传递变量
优先级测试:
外置传入参数优先级最高 ---> playbook ( vars_files(共享)--->vars(私有) )
---> host_vars --> group_vars/group_name ---> group_vars/all
变量注册
[root@manager project1]# cat 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 }}"
facts变量
#0.怎么查看facts变量(要注意它的目录包含级别)
[root@manage project]# ansible localhost -m setup -i hosts
例如:
#1.根据主机的cpu信息,生成不同的配置.
A: 1核心 work_process 1;
B: 2核心 work_process 2;
[root@manage project]# cat f10.yml
- hosts: webservers
tasks:
- name: Configure Nginx.conf
#template和copy的功能相同,但是template会解析变量
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#此处修改/etc/nginx/nginx.conf下
[root@manage project]# vim file/nginx.conf.j2
worker_processes {{ ansible_processor_vcpus }};
#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=""
#4.根据操作系统的区别安装软件(用when判断)
[root@manage project]# cat f13.yml
- hosts: webservers
tasks:
- name: Centos Installed Httpd Server
yum:
name: httpd
state: present
when: ( ansible_distribution == "CentOS" )
- name: Ubuntu Installed Httpd Server
yum:
name: httpd
state: present
when: ( ansible_distribution == "Ubuntu" )
[root@manage project]# ansible-playbook f13.yml -ihosts
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 }}"