[toc] ansible變數 定義變數的方式 命令行 playbook 主機清單 如何定義變數 1.在playbook中定義變數 2.使用vars_files定義變數 3.使用ansible內置變數 4.使用主機清單定義變數 調用變數 5.官方推薦方式 6.命令行定義變數 ansible變數優先順序 ...
目錄
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 }}