0%

Ansible变量

Ansible 变量

定义变量的方式

1.命令行

2.playbook

3.主机清单

如何定义变量

1.在playbook中定义变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#方法一:
- 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_file定义变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#定义阶段
[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内置变量

1
2
3
4
5
6
7
8
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.使用主机清单定义变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#定义阶段
[web_group]
zls_web01 ansible_ssh_host=172.16.1.7
zls_web02 ansible_ssh_host=172.16.1.8

[web_group: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.官方推荐方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#定义阶段
#创建主机组变量定义目录
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.命令行定义变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#调用阶段
[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变量优先级测试

1
2
3
4
5
6
7
8
9
- hosts: webs
vars:
dir: playbook_vars
vars_files: ./vars_file.yml
tasks:
- name: Create Dir
file:
path: /opt/{{ dir }}
state: directory
1
2
[root@m01 ~]# vim vars_file.yml
dir: vars_file
1
2
[root@m01 ~]# vim host_vars/zls_web01
dir: host_vars
1
2
[root@m01 ~]# vim group_vars/webs
dir: group_vars

测试阶段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#1.
[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层级定义变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#编辑变量文件
[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

lnmt:
framework:
web_package: nginx
db_package: mysql
java_package: tomcat

#编辑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'] }}"

#执行playbook
[root@m01 ~]# ansible-playbook test.yml

PLAY [web_group] *****************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [web01]
ok: [web02]

TASK [Install LAMP httpd] ********************************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]

TASK [Install LAMP mariadb-server] ***********************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]

TASK [Install LAMP php] **********************************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
web01 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1
2
3
4
5
6
7
- hosts: webs
tasks:
- name: mem
file:
path: /opt/{{ ansible_facts['fqdn'] }}
#path: /root/{{ ansible_fqdn }}
state: directory

关闭facts

1
2
3
4
[root@m01 ~]# vim facts.yml
- hosts: web_group
gather_facts: no #关闭信息采集
tasks: