zoukankan      html  css  js  c++  java
  • ansible笔记(12):变量(一)

    1.定义变量规范

      先说说怎样定义变量,变量名应该由字母、数字、下划线组成,变量名需要以字母开头,ansible内置的关键字不能作为变量名。

    2.定义变量,使用关键字:vars

      定义变量用vars,调用变量用{{ 变量名 }}

    ---
      - hosts: 192.168.10.2
        remote_user: root
        vars:
          testvar1: testfile
        tasks:
        - name: test for var
          file:
            path: /test/{{testvar2}}
            state: touch
    

      定义多个变量,如下操作:

        vars:
          testvar1: testfile1
          testvar2: testfile2
    

      或者使用YAML的块序列语法定义,等效,如下操作:

         vars:
    - testvar1: testfile1 - testvar2: testfile2  

    关于nginx的一个案例:  

      使用类似“属性”的方法来定义变量,这样可以更加清晰地分辨出所设置的变量是属于谁的,也方便在使用的时候调用相应的变量。

    ---
      - hosts: 192.168.10.2
        remote_user: root
        vars:
          nginx:
            conf80: /etc/nginx/conf.d/80.conf
            conf8080: /etc/nginx/conf.d/8080.conf
        tasks:
        - name: nginx configuration
          file:
            path: "{{nginx.conf80}}"
            state: touch
        - name: nginx configuration
          file:
            path: "{{nginx.conf8080}}"
            state: touch
    

     调用变量时可用的两种语法:

    语法一:   "{{nginx.conf80}}"
    
    语法二:   "{{nginx['conf8080']}}"
    

     注意:关于是否在调用变量的时候使用双引号(" ")的问题:当调用变量直接被放在开头位置时候需要使用双引号,例如:path: "{{nginx.conf80}}"

        当调用变量时不在开头,则不需要使用双引号,例如:path: /test/{{testvar2}}

      前文中有描述过,当在playbook中为模块的参数赋值时,可以使用“冒号”,也可以使用“等号”,当使用“等号”为模块的参数赋值时,则不用考虑引用变量时是否使用“引号”的问题,示例如下:

    ---
      - hosts: 192.168.10.2
        remote_user: root
        vars:
          nginx:
            conf80: /etc/nginx/conf.d/80.conf
            conf8080: /etc/nginx/conf.d/8080.conf
        tasks:
        - name: nginx configuration
          file:
            path={{nginx.conf80}}
            state=touch
        - name: nginx configuration
          file:
            path={{nginx.conf8080}}
            state=touch
    

      

    3.变量文件分离,使用vars_file参数

      我们不在剧本中写入并调用变量,采用单独将变量写入一个文档里,实现变量文件分离,当我们需要调用某个变量文件中的变量时,只需要使用vars_file参数进行调用,被调用变量文件的路径前面要加上"-",横杠。

    ---
      - hosts: 192.168.10.2
        remote_user: root
        vars_files:
        - /playbook/nginx_vars.yml
        tasks:
        - name: nginx configuration
          file:
            path: "{{nginx.conf80}}"
            state: touch
        - name: nginx configuration
          file:
            path: "{{nginx.conf8080}}"
            state: touch  

    注意:vars关键字和vars_file关键字可以同时使用,示例如下:

        vars:
          conf90: /etc/nginx/conf.d/9090.conf
        vars_files:
        - /playbook/nginx_vars.yml
    

      

  • 相关阅读:
    Exchange 2013与 Office Web Apps 整合
    SharePoint2013 以其他用户登录和修改AD域用户密码 功能
    sharepoint 2010 自定义页面布局
    sharepoint 2010 记录管理 对象模型
    SharePoint2010 对象模型 关联列表
    在SharePoint Server 2010中更改“我的网站”
    xenapp 6.5 客户端插件第一次安装总是跳到官网
    如何解决在Windows Server 2008 R2 上安装证书服务重启后出现 CertificationAuthority 91错误事件
    在Win7 Hyper-v虚拟机中挂接真实机的声卡
    win8 中如何删除 共享文件夹 用户名和密码
  • 原文地址:https://www.cnblogs.com/python-wen/p/11492189.html
Copyright © 2011-2022 走看看