ansible5:ansible变量

变量简介

  变量可以让我们的工作变的更加快捷高效,比如shell脚本使用变量就可以省去很多重复调用,ansible亦是如此。变量名由字母、数字、下划线组成,且需要以字母开头,ansible内置的关键字不能作为变量名,举例理解ansible的变量:

  1. 举例1:

      0 16:38:34 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test8.yaml
    ---
    - hosts: ck-node1
      vars:
        testvar: tom
      tasks:
        - name: task1
          file: path=/root/{{testvar}} state=touch
      0 16:38:37 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test8.yaml
    

变量定义

  1. 变量的定义方式一:

    vars:
      var1: tom		# 或者用var1=tom也可以。
      var2: info
    
  2. 变量的定义方式二:

    vars:
      - var1: tom
      - var2: info
    
  3. 变量的定义方式三:

      0 16:49:35 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test9.yaml
    ---
    - hosts: ck-node1
      vars:
        multivars:
          var1: zoe
          var2: bob
      tasks:
        - name: task1
          file: path=/root/{{multivars.var1}} state=touch
        - name: task2
          file: path=/root/{{multivars.var2}} state=touch
    PS:此定义方式中我们为模块参数赋值使用的是“=”,如果使用的是“:”且变量位于值开头时,需要给变量加双引号,否则会报语法错误,如:path: "{{multivars.var1}}"
    
  4. 变量的定义方式四:在文件中定义变量,然后在playbook中引入变量文件。

      0 17:19:01 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat variables.yaml
    multivars:
      var1: jane
      var2: selina
      0 17:19:03 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test10.yaml 
    ---
    - hosts: ck-node1
      vars_files: /server/ops_ansible/variables.yaml
      tasks:
        - name: task1
          file: path=/root/{{multivars.var1}} state=touch
        - name: task2
          file: path=/root/{{multivars.var2}} state=touch
    # 如果引入多个变量文件:
    vars_files:
      - /server/ops_ansible/variables.yaml
      - /server/ops_ansible/variables2.yaml
    
  5. 变量的定义方式五:交互式定义变量,类似于shell中的”read -p“。

      0 17:37:40 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test11.yaml
    ---
    - hosts: ck-node1
      vars_prompt:
        - name: user_name
          prompt: "Please enter username"
          private: no					# 默认情况,执行playbook时用户填入信息是不显示的,加上此选项可以显示出来。
        - name: user_password
          prompt: "Please enter user passwd"
          encrypt: "sha512_crypt"		# user模块的密码需要加密才能有效,可能需要安装的依赖包:pip3 install passlib
          confirm: yes					# 添加确认密码的功能。
      tasks:
        - name: creater user
          user: name={{user_name}} password={{user_password}}
      0 17:37:42 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test11.yaml
    Please enter username: onion
    Please enter user passwd: 
    confirm Please enter user passwd:
    
  6. 变量的定义方式六:在清单中配置主机信息的同时配置变量,当操作这个主机时,可直接使用对应的变量,这种变量称为主机变量

      0 17:40:25 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat /etc/ansible/hosts
    ck-node1	var1=nuanshu
    ck-node2
    ck-node3
      0 17:40:26 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible ck-node1 -m shell -a 'echo {{var1}}'
    ck-node1 | CHANGED | rc=0 >>
    nuanshu
    
  7. 变量的定义方式七:为清单的主机组配置变量,变量针对主机组生效,这种变量称为主机组变量。

      0 17:43:51 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat /etc/ansible/hosts
    [dev]
    ck-node1
    ck-node2
    
    [fat]
    ck-node3
    
    [dev:vars]
    var1=baiye
      0 17:43:53 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible ck-node1 -m shell -a 'echo {{var1}}'
    ck-node1 | CHANGED | rc=0 >>
    baiye
      0 17:43:56 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible ck-node2 -m shell -a 'echo {{var1}}'
    ck-node2 | CHANGED | rc=0 >>
    baiye
    
  8. 变量的定义方式八:使用set_fact模块定义变量。

      0 17:49:08 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test12.yaml
    ---
    - hosts: ck-node1
      tasks:
      - name: define var
        set_fact: var1=jingqing
      - name: use var
        debug: msg={{var1}}
      0 17:49:09 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test12.yaml
    # 也可以将变量值赋值给新的变量。
      0 18:15:01 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test13.yaml
    ---
    - hosts: ck-node1
      vars:
        var1: pingan
      tasks:
        - shell: echo ningyao
          register: var2
        - set_fact: var3={{var1}} var4={{var2.stdout}}
        - debug: msg="{{var3}} {{var4}}"
      0 18:15:13 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test13.yaml
    
  9. 变量的定义方式九:通过命令行定义变量。当命令行和playbook中都定义变量时,以命令行定义的为准。

      0 18:18:54 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test14.yaml
    ---
    - hosts: ck-node1
      tasks:
        - name: 
          debug: msg={{var1}}
      0 18:18:57 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook -e "var1=changming" test14.yaml
    # 传入多个变量用空格隔开。
      0 18:30:22 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test15.yaml
    ---
    - hosts: ck-node1
      tasks:
        - name: 
          debug: msg="{{var1}} {{var2}}"
      0 18:30:26 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook -e 'var1=changming var2=zhulian' test15.yaml
    # 传入的变量支持支持key=value的方式,也支持json格式传入变量。如:-e '{"ver1":"lala","var2":"haha"}'
    # 命令行也可以传入变量文件,文件需要时yaml或者json格式,-e参数指定文件时需要加@。
      0 18:36:29 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat variables.yaml 
    var1: jane
    var2: selina
      0 18:36:31 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook -e '@/server/ops_ansible/variables.yaml' test16.yaml
    
  10. 变量的定义方式十:ansible的任务执行完是有返回值的,只是默认不显示,我们可以把返回值注册到变量上,通过调用变量查看返回值,也可将返回值用在其它地方。

      0 17:08:24 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test17.yaml
    ---
    - hosts: ck-node1
      tasks:
        - name: test shell
          shell: echo 'wula'
          register: var1
        - name: reveive return value
          debug:
            var: var1	# 如果只是想看到ansible返回的某部分信息,可以使用msg指定key调用,如:msg: "{{var1.stdout}}"或者msg: "{{var1['stdout']}}"
      0 17:08:26 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test17.yaml
    ...
    TASK [reveive return value] **********************************************************************************
    ok: [ck-node1] => {
        "var1": {
            "changed": true, 
            "cmd": "echo 'wula'", 
            "delta": "0:00:00.007907", 
            "end": "2021-10-21 17:14:05.471278", 
            "failed": false, 
            "rc": 0, 
            "start": "2021-10-21 17:14:05.463371", 
            "stderr": "", 
            "stderr_lines": [], 
            "stdout": "wula", 
            "stdout_lines": [
                "wula"
            ]
        }
    }
    ...
    

其它说明

includ_vars

  1. 使用“includ_vars“关键字重新加载变量文件。

      0 18:37:15 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat variables.yaml 
    var1: jane
    var2: selina
      0 18:37:18 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test18.yaml 
    ---
    - hosts: ck-master
      vars_files: /server/ops_ansible/variables.yaml
      tasks:
      - name: task1
        debug:
          msg: "{{var1}},{{var2}}"
      - name: task2
        lineinfile:
          path: /server/ops_ansible/variables.yaml
          line: "var3: maria"
      - name: task3
        debug:
          msg: "{{var3}}"
      0 18:37:23 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test18.yaml
    ...
    fatal: [ck-master]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'var3' is undefined
    ...
    # 执行报错提示var3变量未定义,这是因为playbook在执行过程中不会动态实时刷新变量文件内容,我们需要借助“includ_vars”关键字重新加载一下变量文件。
      0 18:38:54 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat variables.yaml 
    var1: jane
    var2: selina
      0 18:38:56 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test18.yaml 
    ---
    - hosts: ck-master
      vars_files: /server/ops_ansible/variables.yaml
      tasks:
      - name: task1
        debug:
          msg: "{{var1}},{{var2}}"
      - name: task2
        lineinfile:
          path: /server/ops_ansible/variables.yaml
          line: "var3: maria"
      - include_vars: /server/ops_ansible/variables.yaml
      - name: task3
        debug:
          msg: "{{var3}}"
      0 18:39:00 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test18.yaml
      0 18:39:13 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat variables.yaml 
    var1: jane
    var2: selina
    var3: maria
    
  2. 使用“include_vars”来加载变量文件。

      0 18:43:42 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat variables.yaml 
    var1: jane
    var2: selina
    var3: maria
      0 18:43:45 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test19.yaml 
    ---
    - hosts: ck-master
      vars_files: /server/ops_ansible/variables.yaml
      tasks:
      - name: load vars file
        include_vars: 
          file: /server/ops_ansible/variables.yaml
      - name: task1
        debug:
          msg: "{{var1}}"
      0 18:43:48 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test19.yaml
    # 上面的include_vars也能写作一行,此种形式只能加载变量文件。
      - name: load vars file
        include_vars: /server/ops_ansible/variables.yaml
    
  3. 使用“include_vars”来加载变量目录。默认情况下,变量目录中所有的文件都必须以'.yaml' 、'.yml' 、'.json'后缀结尾,目录中的子目录也要遵循这个要求,否则会报错。如果想要使用其它后缀的变量文件,需要用extensions参数去指定。

      0 19:38:22 root@ck-ansible,172.16.2.9:/server/ops_ansible # ll vars/
    总用量 16
    -rw-r--r-- 1 root root 24 10月 21 19:17 var.yaml
    -rw-r--r-- 1 root root 20 10月 21 19:20 vas.txt
    -rw-r--r-- 1 root root 25 10月 21 19:21 vat.yml
    -rw-r--r-- 1 root root 25 10月 21 19:36 vau.log
      0 19:38:26 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat vars/var.yaml 
    var1: jane
    var2: selina
      0 19:38:31 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat vars/vas.txt 
    var3: zoe
    var4: bob
      0 19:38:35 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat vars/vat.yml 
    var5: marina
    var6: baiye
      0 19:38:38 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat vars/vau.log 
    var7: nuanshu
    var8: mili
      0 19:38:58 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test20.yaml 
    ---
    - hosts: ck-master
      tasks:
      - name: load vars directory
        include_vars: 
          dir: /server/ops_ansible/vars			# dir参数与file参数不能同事使用。
          #depth: 1											# 默认递归所有,此参数可以控制递归层数。
          extensions: ["yaml","yml","json","txt","log"]		# 指定变量合规的后缀,参数值需要是一个列表。
          #files_matching: "^var*"							# 使用正则匹配想要加载的变量文件。
          #ignore_files: ["^vas*","*.log"]					# 使用正则忽略不想要加载的变量文件,参数值需要是一个列表。
      - debug:
          msg: "{{var2}},{{var8}}"
      0 19:39:01 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test20.yaml
    
  4. 在ansible 2.4以后的版本中,执行“include_vars”模块会将载入的变量文件列表写入到自己的返回值中,可以通过调用关键字取得相关信息。

    # 调用关键字'ansible_included_var_files'来显示所加载的变量文件。
      0 19:50:38 root@ck-ansible,172.16.2.9:/server/ops_ansible # ll vars/
    总用量 8
    -rw-r--r-- 1 root root 24 10月 21 19:17 var.yaml
    -rw-r--r-- 1 root root 25 10月 21 19:21 vat.yml
      0 19:50:42 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test21.yaml 
    ---
    - hosts: ck-master
      tasks:
      - name: load vars directory
        include_vars: 
          dir: /server/ops_ansible/vars
        register: return_var
      - debug:
          msg: "{{return_var.ansible_included_var_files}}"
      0 19:50:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test21.yaml
    # 调用关键字'ansible_facts'来显示所加载的变量。
    ...
          msg: "{{return_var.ansible_facts}}"
    ...
    

内置变量

  1. ansible_version:获取ansible版本信息。

      0 20:11:23 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible ck-node1 -m debug -a "msg={{ansible_version}}"
    
  2. hostvars:获取远程主机的相关信息。

      0 10:03:21 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible ck-node1 -m debug -a "msg={{hostvars}}
    # 在ck-node2主机上查看ck-node1的网卡信息。
      0 10:12:33 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test22.yaml
    ---
    - name: task1
      hosts: ck-node1		# 写这两行是需要先收集node1的信息,然后给下面的node2调用。
    
    - name: task1
      hosts: ck-node2
      tasks:
      - debug:
          msg: "{{hostvars['ck-node1'].ansible_eth0.ipv4}}"
      0 10:12:42 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test22.yaml
    # 在ck-node2主机中调用ck-node1的注册变量。
      0 10:16:36 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test23.yaml
    ---
    - hosts: ck-node1
      tasks:
      - shell: echo 'wula'
        register: shellreturn
    
    - hosts: ck-node2
      tasks:
      - debug:
          msg: "{{hostvars['ck-node1'].shellreturn.stdout}}"
      0 10:16:39 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test23.yaml
    
  3. inventory_hostname:通过此变量可以获取到被操作主机的主机名(不是linux主机名,是配置清单中配置的主机名)。

      0 10:20:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat /etc/ansible/hosts
    [dev]
    miyu		ansible_host=172.16.15.21
    baiye		ansible_host=172.16.15.21
      0 10:20:57 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible dev -m debug -a 'msg={{inventory_hostname}}'
    
  4. groups:获取清单的分组信息。

      0 10:30:36 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat /etc/ansible/hosts
    [dev]
    ck-master
    
    [fat]
    ck-node1
    ck-node2
    
    [uat]
    ck-node3
      0 10:30:45 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible ck-node1 -m debug -a 'msg={{groups}}'
    # 查看某个分组
      0 10:30:46 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible ck-node1 -m debug -a 'msg={{groups.uat}}'
    
  5. group_names:获取当前主机所在分组的组名。

      0 10:31:58 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat /etc/ansible/hosts
    [dev]
    ck-master
    
    [fat]
    ck-node1
    ck-node2
    
    [uat]
    ck-node3
      0 10:32:06 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible ck-node1 -m debug -a 'msg={{group_names}}'
    
  6. inventory_dir:获取配置清单的存放路径。

      0 10:32:08 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible ck-node1 -m debug -a 'msg={{inventory_dir}}'
    


写作不易,转载请注明出处,谢谢~~

posted @ 2021-11-04 16:00  loosenc  阅读(426)  评论(0编辑  收藏  举报