##1.变量命名##
只能包含数字,下划线,字母
只能用下划线或字母开头
##2.变量级别##
全局: 从命令行或配置文件中设定的
paly: 在play和相关结构中设定的
主机: 由清单,事实收集或注册的任务
变量优先级设定:
狭窄范围有限与广域范围
##3.变量设定和使用方式##
#1.在playbook中直接定义变量
---
- name: test var
hosts: all
vars:
USER: westosuser
#2.在文件中定义变量#
vim user_list.yml
---
user: westosuser
vim westos.yml
---
- name: Create User
hosts: all
vars_files:
- ./user_list.yml
[devops@nodea111 .ansible]$ vim user_list.yml
---
USER1:
NAME: user1
UID: 666
[devops@nodea111 .ansible]$ vim user.yml
- name: create user
hosts: westos
vars_files:
- ./user_list.yml
tasks:
- name: create user1
user:
name: "{{USER1['NAME']}}"
uid: "{{USER1.UID}}"
[devops@nodea111 .ansible]$ ansible-playbook user.yml
#3.使用变量#
tasks:
- name: create user
user:
name: "{{ USER }}"
#4.设定主机变量和清单变量#
#在定义主机变量和清单变量时使用
vim inventory
[westos_list1]
172.25.0.254
172.25.0.1
[westos_list2]
172.25.0.2
[westos_list3]
172.25.0.3
[westos_group:children]
westos_list2
westos_list3
[westos_list1:vars]
USER=westos1
[westos_group:vars]
USER=westos2
[devops@nodea111 .ansible]$ cat inventory
[westos]
172.25.254.211
[westos:vars]
WESTOS=hello
[devops@nodea111 .ansible]$ vim var.yml
- name: test
hosts: westos
tasks:
- debug:
msg: "{{WESTOS}}"
[devops@nodea111 .ansible]$ ansible-playbook var.yml
PLAY [test] *****************************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [172.25.254.211]
TASK [debug] ****************************************************************************
ok: [172.25.254.211] => {
"msg": "hello"
}
PLAY RECAP ******************************************************************************
172.25.254.211 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#5.目录设定变量#
group_vars ##清单变量,目录中的文件名称与主机清单名称一致
host_vars ##主机变量,目录中的文件名称与主机名称一致
#6.用命令覆盖变量#
ansible-playbook user.yml -e "USER=hello"
#7.使用数组设定变量#
#vim user_var.yml
---
USER:
lee:
age: 18
obj: linux
westos:
age: 20
obj: java
#vim user.yml
- name: Create User
hosts: all
gather_facts: no
vars_files:
./user_var.yml
tasks:
- name: create user
shell:
echo "{{USER['lee']['age']}}"
echo "{{USER.westos.obj}}"
练习:
create web vhost
www.westos.com 80------ > /var/www/html------> www.westos.com
linux.westos.com 80 ------> /var/www/virtual/westos.com/linux -----> linux.westos.com
[devops@nodea111 .ansible]$ cat vhost_var.yml
web_default:
doc: /var/www/html
index: www.westos.com
web_linux:
name: linux.westos.com
doc: /var/www/virtual/westos.com/linux
index: linux.westos.com
[devops@nodea111 .ansible]$ cat creat_web.yml
- name: create web vhost
hosts: westos
vars_files:
./vhost_var.yml
tasks:
- name: install http
dnf:
name: httpd
state: present
- name: firewalld
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
- name: copy
copy:
dest: /var/www/html/index.html
content: " www.westos.com"
- name: create directory
file:
path: /var/www/virtual/westos.com/linux/
state: directory
- name: copy2
copy:
dest: /var/www/virtual/westos.com/linux/index.html
content: " linux.westos.com "
- name: check_file
file:
path: /etc/httpd/conf.d/vhosts.conf
state: absent
- name: create web
lineinfile:
path: /etc/httpd/conf.d/vhosts.conf
state: present
create: yes
line: |+
<VirtualHost _default_:80>
DocumentRoot {{web_default.doc}}
</VirtualHost>
<VirtualHost *:80>
ServerName {{web_linux.name}}
DocumentRoot {{web_linux.doc}}
</VirtualHost>
- name: start httpd
service:
name: httpd
state: restarted
enabled: yes
在浏览器主机进行地址解析
[root@westos_student11 ~]# vim /etc/hosts
172.25.254.211 www.westos.com linux.westos.com
#8.注册变量#
#register 把模块输出注册到指定字符串中
直接执行是看不到输出的,需要用注册变量显示输出
[devops@nodea111 .ansible]$ cat westos.yml
- name: test playbook
hosts: westos
tasks:
- name: check file
shell: test -e /mnt/file1
register: out
- name: debug
debug:
msg: "{{out.rc}}"
[devops@nodea111 .ansible]$ ansible-playbook westos.yml
PLAY [test playbook] ***********************************************************
TASK [Gathering Facts] *********************************************************
ok: [172.25.254.211]
TASK [check file] **************************************************************
changed: [172.25.254.211]
TASK [debug] *******************************************************************
ok: [172.25.254.211] => {
"msg": "0" ##0代表运行成功
}
PLAY RECAP *********************************************************************
172.25.254.211 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[devops@nodea111 .ansible]$ cat westos.yml
- name: test playbook
hosts: westos
tasks:
- name: check file
shell: test -e /mnt/file1
register: out
- name: debug
debug:
msg: "{{out.end}}"
[devops@nodea111 .ansible]$ ansible-playbook westos.yml
PLAY [test playbook] ***********************************************************
TASK [Gathering Facts] *********************************************************
ok: [172.25.254.211]
TASK [check file] **************************************************************
changed: [172.25.254.211]
TASK [debug] *******************************************************************
ok: [172.25.254.211] => {
"msg": "2021-12-03 09:30:17.763556" ##end显示时间
}
PLAY RECAP *********************************************************************
172.25.254.211 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
#9.事实变量#
事实变量是ansible在受控主机中自动检测出的变量
事实变量中还有与主机相关的信息
当需要使用主机相关信息时不需要采集赋值,直接调用即可
因为变量信息为系统信息所以不能随意设定仅为采集信息,故被成为事实变量
[devops@nodea111 .ansible]$ ansible westos -m setup ##采集的信息
[devops@nodea111 .ansible]$ cat westos.yml
- name: test playbook
hosts: westos
tasks:
- name: debug
debug:
msg: "{{ansible_facts['fqdn']}}" ##对应的是setup中的 "ansible_fqdn": "nodeb.westos.org",
[devops@nodea111 .ansible]$ ansible-playbook westos.yml
PLAY [test playbook] ***********************************************************
TASK [Gathering Facts] *********************************************************
ok: [172.25.254.211]
TASK [debug] *******************************************************************
ok: [172.25.254.211] => {
"msg": "nodeb.westos.org"
}
PLAY RECAP *********************************************************************
172.25.254.211 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
--------------------------------------
#10.魔法变量#
hostvars: ##ansible软件的内部信息
#eg:
ansible localhost -m debug -m "var=hostvars"
group_names: ##当前受管主机所在组
#eg:
ansible localhost -m debug -m "var=group_names"
groups: ##列出清单中所有的组和主机
#eg:
ansible localhost -m debug -m "var=groups"
inventory_hostname: ##包含清单中配置的当前授管主机的名称
#eg:
ansible localhost -m debug -m "var=inventory_hostname
[devops@nodea111 .ansible]$ ansible localhost -m debug -a 'var=hostvars'
[devops@nodea111 .ansible]$ ansible westos -m debug -a 'var=group_names'
172.25.254.211 | SUCCESS => {
"group_names": [
"westos"
]
}
[devops@nodea111 .ansible]$ ansible westos -m debug -a 'var=groups'
172.25.254.211 | SUCCESS => {
"groups": {
"all": [
"172.25.254.211"
],
"ungrouped": [],
"westos": [
"172.25.254.211"
]
}
}
[devops@nodea111 .ansible]$ ansible westos -m debug -a 'var=inventory_hostname'
172.25.254.211 | SUCCESS => {
"inventory_hostname": "172.25.254.211"
}
##JINJA2模板##
#介绍
Jinja2是Python下一个被广泛应用的模版引擎
他的设计思想来源于Django的模板引擎,
并扩展了其语法和一系列强大的功能。
其中最显著的一个是增加了沙箱执行功能和可选的自动转义功能
#j2模板书写规则#
{# /etc/hosts line #} ##注释说明文件用途
127.0.0.1 localhost ##文件内容
{{ ansible_facts['all_ipv4_addresses'] }} {{ansible_facts['fqdn']}} ##使用事实变量、
for 循环
vim users.yml
users:
- westos
- linux
- ansible
vim test.j2
{% for NAME in users %}
{{ NAME }}
{%endfor%}
if判定
{% for NAME in users if not NAME == "ansible" %}
User number {{loop.index}} - {{ NAME }}
{%endfor%}
loop.index ##循环迭代记数从1开始
loop.index0 ##循环迭代计数从0开始
{% for user in students %}
name: {{user['name']}}
{%if user['age'] is defined%}
age: {{user['age']}}
{%endif%}
{% if user['age'] is not defined %}
age: null
{% endif%}
obj:{{user['obj']}}
{%endfor%}
#j2模板在playbook中的应用#
[devops@nodea111 .ansible]$ mkdir host_vars
[devops@nodea111 .ansible]$ cat host_vars/172.25.254.211.yml
users:
- westos
- lee
- linux
[devops@nodea111 .ansible]$ cat test.j2
{# /mnt/westos #}
{% for user in users%}
{{ user }}
{% endfor %}
[devops@nodea111 .ansible]$ cat westos.yml
- name: test playbook
hosts: westos
tasks:
- name: test j2
template:
src: ./test.j2
dest: /mnt/westos
[devops@nodea111 .ansible]$ ansible-playbook westos.yml
[root@nodeb mnt]# cat westos
westos
lee
linux
[devops@nodea111 .ansible]$ cat westos1.yml
- name: host playbook
hosts: westos
tasks:
- name: hosts j2
template:
src: ./hosts.j2
dest: /mnt/westos1
[devops@nodea111 .ansible]$ cat hosts.j2
{%for HOST in groups['westos']%}
{{ hostvars[HOST]['ansible_facts']['fqdn']}}
{%endfor%}
## Ansible的加密控制 ##
#创建建立文件
1.
ansible-vault create westos
2.
vim westos-vault
lee
ansible-vault create --vault-password-file=westos-valut westos
#加密现有文件
ansible-vault encrypt test
#查看加密文件
ansible-vault view westos
ansible-vault view --vault-password-file=westos-valut westos
#编辑加密文件
ansible-vault edit westos1
ansible-vault edit --vault-password-file=westos-valut westos
##解密文件
ansible-vault decrypt westos ##文件永久解密
ansible-vault decrypt westos --output=linux ##文件解密保存为linux
##更改密码
ansible-vault rekey westos1
ansible-vault rekey westos1 --new-vault-password-file=key1
#playbook#
ansible-playbook apache_install.yml --ask-vault-pass