zoukankan      html  css  js  c++  java
  • Ansible变量

    Ansible变量

    特点 PlayBook ad-hoc
    完整性
    持久性
    执行效率
    变量 支持 不支持
    耦合度

    1.PlayBook功能比ad-hoc更全,是对ad-hoc的一种编排.
    2.PlayBook能很好的控制先后执行顺序, 以及依赖关系.
    3.PlayBook语法展现更加的直观.
    4.playbook可以持久使用,ad-hoc无法持久使用.


    扩展模块

    unarchive 模块

    用来解压,在管理机上的压缩包可以直接解压到目标主机上,也可在远程主机上执行默认解压到/tmp目录下。需要依耐各种解压命令。

    [root@m01 ~]# ansible web_group -m unarchive -a 'src=/root/wordpress-5.4-zh_CN.tar.gz dest=/root'
    
    [root@m01 ~]# ansible web01 -m unarchive -a 'src=/root/wordpress-5.4-zh_CN.tar.gz dest=/root remote_src=yes'
    
    src:		# 指定源文件在哪里(压缩包的路径)
    dest:        # 指定你要解压的位置在哪里
    remote_src:  # 指定该包是否在远端机器
    	yes:	# 在
    	no:  	# 不在
    

    数据控制模块


    grant all on *.* to wp@'%' identified by '123'
    
     mysql_user:
         name: wp
         password: 123
         host: "%"
         priv: '*.*:ALL'
         state: present
    
    name		# 创建的用户名
    password	# 创建的密码
    host		# 允许连接的主机
    priv		# 权限
    state		
      absent    # 删除
      present	# 创建
    
    mysql_db:
         name: 库名
         state: prensent
    

    定义playbook变量

    定义方式


    1. 命令方式定义
    2. play文件中定义
    3. 通过主机清单定义

    通过playbook定义


    # 定义变量的一种方式
    
    [root@m01 ~]# vi vars.yml
    - hosts: web_group
      vars:
      # packges 变量名
        packges:
          - httpd
          - tree
          - mariadb
      tasks:
        - name: install service
          yum: 
          # 调用变量
            name: "{{ packges_name }}"
            state: present
    
    # 方法二:
    
    - hosts: web_group
      vars:
          - web_server: httpd
          - db_server: mariadb-server
          - php_server:
            - php
            - php-mysql
            - php-pdo
       tasks:
      - name: Install httpd  mariadb php Server
        yum:
          name:
            - "{{ web_server }}"
            - "{{ db_server }}"
            - "{{ php_server }}"
    

    通过vars_file定义


    ## 1.定义变量
    [root@m01 ~]# vim /root/vars_file.yml
    create_file: zls_vars_file
    
    
    ## 2.调用变量
    [root@m01 ~]# vim var1.yml 
    
    - hosts: web01
      vars_files: /root/vars_file.yml
      tasks:
        - name: Create file
          file:
            path: /root/{{ create_file }}
            state: touch
    

    在主机清单中定义变量


    ## 1.定义变量
    [root@m01 ~]# vim /etc/ansible/hosts 
    [web_group:vars]
    create_file=zls_inventory
    
    ## 2.调用变量
    - hosts: web01
      tasks:
        - name: Create file
          file:
            path: /root/{{ create_file }}
            state: touch
    

    官方推荐


    官方推荐在ansible项目目录中创建变量,在play-book指定的目录下创建两个固定的目录名group_vars、host_vars再再里面创建变量。

    [root@m01 ~/ansible]# tree
    .
    ├── create.yml
    ├── group_vars
    │   └── web_group
    ├── host_vars
    │   ├── web01
    │   └── web02
    
    
    [root@m01 ~/ansible]# vi create.yml
    - hosts: all
      tasks:
        - name: create
          debug:
            msg: "{{ create_file }}"
          when: ansible_fqdn == "web01"
    # 打印web01的变量
    

    变量的优先级


    ## 调用变量
    
    - hosts: web_group
      vars:
        create_file: vars
      vars_files: /root/vars_files.yml
      tasks:
        - name: c f
          file:
            path: /root/{{ create_file }}
            state: touch
    
    
    ## 定义变量
    1.  vars:
        create_file: vars
    
    2. vars_files: /root/vars_files.yml
    
    3.host_vars
    [root@m01 ansible]# vim host_vars/web01 
    create_file: host_vars_web01
    [root@m01 ansible]# vim host_vars/web02
    create_file: host_vars_web02
    
    
    4.group_vars
    [root@m01 ansible]# vim group_vars/web_group 
    create_file: group_vars
    
    5.命令行定义变量  -e '变量名=变量值'
    [root@m01 ansible]# ansible-playbook var4.yml  -e 'create_file=minglinghang_file'
    
    ##### 优先级最高:命令行
    #### 优先级第二:vars_file
    ### 优先级第三:vars
    ## 优先级第四:host_vars
    # 优先级第五:group_vars
    
    
    
    #### 层级定义变量
    lnmt:
      framework:
        web_package: nginx
        db_package: mysql
        java_package: tomcat
      xxx:
        a: 123
        b:456
    
    
    ### 层级调用变量
    {{ lnmt.xxx.a }}
    {{ lnmt.framework.web_package }} 
    
    变量 优先级
    命令行file 1
    vars_file 2
    vars 3
    host_vars 4
    group_vars 5

    变量注册


    [root@m01 ~]# vi regester.yml
    - hosts: web_group
      tasks:
        - name: Test re vars
          shell: "df -h"
          register: disk_info
    
        - name: get disk info
          debug:
            msg: "{{ disk_info.stdout_lines }}"
    
    # 案例
    [root@m01 ~/ansible]# vim register.yml
    - hosts: web_group
      tasks:
      - name: register vars
        shell: "hostname -I"
        register: host_ip
    
      - name: get register vars
        debug:
          msg: "{{ host_ip.stdout_lines }}"
    
    [root@m01 ~/ansible]# vi create.yml 
    - hosts: web01
      tasks:
        - name: register var
          shell: "ls /root/nginx_php"
          register: directory_info
          ignore_errors: yes
    	# 忽略错误
        - name: create web01
          shell: "mv /root/nginx_php /root/web01"
          when: directory_info.rc == 0
          
    # 通过注册变量的方式来判断文件是否存在,然后做出相应的动作。
    

    层级变量


    # 定义层级变量
    [root@m01 ~/ansible]# vi vars_file.yml
    lamp:
      framework:
        web_package: httpd
        db_package: mariadb-server
        php_package: php
        
    # 调用方法
    [root@m01 ~/ansible]# vi level.yml
    - hosts: web01
      vars_files: ./vars_file.yml
      tasks:
        - name: debug level vars
          debug:
            # 每一层后面加"."取到下一层
            msg: "{{ lamp.framework.web_package }}"
    
    

    facts 缓存


    就是每一次执行一次play-book缓存的主机信息。

    [root@m01 ~/ansible]# ansible web01 -m setup

    ## 关闭facts
    [root@m01 ~]# vim facts.yml
    - hosts: web_group
      gather_facts: no #关闭信息采集
      tasks:
    

    template 模块

    在使用它推送脚本的时候,会把脚本里面的ansible变量给替换掉。

    [root@m01 ~/ansible]# cat hostname.conf 
    hostname={{ ansible_fqdn }}
    
    [root@m01 ~/ansible]# vi template.yml
    - hosts: web01
      tasks:
        - name: telplate test
          template:
            src: /root/ansible/hostname.conf
            dest: /root/
            
    [root@web01 ~]# cat hostname.conf 
    hostname=web01
    
  • 相关阅读:
    Crontab问题总结
    确定两个 Geometry 实例之间的关系
    Silverlight 多点触控(MultiTouch)
    在.NET中嵌入IronPython 交互
    Siebel集成OBI报表
    Silverlight XPS (PPT) Online
    Siebel Data Validation Manage 数据验证
    Siebel 附件清理
    Siebel 消息广播管理
    怎样创建一个.NET RIA Services Application (二)
  • 原文地址:https://www.cnblogs.com/gshelldon/p/13375489.html
Copyright © 2011-2022 走看看