变量
vim file.yml
[
- name: test
hosts: westos
vars: ----定义变量
NAME: westos 定义变量NAME为westos
tasks:
- name: debug
debug:
msg: "{{ NAME }}"
- name: create file
file:
path: "/mnt/{{NAME}}"
state: touch
- name: create file
file:
path: "/mnt/{{item}}" 使用loop循环必须要用item
state: touch
loop:
- file1
- file2
- file3
- name: copy
copy:
dest: /mnt/testfile
content: "{{ansible_facts['fqdn']}}" 把用户名称打入testfile
]
===========================================================
vim userlist.yml
[
- name: create user
hosts: westos
vars:
USER1: ----定义变量USER1
NAME: user1 ---赋予变量详细附属信息
UID: 678
tasks:
- name: create user1
user:
name: "{{USER1.NAME}}" ----变量的使用
uid: "{{USER1.UID}}"
]
使用文件形式
vim user_list.yml ---- 在文件中定义变量
[
---
USER1:
NAME: user1
UID: 666
]
vim userlist.yml
[
- name: create user
hosts: westos
vars_files:
- ./user_list.yml ----当前目录的user_list.yml,使用此文件
tasks:
- name: create user1
user:
name: "{{USER1.NAME}}"
uid: "{{USER1.UID}}"
]
====================================================
设定清单变量
vim inventory
[
[westos:vars] ------编辑清单里的变量
WESTOS=who are you
]
vim list.yml
[
- name: test
hosts: westos
tasks:
- debug:
msg: "{{WESTOS}}" ----引用清单里的变量
]
================================================
注册变量
vim www.yml
- name: www
hosts: westos
tasks:
- shell: test -e /mnt/file1
register: out
- debug:
msg: "{{out.rc}}"
事实变量
- name: www
hosts: westos
tasks:
- debug:
msg: "{{ansible_facts['fqdn']}}"
========================================================================
j2
能抓取所有主控机和受控机的ip
先在受控机建立westos
主控机:
vim westos.yml
[
- name: test
hosts: all
tasks:
- name: host j2
template:
src: ./hosts.j2
dest: /mnt/westos
]
vim hosts.j2
[
{%for HOST in groups['all']%}
{{HOST}} {{hostvars[HOST]['ansible_facts']['fqdn']}}
{%endfor%}
]
练习,分别在网页输入www.westos.com, linux.westos.com, news.westos.com,最终显示其名
vim http.yml
WEBS:
- DOC: /var/www/html
INDEX: /var/www/html/index.html
INDEX_TEST: www.westos.org
- NAME: linux.westos.org
DOC: /var/www/virtual/westos.com/linux
INDEX: /var/www/virtual/westos.com/linux/index.html
INDEX_TEST: linux.westos.org
- NAME: news.westos.org
DOC: /var/www/virtual/westos.com/news
INDEX: /var/www/virtual/westos.com/news/index.html
INDEX_TEST: news.westos.org
vim http.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 %}
vim web.yml
- name: web
hosts: westos
vars_files: ./http.yml
tasks:
- name: install apache
dnf:
name: httpd
state: present
- name: check_file
file:
path: /etc/httpd/conf.d/vhosts.conf
state: absent
- name: firewalld
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
- name: start apache
service:
name: httpd
state: restarted
enabled: yes
- name: create
file:
path: "{{item.DOC}}"
state: directory
loop:
"{{WEBS}}"
- name: configure
template:
src: ./http.j2
dest: /mnt/http
- name: index
copy:
content: "{{item.INDEX_TEST}}"
dest: "{{item.INDEX}}"
loop:
"{{WEBS}}"
=================================================
加密
ansible-vault encrypt westos.yml -------给westos加密
cat westos.yml ----查看只会发现加密码
ansible-vault view westos.yml 查看需要输入密码
ansible-vault edit westos.yml ----编辑westos,输入密码
vim pass ----为了方便,提前编写一个文件写入密码
【
westos
】
ansible-vault view westos.yml --vault-password-file=pass
利用文件里的密码直接,不用登陆直接查看westos
ansible-vault rekey westos.yml --vault-password-file=pass
修改密码,注意:修改完密码后,原先编辑的密码文件需要更改为修改后的密码