[Ansible系列⑥]Ansible变量详解

一.    简介

         ansible支持变量,用于存储会在整个项目中重复使用到的一些值。以简化项目的创建与维护,降低出错的机率。

变量的定义:

  • 变量名应该由字母、数字下划数组成
  • 变量名必须以字母开头
  • ansible内置关键字不能作为变量名

 二.    Inventory中定义变量

2.1    定义主机变量

 定义方式:在inventory清单中,主机名称或主机ip后面 写定义变量。

使用范围:对应的主机才能使用该变量

inventory清单文件:
[root@clinet test1]# cat hosts 
[web]
10.10.10.135 ansible_ssh_port=22 state=master
10.10.10.134 ansible_ssh_port=22 state=backup



yaml文件
[root@clinet test1]# cat vars.yml 
- hosts: web
  
  tasks:
    - name: debug message..
      debug: 
        msg: 
        - '{
   
   { ansible_ssh_port }}'
        - '{
   
   { state }}'

执行结果:

[root@clinet test1]# ansible-playbook vars.yml 
PLAY [web] ***********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [10.10.10.134]
ok: [10.10.10.135]

TASK [debug message..] ***********************************************************************************************
ok: [10.10.10.135] => {
    "msg": [
        22, 
        "master"
    ]
}
ok: [10.10.10.134] => {
    "msg": [
        22, 
        "backup"
    ]
}

PLAY RECAP ***********************************************************************************************************
10.10.10.134               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.135               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet test1]# 

2.2    定义主机组变量

 定义方式:[组名:vars]后面接定义的变量

使用访问:组内的主机都可以使用该变量

inventory清单文件:
[root@clinet test1]# cat hosts 
[db]
10.10.10.135
10.10.10.136

[db:vars]
ntp_server=aliyun1.ntp.com
proxy=nginx_proxy


yaml文件
[root@clinet test1]# cat vars.yml 
- hosts: db
  
  tasks:
    - name: debug message..
      debug: 
        msg: 
        - '{
   
   { ntp_server }}'
        - '{
   
   { proxy }}'

[root@clinet test1]# ansible-playbook vars.yml 

PLAY [db] ************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [10.10.10.135]
ok: [10.10.10.136]

TASK [debug message..] ***********************************************************************************************
ok: [10.10.10.135] => {
    "msg": [
        "aliyun1.ntp.com", 
        "nginx_proxy"
    ]
}
ok: [10.10.10.136] => {
    "msg": [
        "aliyun1.ntp.com", 
        "nginx_proxy"
    ]
}

PLAY RECAP ***********************************************************************************************************
10.10.10.135               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.136               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet test1]# 

 三.    在Playbook中定义变量

 3.1    vars 关键字定义

定义方式: 在playbook中通过vars关键字定义变量。

使用范围:该playbook范围中。

inventory清单文件:
[root@clinet test1]# cat hosts 
[db]
10.10.10.135
10.10.10.136


yaml文件
[root@clinet test1]# cat vars.yml 
- hosts: db
  vars:
    http_port: 80
    dns_port: 53
  
  tasks:
    - name: debug message..
      debug: 
        msg: 
        - '{
   
   { http_port }}'
        - '{
   
   { dns_port }}'

执行结果:
[root@clinet test1]# ansible-playbook vars.yml 
PLAY [db] ************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [10.10.10.135]
ok: [10.10.10.136]

TASK [debug message..] ***********************************************************************************************
ok: [10.10.10.135] => {
    "msg": [
        80, 
        53
    ]
}
ok: [10.10.10.136] => {
    "msg": [
        80, 
        53
    ]
}

PLAY RECAP ***********************************************************************************************************
10.10.10.135               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.136               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet test1]#

3.2    通过vars_files关键字引入变量文件

 方法一:

##定义vars_files
[root@clinet test1]# cat vars_file.yml
users:
  bjones:
    first_name: bob
    last_name: jons
    home_dirs: /users/bjones
  
  acook:
    first_name: Anne
    last_name: Cook
    home_dirs: /users/accok


##yml文件
[root@clinet test1]# cat vars.yml
- hosts: db
  vars_files:
    - /root/ansibel-test/test1/vars_file.yml                      ##最好为绝对路径
  
  tasks:
    - name: debug message..
      debug:
        msg: 
          - '{
   
   { users["bjones"] ["first_name"]}}'               ##主要两个[]之间有个空格

四.    host_vars和group_vars定义变量

 4.1    host_vars定义变量

        在项目目录中创建host_vars目录,然后创建文件,文件的名称要与inventory清单中的主机名称保持一致,如果是ip地址,则创建相同ip地址的文件即可。

定义host_vars变量
[root@clinet test1]# cat host_vars/10.10.10.134 
hostname: Route
ip_add: 10.10.10.134
[root@clinet test1]# cat host_vars/10.10.10.135
hostname: lvs_1
ip_add: 10.10.10.135
[root@clinet test1]#


编写yml文件
[root@clinet test1]# 
[root@clinet test1]# cat vars.yml 
- hosts: web
  
  tasks:
    - name: debug message..
      debug:
        msg: 
          - '{
   
   { hostname }}'
          - '{
   
   { ip_add }}'

[root@clinet test1]# 

执行结果
[root@clinet test1]# ansible-playbook vars.yml 
PLAY [web] ***********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [10.10.10.134]
ok: [10.10.10.135]

TASK [debug message..] ***********************************************************************************************
ok: [10.10.10.135] => {
    "msg": [
        "lvs_1", 
        "10.10.10.135"
    ]
}
ok: [10.10.10.134] => {
    "msg": [
        "Route", 
        "10.10.10.134"
    ]
}

PLAY RECAP ***********************************************************************************************************
10.10.10.134               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.135               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet test1]#

 4.2    定义group_vars变量

         在项目目录中创建group_vars目录,然后创建一个文件,文件的文件名称要与inventory清单中的组名称保持一致。

定义group_vars
[root@clinet test1]# cat group_vars/web 
group_name: web_group
[root@clinet test1]# cat group_vars/db 
group_name: db_group
[root@clinet test1]#


编写yml文件
[root@clinet test1]# cat vars.yml 
- hosts: db:web
  
  tasks:
    - name: debug message..
      debug:
        msg: 
          - '{
   
   { group_name }}'

[root@clinet test1]# 


执行结果
[root@clinet test1]# ansible-playbook vars.yml 
PLAY [db:web] ********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [10.10.10.134]
ok: [10.10.10.135]
ok: [10.10.10.136]

TASK [debug message..] ***********************************************************************************************
ok: [10.10.10.135] => {
    "msg": [
        "web_group"
    ]
}
ok: [10.10.10.134] => {
    "msg": [
        "web_group"
    ]
}
ok: [10.10.10.136] => {
    "msg": [
        "db_group"
    ]
}

PLAY RECAP ***********************************************************************************************************
10.10.10.134               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.135               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.136               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet test1]#

 变量优先级总结:

命令行变量-->playbook中vars_files-->paybook中vars-->inventory中hosts-->host_vars-->group_vars-->group_vars/all --->inventory中group

 五.    register变量

         register关键字可以将某个task任务执行的结果存储在变量中,最后使用debug输出内容,可以用于后续的故障排查。

示例1:

[root@clinet test1]# cat vars.yml 
- hosts: db:web
  
  tasks:
    - name: debug message..
      shell: netstat -ntpl
      register: system_info

    - name: get_info
      debug:
        msg:
          - '{
   
   { system_info}}'

[root@clinet test1]# 

执行结果:

[root@clinet test1]# ansible-playbook vars.yml
PLAY [db:web] ********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [10.10.10.134]
ok: [10.10.10.135]
ok: [10.10.10.136]

TASK [debug message..] ***********************************************************************************************
changed: [10.10.10.134]
changed: [10.10.10.135]
changed: [10.10.10.136]

TASK [get_info] ******************************************************************************************************
ok: [10.10.10.135] => {
    "msg": [
        {
            "changed": true, 
            "cmd": "netstat -ntpl", 
            "delta": "0:00:00.009932", 
            "end": "2022-10-28 16:38:44.957768", 
            "failed": false, 
            "rc": 0, 
            "start": "2022-10-28 16:38:44.947836", 
            "stderr": "", 
            "stderr_lines": [], 
            "stdout": "Active Internet connections (only servers)\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    \ntcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1523/sshd           \ntcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1702/master         \ntcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      957/rpcbind         \ntcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1558/nginx: master  \ntcp6       0      0 :::22                   :::*                    LISTEN      1523/sshd           \ntcp6       0      0 ::1:25                  :::*                    LISTEN      1702/master         \ntcp6       0      0 :::111                  :::*                    LISTEN      957/rpcbind         \ntcp6       0      0 :::80                   :::*                    LISTEN      1558/nginx: master  ", 
            "stdout_lines": [
                "Active Internet connections (only servers)", 
                "Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    ", 
                "tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1523/sshd           ", 
                "tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1702/master         ", 
                "tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      957/rpcbind         ", 
                "tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1558/nginx: master  ", 
                "tcp6       0      0 :::22                   :::*                    LISTEN      1523/sshd           ", 
                "tcp6       0      0 ::1:25                  :::*                    LISTEN      1702/master         ", 
                "tcp6       0      0 :::111                  :::*                    LISTEN      957/rpcbind         ", 
                "tcp6       0      0 :::80                   :::*                    LISTEN      1558/nginx: master  "
            ]
        }
    ]
}
ok: [10.10.10.136] => {
    "msg": [
        {
            "changed": true, 
            "cmd": "netstat -ntpl", 
            "delta": "0:00:00.021772", 
            "end": "2022-10-28 16:38:45.896962", 
            "failed": false, 
            "rc": 0, 
            "start": "2022-10-28 16:38:45.875190", 
            "stderr": "", 
            "stderr_lines": [], 
            "stdout": "Active Internet connections (only servers)\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    \ntcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      997/rpcbind         \ntcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1533/sshd           \ntcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1716/master         \ntcp6       0      0 :::111                  :::*                    LISTEN      997/rpcbind         \ntcp6       0      0 :::22                   :::*                    LISTEN      1533/sshd           \ntcp6       0      0 ::1:25                  :::*                    LISTEN      1716/master         ", 
            "stdout_lines": [
                "Active Internet connections (only servers)", 
                "Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    ", 
                "tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      997/rpcbind         ", 
                "tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1533/sshd           ", 
                "tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1716/master         ", 
                "tcp6       0      0 :::111                  :::*                    LISTEN      997/rpcbind         ", 
                "tcp6       0      0 :::22                   :::*                    LISTEN      1533/sshd           ", 
                "tcp6       0      0 ::1:25                  :::*                    LISTEN      1716/master         "
            ]
        }
    ]
}
ok: [10.10.10.134] => {
    "msg": [
        {
            "changed": true, 
            "cmd": "netstat -ntpl", 
            "delta": "0:00:00.008834", 
            "end": "2022-10-28 16:38:45.406496", 
            "failed": false, 
            "rc": 0, 
            "start": "2022-10-28 16:38:45.397662", 
            "stderr": "", 
            "stderr_lines": [], 
            "stdout": "Active Internet connections (only servers)\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    \ntcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1557/nginx: master  \ntcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1525/sshd           \ntcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1117/rpcbind        \ntcp6       0      0 :::80                   :::*                    LISTEN      1557/nginx: master  \ntcp6       0      0 :::22                   :::*                    LISTEN      1525/sshd           \ntcp6       0      0 ::1:25                  :::*                    LISTEN      1701/master         \ntcp6       0      0 :::111                  :::*                    LISTEN      1117/rpcbind        ", 
            "stdout_lines": [
                "Active Internet connections (only servers)", 
                "Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    ", 
                "tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1557/nginx: master  ", 
                "tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1525/sshd           ", 
                "tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1117/rpcbind        ", 
                "tcp6       0      0 :::80                   :::*                    LISTEN      1557/nginx: master  ", 
                "tcp6       0      0 :::22                   :::*                    LISTEN      1525/sshd           ", 
                "tcp6       0      0 ::1:25                  :::*                    LISTEN      1701/master         ", 
                "tcp6       0      0 :::111                  :::*                    LISTEN      1117/rpcbind        "
            ]
        }
    ]
}

PLAY RECAP ***********************************************************************************************************
10.10.10.134               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.135               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.136               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet test1]# 

也可以只打印register变量中的子变量

[root@clinet test1]# cat vars.yml 
- hosts: db:web
  
  tasks:
    - name: debug message..
      shell: netstat -ntpl
      register: system_info

    - name: get_info
      debug:
        msg:
          - '{
   
   { system_info.start   }}'

    - name: get_info2
      debug:
        msg:
          - '{
   
   { system_info.stdout   }}'

[root@clinet test1]# 


结果
[root@clinet test1]# ansible-playbook vars.yml
PLAY [db:web] ********************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [10.10.10.134]
ok: [10.10.10.135]
ok: [10.10.10.136]

TASK [debug message..] ***********************************************************************************************
changed: [10.10.10.135]
changed: [10.10.10.134]
changed: [10.10.10.136]

TASK [get_info] ******************************************************************************************************
ok: [10.10.10.135] => {
    "msg": [
        "2022-10-28 16:47:06.690457"
    ]
}
ok: [10.10.10.136] => {
    "msg": [
        "2022-10-28 16:47:07.616910"
    ]
}
ok: [10.10.10.134] => {
    "msg": [
        "2022-10-28 16:47:07.158014"
    ]
}

TASK [get_info2] *****************************************************************************************************
ok: [10.10.10.135] => {
    "msg": [
        "Active Internet connections (only servers)\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    \ntcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1523/sshd           \ntcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1702/master         \ntcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      957/rpcbind         \ntcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1558/nginx: master  \ntcp6       0      0 :::22                   :::*                    LISTEN      1523/sshd           \ntcp6       0      0 ::1:25                  :::*                    LISTEN      1702/master         \ntcp6       0      0 :::111                  :::*                    LISTEN      957/rpcbind         \ntcp6       0      0 :::80                   :::*                    LISTEN      1558/nginx: master  "
    ]
}
ok: [10.10.10.136] => {
    "msg": [
        "Active Internet connections (only servers)\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    \ntcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      997/rpcbind         \ntcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1533/sshd           \ntcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1716/master         \ntcp6       0      0 :::111                  :::*                    LISTEN      997/rpcbind         \ntcp6       0      0 :::22                   :::*                    LISTEN      1533/sshd           \ntcp6       0      0 ::1:25                  :::*                    LISTEN      1716/master         "
    ]
}
ok: [10.10.10.134] => {
    "msg": [
        "Active Internet connections (only servers)\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    \ntcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1557/nginx: master  \ntcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1525/sshd           \ntcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1117/rpcbind        \ntcp6       0      0 :::80                   :::*                    LISTEN      1557/nginx: master  \ntcp6       0      0 :::22                   :::*                    LISTEN      1525/sshd           \ntcp6       0      0 ::1:25                  :::*                    LISTEN      1701/master         \ntcp6       0      0 :::111                  :::*                    LISTEN      1117/rpcbind        "
    ]
}

PLAY RECAP ***********************************************************************************************************
10.10.10.134               : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.135               : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.10.10.136               : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet test1]# 

结束语:

        本章重点要理解各种变量的定义方法,以及变量的使用优先级,在后续大型的项目playbook中使用的非常频繁;同时register变量也需要理解,有助于后续的排查故障,可特定的playbook的语法使用,比如接收register的自变量结果之后通过when去判断等等。

猜你喜欢

转载自blog.csdn.net/qq_43714097/article/details/127403819