目录
- ansible变量
- 定义变量的方式
- 如何定义变量
- ansible变量优先级测试
- ansible层定义变量
- facts基本用法
- facts生成mysql配置文件
ansible变量
定义变量的方式
- 命令行
- playbook
- 主机清单
如何定义变量
1.在playbook中定义变量
#方法一:
- hosts: db02
vars:
pkg:
- MySQL-python
- mariadb-server
tasks:
- name:
yum:
name: "{{ pkg }}"
state: present
#方法二:
- hosts: db01
vars:
web_pkg: httpd
db_pkg:
- unzip
- vsftpd
php_pkg:
- php-fpm
- php
- php-mysql
2.使用vars_files定义变量
#定义阶段
[root@m01 project]# vim vars_file.yml
web_pkg: httpd
db_pkg: mariadb-server
php_pkg: php
#调用阶段
[root@m01 project]# vim httpd.yml
- hosts: web_group
vars_files: ./vars_file.yml
tasks:
- name: Install httpd
yum:
name: "{{ db_pkg }}"
state: present
3.使用ansible内置变量
vim mkdri.yml
- hosts: web_group
tasks:
- name: MKDIR
file:
path: /backup/{{ ansible_fqdn }}_{{ ansible_default_ipv4['address'] }}_{{ ansible_date_time['date'] }}
state: directory
recurse: yes
4.使用主机清单定义变量
#定义阶段
[webs]
zls_web01 ansible_ssh_host=172.16.1.7
zls_web02 ansible_ssh_host=172.16.1.8
[webs:vars]
web_pkg=nginx
[backup_group]
backup ansible_ssh_host=172.16.1.41
[db_group]
db01 ansible_ssh_host=172.16.1.51
db02 ansible_ssh_host=172.16.1.52
[nfs_group]
nfs ansible_ssh_host=172.16.1.31
[rsync_server:children]
web_group
backup_group
[rsync_server:vars]
web_pkg=httpd
[nfs_server:children]
web_group
nfs_group
调用变量
- hosts: zls_web01
tasks:
- name: Install web server
yum:
name: "{{ web_pkg }}"
state: present
5.官方推荐方式
#定义阶段
#创建主机组变量定义目录
mkdir group_vars
[root@m01 ~]# vim group_vars/webs
web_pkg: httpd
#定义阶段
[root@m01 ~]# cat group_vars/webs
web_pkg: httpd
#定义阶段
[root@m01 ~]# cat host_vars/zls_web01
web_pkg: nginx
[root@m01 ~]# vim web.yml
- hosts: webs
tasks:
- name: Install web server
yum:
name: "{{ web_pkg }}"
state: present
6.命令行定义变量
#调用阶段
[root@m01 ~]# !vim
vim web.yml
- hosts: webs
tasks:
- name: Install httpd Server
yum:
name:
- "{{ web_pkg }}"
- name: Install mariadb Server
yum:
name:
- "{{ db_pkg }}"
#定义阶段
[root@m01 ~]# ansible-playbook web.yml -e "web_pkg=vsftpd" -e "db_pkg=unzip"
ansible变量优先级测试
- hosts: webs
vars:
dir: playbook_vars
vars_files: ./vars_file.yml
tasks:
- name: Create Dir
file:
path: /opt/{{ dir }}
state: directory
[root@m01 ~]# vim vars_file.yml
dir: vars_file
[root@m01 ~]# vim host_vars/zls_web01
dir: host_vars
[root@m01 ~]# vim group_vars/webs
dir: group_vars
测试阶段
[root@m01 ~]# ansible-playbook test.yml -e "dir=command_vars"
[root@web01 ~]# ll /opt/
total 0
drwxr-xr-x 2 root root 6 Sep 19 16:48 command_vars
#2.
[root@m01 ~]# ansible-playbook test.yml
[root@web01 ~]# ll /opt/
total 0
drwxr-xr-x 2 root root 6 Sep 19 16:48 command_vars
drwxr-xr-x 2 root root 6 Sep 19 16:50 vars_file
#3.
[root@m01 ~]# vim test.yml
- hosts: webs
vars:
dir: playbook_vars
tasks:
- name: Create Dir
file:
path: /opt/{{ dir }}
state: directory
[root@m01 ~]# ansible-playbook test.yml
[root@web01 ~]# ll /opt/
total 0
drwxr-xr-x 2 root root 6 Sep 19 16:48 command_vars
drwxr-xr-x 2 root root 6 Sep 19 16:52 playbook_vars
drwxr-xr-x 2 root root 6 Sep 19 16:50 vars_file
#4.
- hosts: webs
tasks:
- name: Create Dir
file:
path: /opt/{{ dir }}
state: directory
[root@m01 ~]# ansible-playbook test.yml
==命令行 > vars_file > playbook vars > host_vars > group_vars==
ansible层定义变量
#编辑变量文件
[root@m01 ~]# vim vars_file.yml
lamp:
framework:
web_package: httpd
db_package: mariadb-server
php_package: php
lnmp:
framework:
web_package: nginx
db_package: mysql
php_package: php
#编辑playbook文件
[root@m01 ~]# vim test.yml
- hosts: web_group
vars_files: ./vars_file.yml
tasks:
- name: Install LAMP httpd
yum:
name: "{{ lamp.framework.web_package }}"
- name: Install LAMP mariadb-server
yum:
name: "{{ lamp.framework.db_package }}"
- name: Install LAMP php
yum:
name: "{{ lamp.framework.php_package }}"
#官方推荐写法
[root@m01 ~]# vim test.yml
- hosts: web_group
vars_files: ./vars_file.yml
tasks:
- name: Install LAMP httpd
yum:
name: "{{ lamp['framework']['web_package'] }}"
- name: Install LAMP mariadb-server
yum:
name: "{{ lamp['framework']['db_package'] }}"
- name: Install LAMP php
yum:
name: "{{ lamp['framework']['php_package'] }}"
facts基本用法
#编辑
[root@m01 ~]# vim facts.yml
- hosts: web_group
tasks:
- name: Get Host Info
debug:
msg: >
Hostname "{{ ansible_fqdn }}" and IP "{{ ansible_default_ipv4.address }}"
#执行
[root@m01 ~]# ansible-playbook facts.yml
[root@m01 ~]# ansible-playbook facts.yml
PLAY [web_group] *****************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]
TASK [Get Host Info] *************************************************************************************************************************************************************************************************************************
ok: [web01] => {
"msg": "Hostname \"web01\" and IP \"10.0.0.7\"\n"
}
ok: [web02] => {
"msg": "Hostname \"web02\" and IP \"10.0.0.8\"\n"
}
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
web01 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- hosts: webs
tasks:
- name: mem
file:
path: /opt/{{ ansible_facts['fqdn'] }}
#path: /root/{{ ansible_fqdn }}
state: directory
关闭facts
[root@m01 ~]# vim facts.yml
- hosts: web_group
gather_facts: no #关闭信息采集
tasks:
facts生成zabbix配置文件
- hosts: web_group
vars:
- zabbix_server: 172.16.1.71
tasks:
- name: copy zabbix agent conf
template:
src: ./zabbix_agentd.conf
dest: /tmp/zabbix_agentd.conf
facts生成mysql配置文件
- hosts: db_group
tasks:
- name: Install mysql server
yum:
name: mariadb-server
state: present
- name: copy mysql conf
template:
src: ./my.cnf
dest: /etc/my.cnf
[root@m01 ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr
datadir=/var/lib/mysql/
socket=/var/lib/mysql/mysql.sock
log_error=/var/log/mariadb/mariadb.log
innodb_buffer_pool_size={{ ansible_memtotal_mb * 0.8 }}