Ansible变量

ansible变量

定义变量规则:由字母/数字/下划线组成,变量需要以字母开头,ansible内置的关键字不能作为变量名。
ansible中,可以将变量简化为三个范围 Global范围(高):从命令行和ansible配置设置的变量 play范围(中):在play和相关结构中设置的变量 Host范围(低):inventory、facts或register的变量,在主机组和个别主机上设置的变量 三个范围的变量优先级依次降低,如果变量重复定义,则以优先级高的为准

注册和定义变量的各种方式

ansible中定义变量的方式有很多种,大致有:
(1) 将模块的执⾏结果注册为变量;
(2) 直接定义字典类型的变量;
(3) role中⽂件内定义变量;
(4) 命令⾏传递变量;
(5) 借助with_items迭代将多个task的结果赋值给⼀个变量;
(6) inventory中的主机或主机组变量;
(7) 内置变量。

vars定义变量

[root@ansible]# su - student
[student@ansible]$ cd ansible/
[student@ansible ansible]$ pwd
/home/student/ansible
[student@ansible ansible]$ vim sb.yml
--- 
- name: vars变量
  hosts: dev
  vars: 
    - aa: 11
    - bb: 22
    - cc: 
        a1: c31
        a2: c32 
  tasks: 
    - name: create debug
      debug: 
        msg: "{
  { aa }}" 
    - name: create debug2
      debug:
        msg: "{
  { bb }}"
    - name: create debug3
      debug:
       msg: "{
  { cc.a1 }}"
    - name: create debug4
      debug:
       msg: "{
  { cc.a2 }}"
       
[student@ansible ansible]$ ansible-playbook sb.yml -C

PLAY [vars变量] **********************************************************************

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

TASK [create debug] ************************************************************
ok: [node1] => {
    "msg": 11
}

TASK [create debug2] ***********************************************************
ok: [node1] => {
    "msg": 22
}

TASK [create debug] ************************************************************
ok: [node1] => {
    "msg": "c31"
}

TASK [create debug4] ***********************************************************
ok: [node1] => {
    "msg": "c32"
}

PLAY RECAP *********************************************************************
node1                      : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[student@ansible ansible]$ 

Vars_files定义变量

[student@ansible ansible]$ vim var.yml 
aa: 11
bb: 22
cc: 
  a1: c31
  a2: c32

[student@ansible ansible]$ vim sb.yml 
--- 
- name: vars_files变量
  hosts: dev
  vars_files: /home/student/ansible/var.yml 
  tasks: 
    - name: create debug
      debug: 
        msg: "{
  { aa }}" 
    - name: create debug2
      debug:
        msg: "{
  { bb }}"
    - name: create debug3
      debug:
        msg: "{
  { cc.a1 }}"
    - name: create debug4
      debug:
        msg: "{
  { cc.a2 }}"

[student@ansible ansible]$ ansible-playbook sb.yml -C

PLAY [vars_files变量] ************************************************************

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

TASK [create debug] ************************************************************
ok: [node1] => {
    "msg": 11
}

TASK [create debug2] ***********************************************************
ok: [node1] => {
    "msg": 22
}

TASK [create debug3] ***********************************************************
ok: [node1] => {
    "msg": "c31"
}

TASK [create debug4] ***********************************************************
ok: [node1] => {
    "msg": "c32"
}

PLAY RECAP *********************************************************************
node1                      : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[student@ansible ansible]$ ls
ansible.cfg  inventory  sb.yml
httpd.yml    roles      var.yml

register注册变量

使⽤register选项,可以将当前task的输出结果赋值给⼀个变量。

[student@ansible ansible]$ vim sb.yml 
---
- name: register注册变量
  hosts: dev
  tasks: 
    - name: shell
      shell: "cat /tmp/zz"
      register: zz
    - name: 创建debug
      debug: 
        var: zz
[student@ansible ansible]$ ansible-playbook sb.yml 
PLAY [register注册变量] ************************************************************

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

TASK [shell] *******************************************************************
changed: [node1]

TASK [创建debug] *****************************************************************
ok: [node1] => {
    "zz": {
        "changed": true,
        "cmd": "cat /tmp/zz",
        "delta": "0:00:00.003963",
        "end": "2022-10-25 23:21:23.533205",
        "failed": false,
        "rc": 0,
        "start": "2022-10-25 23:21:23.529242",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "hello zz",
        "stdout_lines": [
            "hello zz"
        ]
    }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值