目录
变量提供了便捷的方式来管理Ansible playbook的每一个项目中的动态值,比如nginx-1.6.3这个软件包的版本,在其它地方或许会反复使用,那么如果将此值设置为变量,然后再在其他的playbook中调用,会方便许多。如此一来还方便维护,减少维护的成本。
命令行定义变量
在play文件定义变量
在主机清单里定义变量
命令行 > playbook文件 > Inventory文件
playbook变量可以通过多种方式进行定义,最简单的方式就是在playbook的开头通过vars进行定义
# 1.playbook中全局定义
- hosts: web_group
# 定义变量:pkg=nginx,pkg2=tree
vars:
- pkg: nginx
- pkg2: tree
tasks:
- name: Create File
file:
# 表示创建一个叫Nginx的文件
path: /root/{{ pkg }}-------------使用变量时,变量旁边有字符,就不用双引号,双括号就行
state: touch
- name: Install {{ pkg2 }}
yum:
# 表示安装tree命令
name: "{{ pkg2 }}"----------------使用变量时,旁边没有内容,变量外面就需要双引号
state: presen
# 2.playbook中局部定义
- hosts: web_group
tasks:
- name: Create File
file:
path: /root/{{ pkg }}
state: touch
- name: Install {{ pkg2 }}
yum:
name: "{{ pkg2 }}"
state: presen
vars: --------------------与单个模块同级,其他模块将无法识别到变量
- pkg: nginx
- pkg2: tree
刚才我们学到在playbook中使用vars定义变量,有一个缺陷,就是其他的playbook无法使用该变量。所以我们可以 采取第二种定义变量的方式,在vars_file中定义变量
# 编辑一个变量文件,也必须是yml结尾,在里面定义变量
[root@m01 ansible]$ vim bianliang.yml
pkg: httpd
pkg2: tree
pkg3: mariadb-server
# 在playbook中,利用vars_files调用配置好的变量文件,然后就可以使用文件中的变量
[root@m01 ansible]$ vim test_vars.yml
- hosts: web_group
vars_files: /root/ansible/bianliang.yml
tasks:
- name: Create File
file:
path: /root/{{ pkg2 }}
state: touch
# 一个变量设置多个值
pkg:
- httpd
- tree
- mariadb-server
# 调用pkg去安装时,会将httpd,tree,mariadb-server全部安装
系统内置变量有哪些
## 系统内置变量
ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_fqdn / ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。
调用系统内置变量
- hosts: web_group
tasks:
- name: Create File
file:
# 在web端创建/root下以主机名_ip信息(ansible_default_ipv4.address表示:取ipv4下面的address,可以直接取出ip信息)_总内存大小,命名的文件
path: /root/{{ ansible_fqdn }}_{{ ansible_default_ipv4.address }}_{{ ansible_memtotal_mb }}
state: touch
不调用系统内置变量
facts:playbook执行之前会先调取系统内置变量
- hosts: web_group
# 关闭facts缓存:不加载内置变量,默认开启,只能影响内置变量,不影响手动设置的变量
gather_facts: no
tasks:
- name: Create File
file:
path: /root/{{ ansible_fqdn }}_{{ ansible_default_ipv4.address }}_{{ ansible_memtotal_mb }}
state: touch
注意:在Inventory中定义变量,主机的变量要高于主机组的变量,所以该方法不推荐使用,容易将环境弄乱。
# 在主机清单中定义
[root@m01 ~]$ vim /etc/ansible/hosts
## 在指定的主机组中定义变量只有当前主机组能识别
[web_group:vars]
pkg=httpd
pkg2=tree
1.命令行
2.vars_file
3.vars
4.host_vars:单个主机
5.group_vars:主机组
6.主机清单中的单个主机
7.主机清单中的主机组
之前的几种变量定义都不是很好用,比较好用的是在Ansible项目目录下创建两个变量目录:
host_vars
group_vars
切记,目录名字一定要一致,不能做任何修改,并且目录必须和yml文件同级
# 创建两个目录
mkdir host_vars
mkdir group_vars
# 主机定义变量:在host_vars主机变量目录下创建的变量文件,必须以主机名命名,代表给指定主机定义的变量
vim ansible/host_vars/web01
filename=inventory_web01
vim ansible/host_vars/web02
filename=inventory_web02
vim ansible/host_vars/web03
filename=inventory_web03
# 主机组定义变量:在group_vars主机组变量目录下创建的变量文件,必须以主机组命名,代表给指定主机组定义的变量
vim ansible/group_vars/web_group
filename=web_group
默认情况下,ansible的result并不会显示出来,所以,我们可以把这些返回值'存储'到变量中,这样我们就能通过'调用'对应的变量名,从而获取到这些result,这种将模块的返回值,写入到变量中的方法被称为变量注册
# 利用shell模块查看web端根目录下的信息
[root@m01 ~/ansible]$ vim test.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ls -l /"
# 不会返回任何结果
[root@m01 ~/ansible]$ ansible-playbook test.yml
PLAY [web_group] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [web02]
ok: [web01]
TASK [Test Register Vars] ******************************************************
changed: [web01]
changed: [web02]
PLAY RECAP *********************************************************************
web01 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
如上执行结果可见,当我们使用shell模块执行ls -l /时,ansible给我们返回的只有changed我们无法看到执行之后的 结果,所以此时我们需要使用到变量注册
# 编辑playbook
[root@m01 ~/ansible]$ vim test.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ls -l /"
# 表示将shell模块执行的结果,利用register赋值给了list_dir(list_dir可以随便写)
register: list_dir
- name: Return Result
# debug模块
debug:
# 利用msg动作调用上面的赋值,并打印
msg: "{{ list_dir }}"
# 再次查看执行结果
[root@m01 ~/ansible]$ ansible-playbook test.yml
ok: [web01] => {
"stdout_lines": [
"total 20",
"lrwxrwxrwx. 1 root root 7 Jul 5 19:02 bin -> usr/bin",
"dr-xr-xr-x. 5 root root 4096 Jul 5 19:05 boot",
"drwxr-xr-x 3 www www 23 Aug 14 17:21 code",
"drwxr-xr-x 19 root root 3200 Jul 8 23:06 dev",
"drwxr-xr-x. 84 root root 8192 Aug 14 17:21 etc",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 home",
"lrwxrwxrwx. 1 root root 7 Jul 5 19:02 lib -> usr/lib",
"lrwxrwxrwx. 1 root root 9 Jul 5 19:02 lib64 -> usr/lib64",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 media",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 opt",
"dr-xr-xr-x 121 root root 0 Jul 8 23:06 proc",
"dr-xr-x---. 4 root root 179 Aug 14 17:19 root",
"drwxr-xr-x 25 root root 740 Aug 14 17:21 run",
"lrwxrwxrwx. 1 root root 8 Jul 5 19:02 sbin -> usr/sbin",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 srv",
"dr-xr-xr-x 13 root root 0 Aug 15 15:31 sys",
"drwxrwxrwt. 20 root root 4096 Aug 15 15:35 tmp",
"drwxr-xr-x. 13 root root 155 Jul 5 19:02 usr",
"drwxr-xr-x. 20 root root 278 Aug 14 17:21 var"
]
}
ok: [web02] => {
"stdout_lines": [
"total 20",
"lrwxrwxrwx. 1 root root 7 Jul 5 19:02 bin -> usr/bin",
"dr-xr-xr-x. 5 root root 4096 Jul 5 19:05 boot",
"drwxr-xr-x 3 www www 23 Aug 14 17:21 code",
"drwxr-xr-x 19 root root 3200 Jul 8 23:11 dev",
"drwxr-xr-x. 84 root root 8192 Aug 14 17:21 etc",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 home",
"lrwxrwxrwx. 1 root root 7 Jul 5 19:02 lib -> usr/lib",
"lrwxrwxrwx. 1 root root 9 Jul 5 19:02 lib64 -> usr/lib64",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 media",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 opt",
"dr-xr-xr-x 120 root root 0 Jul 8 23:11 proc",
"dr-xr-x---. 4 root root 179 Aug 14 17:19 root",
"drwxr-xr-x 25 root root 740 Aug 14 17:21 run",
"lrwxrwxrwx. 1 root root 8 Jul 5 19:02 sbin -> usr/sbin",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 srv",
"dr-xr-xr-x 13 root root 0 Aug 15 15:31 sys",
"drwxrwxrwt. 20 root root 4096 Aug 15 15:35 tmp",
"drwxr-xr-x. 13 root root 155 Jul 5 19:02 usr",
"drwxr-xr-x. 20 root root 278 Aug 14 17:21 var"
]
}
# 只输出想要查看的内容
[root@m01 ~/ansible]$ vim test.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ls -l /"
register: list_dir
- name: Return Result
debug:
msg: "{{ list_dir.stdout_lines }}" ----------指定整个结果中的stdout_lines模块
# 查看结果
[root@m01 ~/ansible]$ ansible-playbook test.yml
ok: [web01] => {
"msg": [
"total 20",
"lrwxrwxrwx. 1 root root 7 Jul 5 19:02 bin -> usr/bin",
"dr-xr-xr-x. 5 root root 4096 Jul 5 19:05 boot",
"drwxr-xr-x 3 www www 23 Aug 14 17:21 code",
"drwxr-xr-x 19 root root 3200 Jul 8 23:06 dev",
"drwxr-xr-x. 84 root root 8192 Aug 14 17:21 etc",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 home",
"lrwxrwxrwx. 1 root root 7 Jul 5 19:02 lib -> usr/lib",
"lrwxrwxrwx. 1 root root 9 Jul 5 19:02 lib64 -> usr/lib64",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 media",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 opt",
"dr-xr-xr-x 122 root root 0 Jul 8 23:06 proc",
"dr-xr-x---. 4 root root 179 Aug 14 17:19 root",
"drwxr-xr-x 25 root root 740 Aug 14 17:21 run",
"lrwxrwxrwx. 1 root root 8 Jul 5 19:02 sbin -> usr/sbin",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 srv",
"dr-xr-xr-x 13 root root 0 Aug 15 15:31 sys",
"drwxrwxrwt. 20 root root 4096 Aug 15 15:37 tmp",
"drwxr-xr-x. 13 root root 155 Jul 5 19:02 usr",
"drwxr-xr-x. 20 root root 278 Aug 14 17:21 var"
]
}
ok: [web02] => {
"msg": [
"total 20",
"lrwxrwxrwx. 1 root root 7 Jul 5 19:02 bin -> usr/bin",
"dr-xr-xr-x. 5 root root 4096 Jul 5 19:05 boot",
"drwxr-xr-x 3 www www 23 Aug 14 17:21 code",
"drwxr-xr-x 19 root root 3200 Jul 8 23:11 dev",
"drwxr-xr-x. 84 root root 8192 Aug 14 17:21 etc",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 home",
"lrwxrwxrwx. 1 root root 7 Jul 5 19:02 lib -> usr/lib",
"lrwxrwxrwx. 1 root root 9 Jul 5 19:02 lib64 -> usr/lib64",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 media",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 mnt",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 opt",
"dr-xr-xr-x 120 root root 0 Jul 8 23:11 proc",
"dr-xr-x---. 4 root root 179 Aug 14 17:19 root",
"drwxr-xr-x 25 root root 740 Aug 14 17:21 run",
"lrwxrwxrwx. 1 root root 8 Jul 5 19:02 sbin -> usr/sbin",
"drwxr-xr-x. 2 root root 6 Apr 11 2018 srv",
"dr-xr-xr-x 13 root root 0 Aug 15 15:31 sys",
"drwxrwxrwt. 20 root root 4096 Aug 15 15:37 tmp",
"drwxr-xr-x. 13 root root 155 Jul 5 19:02 usr",
"drwxr-xr-x. 20 root root 278 Aug 14 17:21 var"
]
}
msg: #调试输出的消息
var: #将某个任务执行的输出作为变量传递给debug模块,debug会直接将其打印输出
verbosity: #debug的级别(默认是0级,全部显示)
[root@m01 ~/ansible]$ vim test.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ifconfig"
register: suibian
- name: Return Result
debug:
msg: "{{ suibian.stdout_lines }}"
# 查看结果
[root@m01 ~/ansible]$ ansible-playbook test.yml
PLAY [web_group] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [web02]
ok: [web01]
TASK [Test Register Vars] ******************************************************
changed: [web02]
changed: [web01]
TASK [Return Result] ***********************************************************
ok: [web01] => {
"msg": [
"eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500",
" inet 10.0.0.7 netmask 255.255.255.0 broadcast 10.0.0.255",
" inet6 fe80::20c:29ff:fee9:c96a prefixlen 64 scopeid 0x20<link>",
" ether 00:0c:29:e9:c9:6a txqueuelen 1000 (Ethernet)",
" RX packets 25022 bytes 33152427 (31.6 MiB)",
" RX errors 0 dropped 0 overruns 0 frame 0",
" TX packets 7785 bytes 1701680 (1.6 MiB)",
" TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0",
"",
"eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500",
" inet 172.16.1.7 netmask 255.255.255.0 broadcast 172.16.1.255",
" inet6 fe80::20c:29ff:fee9:c974 prefixlen 64 scopeid 0x20<link>",
" ether 00:0c:29:e9:c9:74 txqueuelen 1000 (Ethernet)",
" RX packets 32809 bytes 43612898 (41.5 MiB)",
" RX errors 0 dropped 0 overruns 0 frame 0",
" TX packets 6922 bytes 709784 (693.1 KiB)",
" TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0",
"",
"lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536",
" inet 127.0.0.1 netmask 255.0.0.0",
" inet6 ::1 prefixlen 128 scopeid 0x10<host>",
" loop txqueuelen 1000 (Local Loopback)",
" RX packets 22 bytes 13552 (13.2 KiB)",
" RX errors 0 dropped 0 overruns 0 frame 0",
" TX packets 22 bytes 13552 (13.2 KiB)",
" TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0"
]
}
ok: [web02] => {
"msg": [
"eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500",
" inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255",
" inet6 fe80::20c:29ff:fe02:880a prefixlen 64 scopeid 0x20<link>",
" ether 00:0c:29:02:88:0a txqueuelen 1000 (Ethernet)",
" RX packets 24380 bytes 33105256 (31.5 MiB)",
" RX errors 0 dropped 0 overruns 0 frame 0",
" TX packets 6460 bytes 424432 (414.4 KiB)",
" TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0",
"",
"eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500",
" inet 172.16.1.8 netmask 255.255.255.0 broadcast 172.16.1.255",
" inet6 fe80::20c:29ff:fe02:8814 prefixlen 64 scopeid 0x20<link>",
" ether 00:0c:29:02:88:14 txqueuelen 1000 (Ethernet)",
" RX packets 32328 bytes 43371472 (41.3 MiB)",
" RX errors 0 dropped 0 overruns 0 frame 0",
" TX packets 6391 bytes 648620 (633.4 KiB)",
" TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0",
"",
"lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536",
" inet 127.0.0.1 netmask 255.0.0.0",
" inet6 ::1 prefixlen 128 scopeid 0x10<host>",
" loop txqueuelen 1000 (Local Loopback)",
" RX packets 0 bytes 0 (0.0 B)",
" RX errors 0 dropped 0 overruns 0 frame 0",
" TX packets 0 bytes 0 (0.0 B)",
" TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0"
]
}
# 尝试只取出网卡信息
[root@m01 ~/ansible]$ vim test.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ifconfig"
register: suibian
- name: Return Result
debug:
# 表示取出列表的第一行,0就是第一个元素,1才是第二个,以此类推
msg: "{{ suibian.stdout_lines.0 }}"
# 查看结果
[root@m01 ~/ansible]$ ansible-playbook test.yml
PLAY [web_group] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [web02]
ok: [web01]
TASK [Test Register Vars] ******************************************************
changed: [web01]
changed: [web02]
TASK [Return Result] ***********************************************************
ok: [web01] => {
"msg": "eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500"
}
ok: [web02] => {
"msg": "eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500"
}
----------------------------------------------------------------------------------------
# 如何做到取出整个eth0信息
# 1.第一种方式
[root@m01 ~/ansible]$ vim test.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ifconfig"
register: suibian
- name: Return Result
debug:
# 表示取出第一行到第八行的内容
msg: "{{ suibian.stdout_lines[0:7] }}"
# 2.第二种方式
[root@m01 ~/ansible]$ vim test.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ifconfig"
register: suibian
- name: Return Result
debug:
msg:
- "{{ suibian.stdout_lines.0 }}"
- "{{ suibian.stdout_lines.1 }}"
- "{{ suibian.stdout_lines.2 }}"
- "{{ suibian.stdout_lines.3 }}"
- "{{ suibian.stdout_lines.4 }}"
- "{{ suibian.stdout_lines.5 }}"
- "{{ suibian.stdout_lines.6 }}"
- "{{ suibian.stdout_lines.7 }}"
# 第三种方式
[root@m01 ~/ansible]$ vim test.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ifconfig"
register: suibian
- name: Return Result
debug:
msg: "{{ suibian.stdout_lines[:7] }}"
# 最后的结果
[root@m01 ~/ansible]$ ansible-playbook test.yml
PLAY [web_group] *********************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************
ok: [web02]
ok: [web01]
TASK [Test Register Vars] ************************************************************************************************
changed: [web01]
changed: [web02]
TASK [Return Result] *****************************************************************************************************
ok: [web01] => {
"msg": [
"eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500",
" inet 10.0.0.7 netmask 255.255.255.0 broadcast 10.0.0.255",
" inet6 fe80::20c:29ff:fee9:c96a prefixlen 64 scopeid 0x20<link>",
" ether 00:0c:29:e9:c9:6a txqueuelen 1000 (Ethernet)",
" RX packets 25071 bytes 33157588 (31.6 MiB)",
" RX errors 0 dropped 0 overruns 0 frame 0",
" TX packets 7829 bytes 1705024 (1.6 MiB)"
]
}
ok: [web02] => {
"msg": [
"eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500",
" inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255",
" inet6 fe80::20c:29ff:fe02:880a prefixlen 64 scopeid 0x20<link>",
" ether 00:0c:29:02:88:0a txqueuelen 1000 (Ethernet)",
" RX packets 24429 bytes 33110417 (31.5 MiB)",
" RX errors 0 dropped 0 overruns 0 frame 0",
" TX packets 6504 bytes 427776 (417.7 KiB)"
]
}
# 在group_vars官方推荐目录下定义web_group组专用变量
[root@m01 ~/ansible]$ vim group_vars/web_group
## 表示:lnmp变量下包含pkg变量,pkg变量下又包含了web,db,php三个变量(两个空格缩进,但是不能加横杠)
lnmp:
pkg:
web: nginx
db: mariadb-server
php: php-fpm
# 调用层级变量需要在变量之间加上.符号
[root@m01 ~/ansible]$ vim test.yml
- hosts: web_group
tasks:
# 表示安装mariadb-server
- name: Install {{ lnmp.pkg.db }}
yum:
name: "{{ lnmp.pkg.db }}"
state: present
# 执行结果
[root@m01 ~/ansible]$ ansible-playbook test.yml
PLAY [web_group] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [web02]
ok: [web01]
## 任务名也成功调用变量
TASK [Install mariadb-server] **************************************************
changed: [web01]
changed: [web02]
PLAY RECAP *********************************************************************
web01 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web02 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
手机扫一扫
移动阅读更方便
你可能感兴趣的文章