Ansible变量
变量定义的方式
- 命令行定义变量
- 在play文件中定义变量
- 在主机清单里定义变量
变量的优先级
命令行>playbook文件>inventory文件
如何定义变量
playbook中定义变量
playbook变量可以通过多种方式进行定义,最简单的方式就是在playbook的开头通过vars进行定义。
- hosts: web_group
vars:
- pkg: nginx
- pkg2: tree
tasks:
- name: Create File
file:
path: /root/{{ pkg }}
state: touch
- name: Install {{ pkg2 }}
yum:
name: "{{ pkg2 }}"
state: present
vars_file定义变量
刚才我们学到在playbook中使用vars定义变量,有一个缺陷,就是其他的playbook无法使用该变量。所以我们可以采取第二种定义变量的方式,在vars_file中定义变量。
#定义变量
[root@m01 ~]# vim bianliang.yml
pkg: httpd
pkk: tree
ppk: rsync
#配置文件
[root@m01 ~]# vim 10.yml
- hosts: web_group
vars_files: /root/bianliang.yml
tasks:
- name: create file
file:
path: /root/{{ pkk }}
state: touch
- name: install {{ ppk }}
yum:
name: "{{ ppk }}"
state: present
[root@m01 ansible]# vim bianliang.yml
pkg:
- httpd
- tree
- mariadb-server
## 内置变量
- hosts: web_group
vars_files: /root/ansible/bianliang.yml
tasks:
- name: Create File
file:
path: /root/{{ ansible_fqdn }}_{{ ansible_default_ipv4.address }}_{{ ansible_memtotal_mb }}
state: touch
inventory定义变量
注意:在Inventory中定义变量,主机的变量要高于主机组的变量,所以该方法不推荐使用,容易将环境弄乱。
1.命令行
2.playbook
- vars_file
- vars
3.主机清单
- host_vars
- group_vars
- inventory.host
- inventory.group
官方推荐的变量定义位置
## 1.创建两个目录
group_vars
host_vars
## 2.测试主机清单优先级
/etc/ansible/hosts
[web_group:vars]
filename=inventory_webgroup
[web01:vars]
filename=inventory_web01
[web02:vars]
filename=inventory_web02
[web03:vars]
filename=inventory_web03
hosts_vars
[root@m01 host_vars]# cat ./*
web01
filename: web01
web02
filename: web02
web03
filename: web03
[root@m01 group_vars]# vim web_group
filename: web_group
变量注册
如上执行结果可见,当我们使用shell模块执行ls -l /时,ansible给我们返回的只有changed我们无法看到执行之后的结果,所以此时我们需要使用到变量注册
#编辑playbook
[root@m01 ~]# vim register.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ls -l /"
register: list_dir #变量注册,把"ls -l /"的结果赋值给list_dir
- name: Return Result
#得到上面结果,用debug模块,msg调用上面的结果打印出来
debug:
msg: "{{ list_dir }}"
#查看执行结果
[root@m01 ~]# ansible-playbook register.yml
#只输出自己想要的内容
[root@m01 ~]# vim register.yml
- hosts: web_group
tasks:
- name: Test Register Vars
shell: "ls -l /"
register: list_dir
- name: Return Result
debug:
msg: "{{ list_dir.stdout_lines }}"
#查看结果
[root@m01 ~]# ansible-playbook register.yml
#debug模块常用参数
msg: #调试输出的消息
var: #将某个任务执行的输出作为变量传递给debug模块,debug会直接将其打印输出
verbosity: #debug的级别(默认是0级,全部显示)
vim test_shell.yml
- hosts: web_group
tasks:
- name: xxx
shell: 'ls -l /etc/passwd /etc/shadow'
register: suibian
- name: get result
debug:
msg: "{{ suibian.stdout_lines.1 }}"
[root@m01 ~]# vim test_shell.yml
- hosts: web_group
tasks:
- name: ifconfig
shell: 'ifconfig'
register: suibian
- name: get 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 }}"
# 第二种
- hosts: web_group
tasks:
- name: ifconfig
shell: 'ifconfig'
register: suibian
- name: get result
debug:
msg: "{{ suibian.stdout_lines[0:7] }}"
# 也可以这么写
- hosts: web_group
tasks:
- name: ifconfig
shell: 'ifconfig'
register: suibian
- name: get result
debug:
msg: "{{ suibian.stdout_lines[:7] }}"
层级定义变量
lnmp:
pkg:
web: nginx
db: mariadb-server
php: php-fpm
- hosts: web01_zls
tasks:
- name: Install {{ lnmp.pkg.db }}
yum:
name: "{{ lnmp.pkg.db }}"
state: present
facts缓存
## 关闭facts缓存
- hosts: web01_zls
gather_facts: no
tasks:
- name: Install {{ lnmp.pkg.db }}
yum:
name: "{{ lnmp.pkg.db }}"
state: present
作业(变量优化)
主机名 | 外网IP | 内网ip | 角色 | 部署服务 |
---|---|---|---|---|
m01 | 10.0.0.61 | 172.16.1.61 | ansible管理端 | ansible |
backup | 10.0.0.41 | 172.16.1.41 | ansible被管理端,rsync服务端,nfs备机 | rsync,nfs |
nfs | 10.0.0.31 | 172.16.1.31 | ansible被管理端,rsync客户端,nfs服务端 | rsync,nfs,sersync |
web01 | 10.0.0.7 | 172.16.1.7 | ansible被管理端,nfs客户端,web | nginx,nfs,wordpress |
web02 | 10.0.0.8 | 172.16.1.8 | ansible被管理端,nfs客户端,web | nginx,nfs,wordpress |
db01 | 10.0.0.51 | 172.16.1.51 | ansible被管理端,数据库 | mariadb |
需要写的配置文件
# cd /root/ansible/host_vars (backup,nfs,db01都写一个)
vim backup
passwd: rstnc_backup:123
vim nfs
passwd: 123
cd /root/ansible/group_vars/
vim rsync_install_group 还有web_group
lnmp:
pkg:
ng: nginx
db: mariadb
dbs: mariadb-server
dbM: MySQL-python
php: php-fpm
rs: rsync
rsd: rsyncd
ser: sersyncd
nfs: nfs-utils
nfr: nfs-server
变量优化
- hosts: all
tasks:
- name: Create www Group
group:
name: www
gid: 666
state: present
- name: Creste www User
user:
name: www
uid: 666
group: www
state: present
shell: /sbin/nolongin
create_home: no
- name: Stop Firewalld Server
service:
name: firewalld
state: stopped
- name: Disabled Selinux
selinux:
state: disabled
# rsync_install_group
- hosts: rsync_install_group
tasks:
- name: Install {{ lnmp.pkg.rs }} {{ lnmp.pkg.nfs }} Server
yum:
name:
- "{{ lnmp.pkg.rs }}"
- "{{ lnmp.pkg.nfs }}"
state: present
- name: Confingure Rsyncd Conf
copy:
src: /root/ansible/rsyncd.conf
dest: /etc/rsyncd.conf
- name: Create Rsyncd Password Firewalld
copy:
content: "{{ passwd }}"
dest: /etc/rsync.passwd
mode: 0600
- name: Create Backup Directory
file:
path: /backup
owner: www
group: www
state: directory
- name: Start "{{ lnmp.pkg.rsd }}" Server
service:
name: "{{ lnmp.pkg.rsd }}"
state: started
enabled: yes
- name: Install NFS Server
copy:
content: /data 172.16.1.0/24(rw,all_squash,anonuid=666,anongid=666)
dest: /etc/exports
- name: Create data Directory
file:
path: /data
state: directory
owner: www
group: www
mode: 0755
- name: Start {{ lnmp.pkg.nfr }} Server
service:
name: "{{ lnmp.pkg.nfr }}"
state: started
enabled: yes
# nfs
- hosts: nfs
tasks:
- name: Install Rsyncd Server
yum:
name: "{{ lnmp.pkg.ser }}"
state: present
- name: Create app Directory
file:
path: /app
state: directory
- name: Install Sersync Server
unarchive:
src: /root/ansible/sersync2.5.4_64bit_binary_stable_final.tar.gz
dest: /app
- name: Confingure Sersync Conf
copy:
src: /root/ansible/conf.xml
dest: /app/GNU-Linux-x86/confxml.xml
mode: 0755
- name: Push Sersync Start Shell
copy:
src: /root/ansible/sersyncd.service
dest: /usr/lib/systemd/system
- name: Start Sersync
service:
name: "{{ lnmp.pkg.ser }}"
state: started
enabled: yes
#db01
- hosts: db01
tasks:
- name: Install Maraidb
yum:
name:
- "{{ lnmp.pkg.dbs }}"
- "{{ lnmp.pkg.dbM }}"
state: present
- name: Start Maraidb
service:
name: "{{ lnmp.pkg.db }}"
state: started
enabled: yes
- name: Create wordpress Database
mysql_db:
name: wordpress
state: present
encoding: utf8
- name: Create wordpress User
mysql_user:
name: wordpress
state: present
priv: "wordpress.*:ALL"
host: "%"
password: "123"
#web_group
- hosts: web_group
tasks:
- name: Install {{ lnmp.pkg.ng }} {{ lnmp.pkg.nfs }}
yum:
name:
- "{{ lnmp.pkg.ng }}"
- "{{ lnmp.pkg.nfs }}"
state: present
- name: Push PHP
unarchive:
src: /root/ansible/php.tgz
dest: /tmp
- name: Install PHP
shell: "rpm -Uvh /tmp/*.rpm"
ignore_errors: yes
- name: Push nginx Conf
copy:
src: /root/ansible/nginx.conf
dest: /etc/nginx/nginx.conf
- name: Push nginx Vhost Conf
copy:
src: /root/ansible/blog.drz.com.conf
dest: /etc/nginx/conf.d/blog.drz.com.conf
- name: Push PHP Conf
copy:
src: /root/ansible/www.conf
dest: /etc/php-fpm.d/www.conf
- name: Create code Directory
file:
path: /code
state: directory
owner: www
group: www
mode: 0755
- name: Push code
unarchive:
src: /root/ansible/latest.tar.gz
dest: /code
owner: www
group: www
- name: Create uploads Directory
file:
path: /code/wordpress/wp-content/uploads
state: directory
owner: www
group: www
- name: Start PHP
service:
name: "{{ lnmp.pkg.php }}"
state: started
enabled: yes
- name: Chmod Sock
file:
path: /dev/shm/php71w.sock
owner: www
group: www
- name: Start {{ lnmp.pkg.nfr }} Server
service:
name: "{{ lnmp.pkg.nfr }}"
state: started
enabled: yes
- name: Start {{ lnmp.pkg.ng }}
service:
name: "{{ lnmp.pkg.ng }}"
state: started
enabled: yes
- name: Mount Code Disabled
mount:
path: /code/wordpress/wp-content/uploads
src: 172.16.1.31:/data
fstype: nfs
state: mounted
- name: Shouquan
file:
path: /var/lib/nginx
owner: www
group: www
recurse: yes