vars
定义方法
第一种定义方法
---
- hosts: testB
vars: 定义变量
var1: file 变量值
remote_user: root
tasks:
- name: touch file
file: path=/testdir/{{var1}} state=touch
第二种定义方法
---
- hosts: testB
vars:
- var1: file
remote_user: root
tasks:
- name: touch file
file: path=/testdir/{{var1}} state=touch
定义多个变量
第一种
---
- hosts: testB
vars:
- var1: file
- var2: file2
remote_user: root
tasks:
- name: touch file
file: path=/testdir/{{var1}} state=touch
- name: touch file2
file: path=/testdir/{{var2}} state=directory
第二种
---
- hosts: testB
vars:
var1: file
var2: file2
remote_user: root
tasks:
- name: touch file
file: path=/testdir/{{var1}} state=touch
- name: touch file2
file: path=/testdir/{{var2}} state=directory
第三种:属性定义
- 引用变量属于开头位置要用引号引起来
---
- hosts: testB
remote_user: root
vars:
httpd:
conf80: /etc/httpd/conf.d/80.conf
conf8000: /etc/httpd/conf.d/8000.conf
tasks:
- name: task1
file: path={{httpd.conf80}} state=touch
- name: task2
file:
path: "{{httpd.conf8000}}"
state: touch
---
- hosts: testB
remote_user: root
vars:
httpd:
conf80: /etc/httpd/conf.d/80.conf
conf8000: /etc/httpd/conf.d/8000.conf
tasks:
- name: task1
file: path={{httpd.conf80}} state=touch
- name: task2
file:
path: "{{httpd['conf8000']}}" 区别
state: touch
变量文件分离
写入文件
vim httpd_vars.yml
httpd:
conf80: /etc/httpd/conf.d/80.conf
conf8000: /etc/httpd/conf.d/8000.conf
playbook:
---
- hosts: testB
remote_user: root
vars_files:
- httpd_vars.yml
tasks:
- name: task1
file: path={{httpd.conf80}}
state=touch
- name: task2
file:
path: "{{httpd['conf8000']}}"
state: touch
---
- hosts: testB
remote_user: root
vars:
- conf80: /etc/httpd/conf.d/80.conf
vars_files:
- httpd_vars.yml
tasks:
- name: task1
file: path={{conf80}}
state=touch
- name: task2
file:
path: "{{httpd['conf8000']}}"
state: touch