ansible学习笔记-变量

一、变量规则

1、变量的名称由字母、下划线和数字组成,但是只能以字母开头
2、变量不能是关键字

二、变量类型

  • 全局变量
  • 剧本变量
  • 资产变量

全局变量

1、传递key=value的形式

root@zsh-virtual-machine:~# ansible all -i localhost, -m debug -a "msg='my name is {{ name }}'" -e "name=alonzo"
[WARNING]: Found variable using reserved name: name
localhost | SUCCESS => {
    "msg": "my name is alonzo"
}

2、使用yaml或者json的形式(都必须是字典格式)

#json形式
root@zsh-virtual-machine:~# cat test.json
{"name":"alonzo","age":"18"}
root@zsh-virtual-machine:~# ansible all -i localhost, -m debug -a "msg='my name is {{ name }}, age is {{ age }}'" -e "@./test.json"
[WARNING]: Found variable using reserved name: name
localhost | SUCCESS => {
    "msg": "my name is alonzo, age is 18"
}
#yaml形式
root@zsh-virtual-machine:~# cat test.yaml
---
name: alonzo
age: 18
root@zsh-virtual-machine:~# ansible all -i localhost, -m debug -a "msg='my name is {{ name }}, age is {{ age }}'" -e "@./test.yaml"
[WARNING]: Found variable using reserved name: name
localhost | SUCCESS => {
    "msg": "my name is alonzo, age is 18"
}

剧本变量

此变量和PlayBook有关,定义在playbook中的,他们的定义方式有多种,这里介绍两种最常见的方式
1、使用key=value的形式

root@zsh-virtual-machine:~# cat test_var.yaml
---
- name: test playbook vars
  hosts: all
  remote_user: root
  vars:
    user: alonzo
    home: /home/alonzo
  tasks:
    - name: create the user {{ user }}
      user:
        name: "{{ user }}"
        home: "{{ home }}"

2、使用yaml或者json的形式

root@zsh-virtual-machine:~# cat test_var1.yaml
---
- name: test playbook vars
  hosts: all
  remote_user: root
  vars_files:
    - /root/users.yaml
  tasks:
    - name: create the user {{ user }}
      user:
        name: "{{ user }}"
        home: "{{ home }}"
root@zsh-virtual-machine:~# cat users.yaml
---
user: alonzo1
home: /home/alonzo1

注意:在playbook中变量的使用需要加双引号,否则会被错误解析成字典

资产变量

1、主机变量

root@zsh-virtual-machine:~# cat hosts
[server]
192.168.100.128 user=alonzo
root@zsh-virtual-machine:~# ansible all -i hosts -m debug -a "msg='name is {{ user }}'"
192.168.100.128 | SUCCESS => {
    "msg": "name is alonzo"
}

2、主机组变量

root@zsh-virtual-machine:~# cat hosts
[servers]
192.168.100.128 user=alonzo
192.168.100.130
[servers:vars]
home="/home/alonzo"
root@zsh-virtual-machine:~# ansible all -i hosts -m "debug" -a "msg='home is {{ home }}'"
192.168.100.128 | SUCCESS => {
    "msg": "home is /home/alonzo"
}
192.168.100.130 | SUCCESS => {
    "msg": "home is /home/alonzo"
}

注意:主机变量的优先级大于主机组变量
3、变量的继承

root@zsh-virtual-machine:~# cat hosts
[servers]
192.168.100.128
192.168.100.130

[allservers]
[allservers:children]
servers

[allservers:vars]
user=alonzo
home="/home/alonzo"
root@zsh-virtual-machine:~# ansible all -i hosts -m "debug" -a "msg='name is {{ user }}, home is {{ home }}'"
192.168.100.128 | SUCCESS => {
    "msg": "name is alonzo, home is /home/alonzo"
}
192.168.100.130 | SUCCESS => {
    "msg": "name is alonzo, home is /home/alonzo"
}

4、Inventory内置变量的说明
内置变量基本上都是以ansible_为前缀

ansible_ssh_host #ssh的主机,要连接的主机与之前设定的主机的别名不同可用此变量
ansible_ssh_port #ssh的端口
ansible_ssh_user #ssh的用户名
ansible_ssh_pass #ssh密码
ansible_sudo_pass #sudo密码
ansible_sudo_exe #sudo命令路径
ansible_ssh_private_key_file #ssh使用的私钥文件
ansible_python_interpreter #目标主机python的路径

Facts变量

手动收集Facts变量

root@zsh-virtual-machine:~# ansible all -i localhost, -c local -m setup
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.100.129",
            "172.17.0.1"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::ab41:46ea:3769:3734",
            "fe80::42:e6ff:fe0c:6607",
            "fe80::ac74:1ff:fe93:c02d"
        ],
        "ansible_apparmor": {
            "status": "enabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "02/27/2020",
        "ansible_bios_version": "6.00",
        "ansible_cmdline": {
            "BOOT_IMAGE": "/boot/vmlinuz-5.4.0-52-generic",
            "quiet": true,
            "ro": true,
            "root": "UUID=2adfa333-b010-4ccd-b3ad-41f5717705ef",
            "splash": true
        },
        "ansible_date_time": {
            "date": "2020-11-04",
            "day": "04",
            "epoch": "1604495082",
            "hour": "21",
            "iso8601": "2020-11-04T13:04:42Z",
            "iso8601_basic": "20201104T210442626327",
            "iso8601_basic_short": "20201104T210442",
            "iso8601_micro": "2020-11-04T13:04:42.626327Z",
            "minute": "04",
            "month": "11",
            "second": "42",
            "time": "21:04:42",
            "tz": "CST",
            "tz_offset": "+0800",
            "weekday": "星期三",
            "weekday_number": "3",
            "weeknumber": "44",
            "year": "2020"
        },
        "ansible_default_ipv4": {
            "address": "192.168.100.129",
            "alias": "ens33",
            "broadcast": "192.168.100.255",
            "gateway": "192.168.100.2",
            "interface": "ens33",
            "macaddress": "00:0c:29:b3:15:4c",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "192.168.100.0",
            "type": "ether"
        },
···
···

过滤Facts变量

  • 获取服务器的内存情况
root@zsh-virtual-machine:~# ansible all -i localhost, -m setup -a "filter=*memory*" -c local
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 809,
                "used": 3099
            },
            "real": {
                "free": 132,
                "total": 3908,
                "used": 3776
            },
            "swap": {
                "cached": 7,
                "free": 1804,
                "total": 2047,
                "used": 243
            }
        },
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
  • 获取服务器的挂载情况
root@zsh-virtual-machine:~# ansible all -i localhost, -m setup -a "filter='*mount*'" -c local
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_mounts": [
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 248,
                "block_used": 248,
                "device": "/dev/loop6",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 472,
                "inode_used": 472,
                "mount": "/snap/snapd/9721",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 32505856,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 443,
                "block_used": 443,
                "device": "/dev/loop0",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 10779,
                "inode_used": 10779,
                "mount": "/snap/core18/1932",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 58064896,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 3,
                "block_used": 3,
                "device": "/dev/loop4",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 230,
                "inode_used": 230,
                "mount": "/snap/gnome-characters/570",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 393216,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 497,
                "block_used": 497,
                "device": "/dev/loop3",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 62342,
                "inode_used": 62342,
                "mount": "/snap/gtk-common-themes/1506",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 65142784,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 18,
                "block_used": 18,
                "device": "/dev/loop8",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 784,
                "inode_used": 784,
                "mount": "/snap/gnome-system-monitor/148",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 2359296,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 1744,
                "block_used": 1744,
                "device": "/dev/loop5",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 18513,
                "inode_used": 18513,
                "mount": "/snap/gnome-3-34-1804/60",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 228589568,
                "uuid": "N/A"
            },
            {
                "block_available": 70483722,
                "block_size": 4096,
                "block_total": 77146320,
                "block_used": 6662598,
                "device": "/dev/sda1",
                "fstype": "ext4",
                "inode_available": 19431972,
                "inode_total": 19660800,
                "inode_used": 228828,
                "mount": "/",
                "options": "rw,relatime,errors=remount-ro",
                "size_available": 288701325312,
                "size_total": 315991326720,
                "uuid": "2adfa333-b010-4ccd-b3ad-41f5717705ef"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 20,
                "block_used": 20,
                "device": "/dev/loop2",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 1384,
                "inode_used": 1384,
                "mount": "/snap/gnome-calculator/826",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 2621440,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 443,
                "block_used": 443,
                "device": "/dev/loop12",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 10775,
                "inode_used": 10775,
                "mount": "/snap/core18/1885",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 58064896,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 240,
                "block_used": 240,
                "device": "/dev/loop11",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 463,
                "inode_used": 463,
                "mount": "/snap/snapd/8542",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 31457280,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 3,
                "block_used": 3,
                "device": "/dev/loop10",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 230,
                "inode_used": 230,
                "mount": "/snap/gnome-characters/550",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 393216,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 2045,
                "block_used": 2045,
                "device": "/dev/loop9",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 24339,
                "inode_used": 24339,
                "mount": "/snap/gnome-3-34-1804/36",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 268042240,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 20,
                "block_used": 20,
                "device": "/dev/loop1",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 1351,
                "inode_used": 1351,
                "mount": "/snap/gnome-calculator/748",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 2621440,
                "uuid": "N/A"
            },
            {
                "block_available": 0,
                "block_size": 131072,
                "block_total": 8,
                "block_used": 8,
                "device": "/dev/loop7",
                "fstype": "squashfs",
                "inode_available": 0,
                "inode_total": 355,
                "inode_used": 355,
                "mount": "/snap/gnome-logs/100",
                "options": "ro,nodev,relatime",
                "size_available": 0,
                "size_total": 1048576,
                "uuid": "N/A"
            }
        ],
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
  • 在playbook中使用Facts变量
root@zsh-virtual-machine:~# cat fact.yaml
---
- name: print facts vars
  hosts: all
  remote_user: root
  tasks:
    - name: print ip
      debug:
        msg: "The default IPV4 is {{ ansible_default_ipv4.address }}"

root@zsh-virtual-machine:~# ansible-playbook -i hosts fact.yaml

PLAY [print facts vars] **********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [192.168.100.130]
ok: [192.168.100.128]

TASK [print ip] ******************************************************************************************************************
ok: [192.168.100.128] => {
    "msg": "The default IPV4 is 192.168.100.128"
}
ok: [192.168.100.130] => {
    "msg": "The default IPV4 is 192.168.100.130"
}

PLAY RECAP ***********************************************************************************************************************
192.168.100.128            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.100.130            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

  • 关闭playbook中facts变量获取
root@zsh-virtual-machine:~# cat fact.yaml
---
- name: print facts vars
  hosts: all
  remote_user: root
  gather_facts: no
  tasks:
    - name: test
      shell: ls /root
root@zsh-virtual-machine:~# ansible-playbook -i hosts fact.yaml

PLAY [print facts vars] **********************************************************************************************************

TASK [test] **********************************************************************************************************************
changed: [192.168.100.130]
changed: [192.168.100.128]

PLAY RECAP ***********************************************************************************************************************
192.168.100.128            : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.100.130            : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

注册变量

用于保存一个task任务的执行结果,以便于做进一步的处理
注册变量在playbook中通过register关键字去实现

root@zsh-virtual-machine:~# cat fact.yaml
---
- name: print facts vars
  hosts: all
  remote_user: root
  gather_facts: no
  tasks:
    - name: test
      shell: ls /root
      register: check
    - name: print check
      debug: var=check.stdout
root@zsh-virtual-machine:~# ansible-playbook -i hosts fact.yaml

PLAY [print facts vars] **********************************************************************************************************

TASK [test] **********************************************************************************************************************
changed: [192.168.100.130]
changed: [192.168.100.128]

TASK [print check] ***************************************************************************************************************
ok: [192.168.100.128] => {
    "check.stdout": "aaa.txt\ninstall_docker.sh"
}
ok: [192.168.100.130] => {
    "check.stdout": "aaa.txt"
}

PLAY RECAP ***********************************************************************************************************************
192.168.100.128            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
192.168.100.130            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

变量优先级

全局变量 > 剧本变量 > 资产变量

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,324评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,356评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,328评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,147评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,160评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,115评论 1 296
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,025评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,867评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,307评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,528评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,688评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,409评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,001评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,657评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,811评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,685评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,573评论 2 353