对于重复使用而且庞大的参数值,可以界说变量引用,便于管理和修改,也可以建立文件存放变量,引用变量文件即可

示例如下

[root@jack7-1 ansible]# tree .
.
├── ansible.cfg
├── backup
│   └── backup.sh
├── hosts
├── roles
├── vars ============>存放变量的目录
│   └── httpd.yml =========>存放变量的文件
└── work ===========================>事情目录
├── apache_config_1.yml
├── apache_config.yml
├── apache_create.yml
├── apache_remove.yml
变量写法要主要花样和缩进
[root@jack7-1 ansible]# cat vars/httpd.yml
#apache vars
apache:
conf: /etc/httpd/conf/httpd.conf
vhost: /etc/httpd/vhost
引用方式:
"{{apache.conf}}"

如下方式建立apache的虚拟主机:

需要注重的是对统一文件多次blockin时,marker很主要,否则会笼罩上次插入的内容

[root@jack7-1 ansible]# cat work/apache_config.yml


  • hosts: jack6-2
    remote_user: root
    vars_files:

    • /etc/ansible/vars/httpd.yml
      tasks:
    • name: insert line
      blockinfile:
      path: "{{apache.conf}}"
      block: "Listen 8888\nInclude vhost/*.conf"
      insertafter: EOF
      marker: "#{mark} 8888"
    • name: cat
      shell: grep ^Include {{apache.conf}}
    • name: DocumentRoot
      file:
      path: "{{apache.droot}}"
      state: directory
    • name: insert block
      blockinfile:
      path: "{{apache.vconf}}"
      block: "<VirtualHost *:8888>\n\tDocumentRoot /data/www/html\n\tServerName myvhost.com\n\t<Directory /data/www/html>\n\t\tAllowOverride None\n\t\tRequire all granted\n\t\tSatisfy Any\n\t\tOrder allow,deny\n\t\tAllow from all\n\t</Directory>\n</VirtualHost>"
      insertbefore: BOF
      backup: yes
      notify: reload_httpd

    • meta: flush_handlers
    • name: insert content
      blockinfile:
      path: "{{apache.vhtml}}"
      block: "Hello my bro! Welcome to My Website."
      insertbefore: BOF
      create: yes
      marker: "#{mark} WHAT FCK"
      backup: yes

    handlers:

    • name: reload_httpd
      service:
      name: httpd
      state: reloaded

###########################################################################
当我们需要使用testvar1的变量值时,则需要引用这个变量,如你所见,使用"{{变量名}}"可以引用对应的变量。

也可以界说多个变量,示例如下。

Shell
vars:
testvar1: testfile
testvar2: testfile2
1
2
3
vars:
testvar1: testfile
testvar2: testfile2

除了使用上述语法,使用YAML的块序列语法也可以界说变量,示例如下

Shell
vars:

  • testvar1: testfile
  • testvar2: testfile2
    1
    2
    3
    vars:
  • testvar1: testfile
  • testvar2: testfile2

在界说变量时,还能够以类似"属性"的方式界说变量,示例如下


  • hosts: test70
    remote_user: root
    vars:
    nginx:
    conf80: /etc/nginx/conf.d/80.conf
    conf8080: /etc/nginx/conf.d/8080.conf
    tasks:
    • name: task1
      file:
      path: "{{nginx.conf80}}"
      state: touch
    • name: task2
      file:
      path: "{{nginx.conf8080}}"
      state: touch

  • hosts: test70
    remote_user: root
    vars:
    nginx:
    conf80: /etc/nginx/conf.d/80.conf
    conf8080: /etc/nginx/conf.d/8080.conf
    tasks:
    • name: task1
      file:
      path: "{{nginx.conf80}}"
      state: touch
    • name: task2
      file:
      path: "{{nginx.conf8080}}"
      state: touch
      如上例所示,我界说了两个变量,两个变量的值对应两个nginx配置文件路径

当我们需要引用这两个变量时,有两种语法可用

,

联博统计

www.u-healer.com采用以太坊区块链高度哈希值作为统计数据,联博以太坊统计数据开源、公平、无任何作弊可能性。联博统计免费提供API接口,支持多语言接入。

,

语法一

Shell
"{{nginx.conf80}}"
1
"{{nginx.conf80}}"
语法二

Shell
"{{nginx['conf8080']}}"
1
"{{nginx['conf8080']}}"

而在上述后面的示例中引用变量时,变量被引用时如下,处于"开头的位置"

Shell
path: "{{nginx.conf80}}"
1
path: "{{nginx.conf80}}"
这种情况下,我们引用变量时必须使用双引号引起被引用的变量,否则会报语法错误

"vars"关键字和"vars_files"关键字可以同时使用,如下

Shell
vars:

  • conf90: /etc/nginx/conf.d/90.conf
    vars_files:
  • /testdir/ansible/nginx_vars.yml

    vars:

  • conf90: /etc/nginx/conf.d/90.conf
    vars_files:
  • /testdir/ansible/nginx_vars.yml