ansible变量的定义

1 vars定义变量

[root@wyl01 ansible1]# cat test.yml 
---
- hosts: web
  vars:
    - wyl_name: "wyl123"   #前面的-也可以不加
    - phone: 18255109527

  tasks:
   - name: add content
     copy:
       content: "{{wyl_name}}"
       dest: /wyl/ansible1/a.txt

2  vars_files 方式

[root@wyl01 ansible1]# cat variables 
wyl_name: wyl
http_port: 80

# 案例一
[root@wyl01 ansible1]# cat test.yml 
---
- hosts: web
  vars_files:
    - variables

  tasks:
   - name: add content
     copy:
       content: "{{http_port}}"
       dest: /wyl/ansible1/b.txt

# 案例二
[root@wyl01 ansible1]# cat test.yml 
---
- hosts: web
  vars_files:
    - variables

  tasks:
   - name: add content
     template:
       src: /wyl/ansible1/b.txt
       dest: /wyl/ansible1/c.txt

[root@wyl01 ansible1]# cat b.txt 
{{wyl_name}}
{{http_port}}

[root@wyl01 ansible1]# cat c.txt 
wyl
80

     上例中使用"var_files"关键字引入了对应的变量文件,然后使用了文件中定义的变量。当然也可以引用多个变量文件,每个被引入的文件都需要以“-”开头。“var”关键字和“var_files”关键字可以同时使用,如下:

var:
- wyl_name: "wyl9527"
vars_files:
- variables

 

3 vars_prompt交互式定义变量

[root@wyl01 ansible1]# cat c.txt
{{http_port}}

[root@wyl01 ansible1]# cat test.yml

---
- hosts: web
  vars_prompt:
    - name: http_port
      prommpt: 'http_port='
      private: no     # no为明文, yes为暗文

  tasks:
   - name: add content
     template:
       src: /wyl/ansible1/c.txt
       dest: /wyl/ansible1/c.txt



[root@wyl01 ansible1]# ansible-playbook test.yml 
http_port: 80

PLAY [web] *************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [192.168.52.128]

TASK [add content] *****************************************************************************************************
changed: [192.168.52.128]

PLAY RECAP *************************************************************************************************************
192.168.52.128             : ok=2    changed=1    unreachable=0    failed=0   

[root@wyl01 ansible1]# cat c.txt 
80

4 主机清单中定义变量

[root@wyl01 ansible1]# vim /etc/ansible/hosts 

[web]
192.168.52.128

# 定义变量
[web:vars]
wyl_name = wyl9527
http_port = 8080


[root@wyl01 ansible1]# vim test.yml
- hosts: web

  tasks:
   - name: add content
     template:
       src: /wyl/ansible1/c.txt
       dest: /wyl/ansible1/d.txt


[root@wyl01 ansible1]# cat c.txt 
{{wyl_name}}
{{http_port}}


[root@wyl01 ansible1]# ansible-playbook test.yml 

PLAY [web] *************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [192.168.52.128]

TASK [add content] *****************************************************************************************************
changed: [192.168.52.128]

PLAY RECAP *************************************************************************************************************
192.168.52.128             : ok=2    changed=1    unreachable=0    failed=0   

5 register获取变量

playbook中tasks之间相互传递变量

[root@wyl01 auto]# cat register.yml 
---
- hosts: all
  gather_facts: no
  tasks: 
    - name: display register vars
      shell: hostname
      register: info
    - name: dispaly info
      debug: msg="{{info}}"

ansible变量的定义_第1张图片

你可能感兴趣的:(ansible变量的定义)