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}}"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值