码迷,mamicode.com
首页 > 其他好文 > 详细

ansible变量vars/vars_files

时间:2020-09-17 12:28:04      阅读:25      评论:0      收藏:0      [点我收藏+]

标签:yam   lin   ant   注意   数值   server   require   state   dir   

对于重复使用而且复杂的参数值,可以定义变量引用,便于管理和修改,也可以创建文件存放变量,引用变量文件即可

示例如下

[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配置文件路径

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

语法一

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

ansible变量vars/vars_files

标签:yam   lin   ant   注意   数值   server   require   state   dir   

原文地址:https://blog.51cto.com/13434656/2528260

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!