只能包含数字,下划线,字母
只能用下划线或字母开头
全局: 从命令行或配置文件中设定的
paly: 在play和相关结构中设定的
主机: 由清单,事实收集或注册的任务
变量优先级设定:
狭窄范围有限与广域范围
vim test.yml --- - name: test play vars: username: lee hosts: westos1 tasks: - name: test vars debug: var: username ansible-playbook test.yml
vim user_list.yml --- user: westosuser vim westos.yml --- - name: Create User hosts: all vars_files: - ./user_list.yml
tasks: - name: create user user: name: "{{ USER }}"
#在定义主机变量和清单变量时使用
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
group_vars ##清单变量,目录中的文件名称与主机清单名称一致
host_vars ##主机变量,目录中的文件名称与主机名称一致
ansible-playbook user.yml -e “USER=hello”
--- USER: lee: age: 18 obj: linux westos: age: 20 obj: java
- 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
su - devops #切换用户 cd .ansible #进入个人用户级Ansible操作环境 vim web.yml #编写脚本文件 --- - name: set web vhosts hosts: westos1 vars: - WEB1: WEBNAME: www.westos.org DOC: /var/www/html INDEX: /var/www/virtual/westos.org/html - WEB2: WEBNAME: linux.westos.org DOC: /var/www/virtual/westos.org/html INDEX: /var/www/virtual/westos.org/index.html tasks: - name: install apache dnf: name: httpd state: present - name: create web2 dir file: path: "{{ WEB2['DOC'] }}" state: directoy - name: create web1 index copy: dest: "{{WEB1['INDEX']}}" content: "{{ WEB1['WEBNAME'] }}" - name: create web2 index copy: dest: "{{WEB2['INDEX']}}" content: "{{ WEB2['WEBNAME'] }}" - name: configure vhost lineinfile: path: /etc/httpd/conf.d/vhosts.conf line: |+ <Virtualhost _default_:80> DocumentRoot "{{ WEB1['DOC'] }}" </Virtualhost> create: yes - name: restart apache service: nmae: httpd state: restarted
在访问主机编写解析
vim /etc/hosts
#register 把模块输出注册到指定字符串中
--- - name: test register hosts: 172.25.0.254 tasks: - name: hostname command shell: hostname register: info - name: show messages shell: echo "{{info['stdout']}}"
事实变量是ansible在受控主机中自动检测出的变量
事实变量中还有与主机相关的信息
当需要使用主机相关信息时不需要采集赋值,直接调用即可
因为变量信息为系统信息所以不能随意设定仅为采集信息,故被成为事实变量
--- - name: test register hosts: 172.25.0.254 tasks: - name: show messages debug: msg: "{{ansible_facts['architecture']}}" gather_facts: no ##在playbook中关闭事实变量收集
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是Python下一个被广泛应用的模版引擎
他的设计思想来源于Django的模板引擎,
并扩展了其语法和一系列强大的功能。
其中最显著的一个是增加了沙箱执行功能和可选的自动转义功能
{# /etc/hosts line #} ##注释说明文件用途 127.0.0.1 localhost ##文件内容 {{ ansible_facts['all_ipv4_addresses'] }} {{ansible_facts['fqdn']}} ##使用事实变量
vim users.yml users: - westos - linux - ansible vim test.j2 {% for NAME in users %} {{ NAME }} {%endfor%}
{% 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%}
--- - name: test register hosts: xxxx tasks: - name: create hosts template: src: ./xxxx.j2 dest: /mnt/hosts
--- - 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
ansible-vault create westos #创加密文件,设置完密码进入文件,可自行编写
ansible-vault view westos #交互式查看 vim mima “密码” ansible-vault view --vault-password-file=mima westos #直接查看
vim jwl hello jwl123 ansible-vault encrypt jwl #加密设置密码 ansible-vault view jwl #查看文件
ansible-vault edit jwl #交互式编辑 ansible-vault view jwl #查看 ansible-vault edit --vault-password-file=mima westos #直接编辑加密文件
ansible-vault rekey westos vim mima2 " 密码" ansible-vault rekey westos --new-vault-password-file=mima2 #编辑好密码文件,一步修改
ansible-vault decrypt westos ##文件永久解密 cat westos ansible-vault decrypt jwl --output=linux ##文件解密保存为linux cat linux
ansible-playbook apache_install.yml --ask-vault-pass #加密 ansible-playbook timesync.yml --ask-vault-pass