Ansible学习05-变量vars

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible学习05-变量vars相关的知识,希望对你有一定的参考价值。

Ansible中使用变量操作 会使我们操作方便很多,本次说下简单的变量用法

  - hosts: docker
    remote_user: root
    vars:
        nginx80: /usr/local/nginx/conf/vhost
        nginx14:  https://nginx.org/download/nginx-1.14.2.tar.gz
    tasks:
     - command: ls nginx-1.14.2.tar.gz
       register: result
     - command: ls /usr/local/nginx/conf/vhost
       register: han
       ignore_errors: true
     - name: wget nginx_url14.2
       shell: wget https://nginx.org/download/nginx-1.14.2.tar.gz
       when: result|failed
     - name: mkdir nginx conf
       shell: mkdir -p {{ nginx80 }}
       when: han|failed
     - name: touch nginx config
       file: path={{ nginx80 }}/{{ item }} mode=0644 state=touch
       with_items:
          - nginx80.conf
          - nginx8080.conf
上面是定义的nginx配置文件目录和下载nginx1.14的压缩包,然后调用使用
你也可以换一种写法
  vars:
     -  nginx80: /usr/local/nginx/conf/vhost
     -  nginx14:  https://nginx.org/download/nginx-1.14.2.tar.gz

你也可以在一个vars里面定义多个name

   - hosts: docker
      remote_user: root
      vars:
        nginx:
          nginx80: /usr/local/nginx/conf/vhost1
          nginx14:  https://nginx.org/download/nginx-1.14.2.tar.gz
        wwwuser:
          nginxuser: www
       tasks:
       - name: conf nginx
         shell: wget {{nginx.nginx14 }}
       - name: mkdir nginx80
         shell: mkdir -p {{nginx.nginx80}}
       - name: touch nginx config
         file: path={{ nginx.nginx80 }}/{{ item }} mode=0644 state=touch
         with_items:
          - nginx_var.conf
       - name: gourp www
         group: name={{ wwwuser[‘nginxuser‘]}} gid=3001
       - name: useradd nginx user
         user: name={{ wwwuser.nginxuser }} group=www uid=3001 shell=/sbin/nologin
当我们定义多个vars的时候 有两种方式在写
  A: {{ nginx.nginx80 }}
        B: { wwwuser[‘nginxuser‘]}}

引入变量

vars_files:
        - /data/sh/vars_nginx.yml
        -  /data/sh/vars_nginx_user.yml

例子

    - hosts: docker
      remote_user: root
      vars_files:
       - /data/sh/vars_nginx.yml
       - /data/sh/vars_nginx_user.yml
      tasks:
       - name: conf nginx
         shell: wget {{nginx.nginx14 }}
       - name: mkdir nginx80
         shell: mkdir -p {{nginx.nginx80}}
       - name: touch nginx config
         file: path={{ nginx.nginx80 }}/{{ item }} mode=0644 state=touch
         with_items:
          - nginx_var.conf
       - name: gourp www
         group: name={{ wwwuser[‘nginxuser‘]}} gid=3001
       - name: useradd nginx user
         user: name={{ wwwuser.nginxuser }} group=www uid=3001 shell=/sbin/nologin

交互式输入参数给与变量

   - hosts: docker
     remote_user: root
     vars:
      nginx:
        nginx80: /usr/local/nginx/conf/vhost1
        nginx14:  https://nginx.org/download/nginx-1.14.2.tar.gz
      wwwuser:
        nginxuser: www
     vars_prompt:
      - name: "user_name"
        prompt: "Enter input name"
        private: no
      - name: "user_Password"
        prompt: "Enter input password(md5之后的加密串)" 
        encrypt: "sha512_crypt"   #给与哈希加密   confirm实现密码确认功能 开启是需要数据两次密码
        confirm: yes
     tasks:
      - name: create user
        user: name="{{user_name}}" uid=3004 group=root  password="{{user_Password}}" shell=/bin/bash home=/home/{{user_name}}

以上是关于Ansible学习05-变量vars的主要内容,如果未能解决你的问题,请参考以下文章

ansible-playbook 变量(vars)

ansible-playbook 额外变量的顺序和优先级

ansible笔记(14):变量(一)

ansible——组织变量

ansible 流程控制

ansible 流程控制