一.变量命名
只能包含数字,下划线,字母
只能用下划线或字母开头
二.变量级别
全局: 从命令行或配置文件中设定的
paly: 在play和相关结构中设定的
主机: 由清单,事实收集或注册的任务变量优先级设定:
狭窄范围有限与广域范围
[devops@ansible .ansible]$ vim test1.yaml
- name: test
hosts: westos
vars:
NAME: westos ##全局变量
tasks:
- name: debug
debug:
msg: "{{NAME}}"
- name: create file
file:
path: "/mnt/{{NAME}}"
state: touch
[devops@ansible .ansible]$ ansible-playbook test1.yaml
[devops@ansible .ansible]$ vim test1.yaml
- name: test
hosts: westos
vars:
NAME: westos
tasks:
- name: debug
debug:
msg: "{{NAME}}"
- name: create file
file:
path: "/mnt/{{NAME}}"
state: touch
- name: create file
file:
path: "/mnt/{{item}}" ##paly: 在play和相关结构中设定的
state: touch
loop:
- file1
- file2
- file3
- file4
- name: copy
copy:
dest: /mnt/testfile
content: "{{ansible_facts['fqdn']}}" ##主机变量,由清单,事实收集或注册的任务
[devops@ansible .ansible]$ ansible-playbook test1.yaml
三.变量设定和使用方式
1.在playbook中直接定义变量
---
- name: test var
hosts: all
vars:
USER: westosuser
[devops@ansible .ansible]$ vim user.yaml
[devops@ansible .ansible]$ cat user.yaml
- name: create user
hosts: westos
vars:
USER1:
NAME: user1
UID: 666
tasks:
- name: create user1
user:
name: "{{USER1['NAME']}}"
uid: "{{USER1.UID}}"
[devops@ansible .ansible]$ ansible-playbook user.yaml
2.在文件中定义变量
vim user_list.yml
---
user: westosuservim westos.yml
---
- name: Create User
hosts: all
vars_files:
- ./user_list.yml
[devops@ansible .ansible]$ vim user.yaml
[devops@ansible .ansible]$ cat user.yaml
- name: create user
hosts: westos
vars_files:
- ./user_list.yaml
tasks:
- name: create user1
user:
name: "{{USER1['NAME']}}"
uid: "{{USER1.UID}}"
[devops@ansible .ansible]$ vim user_list.yaml
[devops@ansible .ansible]$ cat user_list.yaml
---
USER1:
NAME: user1
UID: 666
[devops@ansible .ansible]$ ansible-playbook user.yaml
3.使用变量
tasks:
- name: create user
user:
name: "{{ USER }}"
4.设定主机变量和清单变量
##在定义主机变量和清单变量时使用
[devops@ansible .ansible]$ vim inventory
[devops@ansible .ansible]$ cat inventory
[westos]
172.25.254.205
[lee]
172.25.254.210
[westos:vars]
WESTOS=hello
[devops@ansible .ansible]$ vim list.yaml
[devops@ansible .ansible]$ cat list.yaml
- name: test
hosts: westos
tasks:
- debug:
msg: "{{WESTOS}}"
[devops@ansible .ansible]$ ansible-playbook list.yaml
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@ansible .ansible]$ vim web_list.yaml
[devops@ansible .ansible]$ cat web_list.yaml
---
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.org
[devops@ansible .ansible]$ vim web.yaml
[devops@ansible .ansible]$ cat web.yaml
- name: install web server
hosts: westos
vars_files: ./web_list.yaml
tasks:
- name: in stall apache
dnf:
name: httpd
state: present
- name: firewalld
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
- name: create directory
file:
path: /var/www/virtual/westos.com/linux/
state: directory
- name: copy
copy:
dest: /var/www/html/index.html
content: "www.westos.com"
- name: copy
copy:
dest: /var/www/virtual/westos.com/linux/index.html
content: "linux.westos.com"
- name: check_file
file:
path: /etc/httpd/conf.d/vhost.conf
state: absent
- name: create vhost
lineinfile:
path: /etc/httpd/conf.d/vhost.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
[devops@ansible .ansible]$ ansible-playbook web.yaml
8.注册变量
##register 把模块输出注册到指定字符串中
---
- name: test register
hosts: 172.25.254.210
tasks:
- name: hostname command
shell:
hostname
register: info- name: show messages
shell:
echo "{{info['stdout']}}"
[devops@ansible .ansible]$ vim westos.yaml
- name: test
hosts: westos
tasks:
- name: check file
shell: test -e /mnt/file1
register: out
- name: debug
debug:
msg: "{{out.end}}"
[devops@ansible .ansible]$ ansible-playbook westos.yaml
[devops@ansible .ansible]$ vim westos.yaml
- name: test
hosts: westos
tasks:
- name: check file
shell: test -e /mnt/file1
register: out
- name: debug
debug:
msg: "{{out.rc}}"
[devops@ansible .ansible]$ ansible-playbook westos.yaml
当命令可以运行成功时,rc=0
当命令可以运失败时,rc=1
9.事实变量
事实变量是ansible在受控主机中自动检测出的变量
事实变量中还有与主机相关的信息
当需要使用主机相关信息时不需要采集赋值,直接调用即可
因为变量信息为系统信息所以不能随意设定仅为采集信息,故被成为事实变量
[devops@ansible .ansible]$ ansible westos -m setup | less
[devops@ansible .ansible]$ cat westos.yaml
- name: test
hosts: westos
tasks:
- debug:
msg: "{{ansible_facts['fqdn']}}"
[devops@ansible .ansible]$ ansible-playbook westos.yaml
gather_facts: no ##在playbook中关闭事实变量收集
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"
四.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%}{%endfor%}
#j2模板在playbook中的应用#
#playbook1
---
- name: test register
hosts: xxxx
tasks:
- name: create hosts
template:
src: ./xxxx.j2
dest: /mnt/hosts
#playbook2
---
- name: test.j2
hosts: 172.25.0.254
vars:
students:
- name: student1
obj: linux
- name: student2
age: 18
obj: linux
tasks:
- template:
src: ./test.j2
dest: /mnt/list
[devops@ansible .ansible]$ cat host.yaml
- name: host playbook
hosts: westos
tasks:
- name: host j2
template:
src: ./host.j2
dest: /mnt/hosts
[devops@ansible .ansible]$ cat host.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{%for HOST in groups['all']%}
{{hostvars[HOST]['ansible_facts']['ens3']['ipv4']['address']}} {{hostvars[HOST]['ansible_facts']['fqdn']}}
{%endfor%}
五.Ansible的加密控制
1.创建建立文件
1.ansible-vault create westos
2. vim westos-vault
- name: test file
hosts: westos
tasks:
- name: test j2
ansible-vault create --vault-password-file=westos-valut westos
2.查看加密文件
ansible-vault view westos
ansible-vault view --vault-password-file=westos-valut westos
3.编辑加密文件
ansible-vault edit westos1
ansible-vault edit --vault-password-file=westos-valut westos
4.解密文件
ansible-vault decrypt westos ##文件永久解密
ansible-vault decrypt westos --output=linux ##文件解密保存为linux
5.加密现有文件
ansible-vault encrypt test
6.更改密码
ansible-vault rekey westos1
ansible-vault rekey westos1 --new-vault-password-file=key1
练习:
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@ansible .ansible]$ vim web_list.yaml
---
webs:
- doc: /var/www/html
index: www.westos.com
- name: linux.westos.com
doc: /var/www/virtual/westos.com/linux
index: linux.westos.com
- name: news.westos.com
doc: /var/www/virtual/westos.com/news
index: news.westos.com
[devops@ansible .ansible]$ vim westos.yaml
- name: web configure
hosts: westos
vars_files: ./web_list.yaml
tasks:
- name: create DocumentRoot ##建立目录
file:
path: "{{item.doc}}"
state: directory
loop:
"{{webs}}"
- name:
copy:
dest: "{{item.doc}}/index.html" ##在目录下新建index.html
content: "{{item.index}}"
loop:
"{{webs}}"
- name: configure webserver
template:
src: ./vhost.conf.j2
dest: /mnt/vhost.conf
[devops@ansible .ansible]$ vim vhost.conf.j2
{%for web in webs %}
{%if web.name is not defined%}
<VirtualHost _default_:80>
{%endif%}
{%if web.name is defined%}
<VirtualHost *:80>
ServerName {{web.name}}
{%endif%}
DocumentRoot {{web.doc}}
</VirtualHost>
{%endfor%}
[devops@ansible .ansible]$ ansible-playbook westos.yaml