ansible変数

8225 ワード

目次
  • ansible変数
  • 変数を定義する方法
  • 変数
  • の定義方法
  • ansible変数優先度テスト
  • ansibleレイヤ定義変数
  • facts基本用法
  • facts生成mysqlプロファイル

  • ansible変数


    変数の定義方法

  • コマンドライン
  • playbook
  • ホストリスト
  • 変数の定義方法


    1.playbookで変数を定義する
    # :
    - hosts: db02
      vars:
        pkg:
          - MySQL-python
          - mariadb-server
      tasks:
        - name: 
          yum:
            name: "{{ pkg }}"
            state: present
      
    # :
    - hosts: db01
      vars:
         web_pkg: httpd
         db_pkg:
           - unzip
           - vsftpd
         php_pkg:
           - php-fpm
           - php
           - php-mysql

    2.vars_の使用files定義変数
    # 
    [root@m01 project]# vim vars_file.yml
    web_pkg: httpd
    db_pkg: mariadb-server
    php_pkg: php
    
    # 
    [root@m01 project]# vim httpd.yml 
    - hosts: web_group
      vars_files: ./vars_file.yml
      tasks:
        - name: Install httpd
          yum:
            name: "{{ db_pkg }}"
            state: present

    3.ansible内蔵変数を使用
    vim mkdri.yml
    - hosts: web_group
      tasks:
        - name: MKDIR
          file:
            path: /backup/{{ ansible_fqdn }}_{{ ansible_default_ipv4['address'] }}_{{ ansible_date_time['date'] }}
            state: directory
            recurse: yes

    4.ホスト構成表を使用した変数の定義
    # 
    [webs]
    zls_web01 ansible_ssh_host=172.16.1.7
    zls_web02 ansible_ssh_host=172.16.1.8
    
    [webs:vars]
    web_pkg=nginx
    
    [backup_group]
    backup ansible_ssh_host=172.16.1.41
    
    [db_group]
    db01 ansible_ssh_host=172.16.1.51
    db02 ansible_ssh_host=172.16.1.52
    
    [nfs_group]
    nfs ansible_ssh_host=172.16.1.31
    
    [rsync_server:children]
    web_group
    backup_group
    
    [rsync_server:vars]
    web_pkg=httpd
    
    [nfs_server:children]
    web_group
    nfs_group

    呼び出し変数
    - hosts: zls_web01
      tasks:
        - name: Install web server
          yum:
            name: "{{ web_pkg }}"
            state: present

    5.公式推薦方式
    # 
    # 
    mkdir group_vars
    [root@m01 ~]# vim group_vars/webs
    web_pkg: httpd
    
    # 
    [root@m01 ~]# cat group_vars/webs 
    web_pkg: httpd
    # 
    [root@m01 ~]# cat host_vars/zls_web01 
    web_pkg: nginx
    
    [root@m01 ~]# vim web.yml 
    - hosts: webs
      tasks:
        - name: Install web server
          yum:
            name: "{{ web_pkg }}"
            state: present

    6.コマンドライン定義変数
    # 
    [root@m01 ~]# !vim
    vim web.yml 
    - hosts: webs
      tasks:
      - name: Install httpd Server
        yum:
          name:
            - "{{ web_pkg }}"
    
      - name: Install mariadb Server
        yum:
          name:
            - "{{ db_pkg }}"
    
    
    # 
    [root@m01 ~]# ansible-playbook web.yml -e "web_pkg=vsftpd" -e "db_pkg=unzip"

    ansible変数優先度テスト

    - hosts: webs
      vars:
        dir: playbook_vars
      vars_files: ./vars_file.yml
      tasks:
        - name: Create Dir
          file:
            path: /opt/{{ dir }}
            state: directory
    [root@m01 ~]# vim vars_file.yml
    dir: vars_file
    
    [root@m01 ~]# vim host_vars/zls_web01
    dir: host_vars
    
    [root@m01 ~]# vim group_vars/webs
    dir: group_vars

    テストフェーズ
    [root@m01 ~]# ansible-playbook test.yml -e "dir=command_vars"
    [root@web01 ~]# ll /opt/
    total 0
    drwxr-xr-x 2 root root 6 Sep 19 16:48 command_vars
    
    #2.
    [root@m01 ~]# ansible-playbook test.yml
    [root@web01 ~]# ll /opt/
    total 0
    drwxr-xr-x 2 root root 6 Sep 19 16:48 command_vars
    drwxr-xr-x 2 root root 6 Sep 19 16:50 vars_file
    
    #3.
    [root@m01 ~]# vim test.yml 
    - hosts: webs
      vars:
        dir: playbook_vars
      tasks:
        - name: Create Dir
          file:
            path: /opt/{{ dir }}
            state: directory
    
    [root@m01 ~]# ansible-playbook test.yml
    [root@web01 ~]# ll /opt/
    total 0
    drwxr-xr-x 2 root root 6 Sep 19 16:48 command_vars
    drwxr-xr-x 2 root root 6 Sep 19 16:52 playbook_vars
    drwxr-xr-x 2 root root 6 Sep 19 16:50 vars_file
    
    #4.
    - hosts: webs
      tasks:
        - name: Create Dir
          file:
            path: /opt/{{ dir }}
            state: directory
    
    [root@m01 ~]# ansible-playbook test.yml

    ==コマンドライン>vars_file > playbook vars > host_vars > group_vars==

    ansibleレイヤ定義変数

    # 
    [root@m01 ~]# vim vars_file.yml
    lamp:
      framework:
        web_package: httpd
        db_package: mariadb-server
        php_package: php
    
    lnmp:
      framework:
        web_package: nginx
        db_package: mysql
        php_package: php
        
    # playbook 
    [root@m01 ~]# vim test.yml
    - hosts: web_group
      vars_files: ./vars_file.yml
      tasks:
        - name: Install LAMP httpd
          yum:
            name: "{{ lamp.framework.web_package }}"
    
        - name: Install LAMP mariadb-server
          yum:
            name: "{{ lamp.framework.db_package }}"
    
        - name: Install LAMP php
          yum:
            name: "{{ lamp.framework.php_package }}"
           
           
    # 
    [root@m01 ~]# vim test.yml
    - hosts: web_group
      vars_files: ./vars_file.yml
      tasks:
        - name: Install LAMP httpd
          yum:
            name: "{{ lamp['framework']['web_package'] }}"
    
        - name: Install LAMP mariadb-server
          yum:
            name: "{{ lamp['framework']['db_package'] }}"
    
        - name: Install LAMP php
          yum:
            name: "{{ lamp['framework']['php_package'] }}"

    factsの基本的な使い方

    # 
    [root@m01 ~]# vim facts.yml
    - hosts: web_group
      tasks:
        - name: Get Host Info
          debug:
            msg: >
              Hostname "{{ ansible_fqdn }}" and IP "{{ ansible_default_ipv4.address }}"
    
    # 
    [root@m01 ~]# ansible-playbook facts.yml
    [root@m01 ~]# ansible-playbook facts.yml
    
    PLAY [web_group] *****************************************************************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
    ok: [web02]
    ok: [web01]
    
    TASK [Get Host Info] *************************************************************************************************************************************************************************************************************************
    ok: [web01] => {
        "msg": "Hostname \"web01\" and IP \"10.0.0.7\"
    " } ok: [web02] => { "msg": "Hostname \"web02\" and IP \"10.0.0.8\"
    " } PLAY RECAP *********************************************************************************************************************************************************************************************************************************** web01 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    - hosts: webs
      tasks:
        - name: mem
          file:
            path: /opt/{{ ansible_facts['fqdn'] }}
            #path: /root/{{ ansible_fqdn }}
            state: directory

    factsを閉じる

    [root@m01 ~]# vim facts.yml
    - hosts: web_group
      gather_facts: no # 
      tasks:

    facts zabbixプロファイルの生成

    - hosts: web_group
      vars:
        - zabbix_server: 172.16.1.71
      tasks:
        - name: copy zabbix agent conf
          template:
            src: ./zabbix_agentd.conf
            dest: /tmp/zabbix_agentd.conf

    facts mysqlプロファイルの生成

    - hosts: db_group
      tasks:
        - name: Install mysql server
          yum:
            name: mariadb-server
            state: present
    
        - name: copy mysql  conf
          template:
            src: ./my.cnf
            dest: /etc/my.cnf
    
    
    [root@m01 ~]# vim /etc/my.cnf
    [mysqld]
    basedir=/usr
    datadir=/var/lib/mysql/
    socket=/var/lib/mysql/mysql.sock
    log_error=/var/log/mariadb/mariadb.log
    innodb_buffer_pool_size={{ ansible_memtotal_mb * 0.8 }}