Three: Ansible variable

Three: Ansible variable

A: Variable Overview

Variable provides a convenient way to manage dynamic values for each item Ansible playbook in, for example, nginx-1.6.3this version of the package, may be used repeatedly in other places, so if we talked about this value is set to a variable, and then in other playbook calling will be a lot easier. In this way also facilitates maintenance and reduces maintenance costs.

1. Variables defined manner

1.1 Variable Definition command line
[root@m01 ~]# vim test.yml
- hosts: web_group
  tasks:
  - name: Install httpd Server
    yum:
      name: "{{ web_server }}"

#定义阶段
[root@m01 ~]# ansible-playbook test.yml -e "web_server=vsftpd"
1.2 defined variables in play file
- name: ensure a list of packages installed
  yum:
    name: "{{ packages }}"
  vars:
    packages:
    - httpd
    - httpd-tools

#方法一
- hosts: web_group
  vars:
    packages:
      - samba
      - vsftpd
  tasks:
  - name: Install httpd  mariadb php Server
    yum:
      name: "{{ packages }}"

#方法二
- hosts: web_group
  vars:
      - web_server: lsof
      - db_server: htop
      - php_server: iftop,sysstat        #这边注意多个软件包的写法
  tasks:
  - name: Install httpd  mariadb php Server
    yum:
      name:
        - "{{ web_server }}"
        - "{{ db_server }}"
        - "{{ php_server }}"
1.3 Inventory carried out by the host variable definition information file (not recommended, easy to make environmental chaos)
#定义阶段
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
[web_group:vars]
web_server=httpd
index_file=index.html

#调用阶段
- hosts: web_group
  tasks:
  - name: Install httpd Server
    yum:
      name: "{{ web_server }}"
  - name: Create Index File
    file:
      path: /tmp/{{ index_file }}
      state: touch 
1.4 define variables in a template file
EX1:
#定义变量
[root@m01 ~]#vim vars_file.yml 
filename: vars_file

#调用变量
[root@m01 bianliang]# vim file.yml 
- hosts: web_group
  vars_files: ./vars_file.yml
  tasks:
    - name: Create {{ filename }} DIR
      file:
        path: /root/{{ filename }}
        state: directory
        
EX2:
#定义变量
[root@m01 ~]# vim vars_file.yml
mysql_ip: localhost
mysql_user: root
mysql_password: '123'

#调用变量
- hosts: db01
  vars_files: ./vars_file1.yml
  tasks:
    - name: Create a new database with name 'jiangwei'
      mysql_db:
            login_user: {{ mysql_user }}
            login_password: {{ mysql_password }}
            login_host: {{ mysql_ip }}
            name: jiangwei
            state: present  
此变量目的:以后创建用户只需要在vars_file1.yml文件中修改即可,无须修改配置文件


#获取Ansible内置变量(利用setup模块)
- hosts: web_group
  tasks:
    - name: Touch IP File
      file:
        path: /root/{{ ansible_default_ipv4['address'] }}
        state: touch

    - name: Touch Hostname File
      file:
        path: /root/{{ ansible_fqdn }}
        state: touch
#创建分别以地址和主机名命令的文件       
[root@web01 ~]# ll
total 48
-rw-r--r--  1 root root     0 Nov 19 21:03 10.0.0.7
-rw-r--r--  1 root root     0 Nov 19 21:03 web01
1.5 officially recommended variable definitions

Several variables are defined before is not very good, relatively easy to use is to create two variables directory under the project directory Ansible:
host_vars
group_vars
Remember, the directory name must be consistent, not make any changes.

Note: playbook created where these two files is created where.
EX1:
#创建出组变量目录和主机变量目录
[root@m01 bianliang]# mkdir host_vars

#想给哪个主机定义变量,就在host_vars目录下创建该主机的名字文件
例如:web01
[root@m01 bianliang]# vim host_vars/web01   #注意web01必须和主机名对应,
web_server: nginx

[root@m01 bianliang]# vim host_vars/web02
web_server: samba

- hosts: web_group
  tasks:
    - name: Install
      yum:
        name: "{{ web_server }}"
此变量目的:当需求为web01只安装nginx,web02只安装samba时。



EX2:
#定义阶段
[root@m01 ~]# mkdir group_vars

#切记定义变量的文件必须以组名为文件名
[root@m01 ~]# vim /root/group_vars/web_group
web_server: httpd

#调用阶段
- hosts: web_group
  tasks:
  - name: Install httpd Server
    yum:
      name: "{{ web_server }}" 

Priority 2. variables

1. Command Line

2.vars_files

3.vars

4.host_vars

5.group_vars

#官方推荐
host_vars
group_vars

II: Variables registered

When absibleafter the module is operating, in fact, will return some resultresults, like the implementation of the script, the script sometimes we need to give us some returnreturn value, we know that, if the step is executed successfully, but by default, ansibleis resultnot is displayed, so that we can return these values 'stored' in a variable, so that we can pass the 'recall' variable names corresponding to these acquired resultthis module, the return value is written into the variable the method is called variable registration

As a playbook example:

#编辑剧本
[root@m01 ~]# vim register.yml
- hosts: web_group
  tasks:
    - name: Test Register Vars
      shell: "ls -l /"

#查看执行结果
[root@m01 ~]# ansible-playbook register.yml

PLAY [web_group] *****************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]

TASK [Test Register Vars] ********************************************************************************************************************************************************************************************************************
changed: [web01]
changed: [web02]

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
web01                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
web02                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

As the results can be seen, when we use the shell module executes ls -l /, ansible to be changed we can not see the results after the execution of our return, so this time we need to use to register variables

playbook as follows:

#编辑playbook
[root@m01 ~]# vim register.yml
- hosts: web_group
  tasks:
    - name: Test Register Vars
      shell: "ls -l /"
      register: list_dir     ####

    - name: Return Result
      debug:
        msg: "{{ list_dir }}"

#查看执行结果
[root@m01 ~]# ansible-playbook register.yml

PLAY [web_group] *****************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [web01]
ok: [web02]

TASK [Test Register Vars] ********************************************************************************************************************************************************************************************************************
changed: [web01]
changed: [web02]

TASK [Return Result] *************************************************************************************************************************************************************************************************************************
ok: [web01] => {
    "msg": {
        "changed": true,
        "cmd": "ls -l /",
        "delta": "0:00:00.005536",
        "end": "2019-09-16 11:52:16.492946",
        "failed": false,
        "rc": 0,
        "start": "2019-09-16 11:52:16.487410",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "总用量 28\nlrwxrwxrwx.   1 root root    7 3月   9 2019 bin -> usr/bin\ndr-xr-xr-x.   5 root root 4096 3月   9 2019 boot\ndrwxr-xr-x.  20 root root 3280 9月   8 12:25 dev\ndrwxr-xr-x.  80 root root 8192 9月  10 20:52 etc\ndrwxr-xr-x.   5 root root   41 9月   8 16:22 home\nlrwxrwxrwx.   1 root root    7 3月   9 2019 lib -> usr/lib\nlrwxrwxrwx.   1 root root    9 3月   9 2019 lib64 -> usr/lib64\ndrwxr-xr-x.   2 root root    6 4月  11 2018 media\ndrwxr-xr-x.   2 root root    6 4月  11 2018 mnt\ndrwxr-xr-x.   2 www  www     6 9月  10 15:31 opt\ndr-xr-xr-x. 128 root root    0 9月   8 12:25 proc\ndr-xr-x---.   9 root root 4096 9月  10 21:16 root\ndrwxr-xr-x.  25 root root  740 9月  10 20:52 run\nlrwxrwxrwx.   1 root root    8 3月   9 2019 sbin -> usr/sbin\ndrwxr-xr-x.   2 root root    6 4月  11 2018 srv\ndr-xr-xr-x.  13 root root    0 9月   8 12:25 sys\ndrwxrwxrwt.  15 root root 4096 9月  16 11:52 tmp\ndrwxr-xr-x.  13 root root  155 3月   9 2019 usr\ndrwxr-xr-x.  21 root root 4096 9月  10 20:52 var",
        "stdout_lines": [
            "总用量 28",
            "lrwxrwxrwx.   1 root root    7 3月   9 2019 bin -> usr/bin",
            "dr-xr-xr-x.   5 root root 4096 3月   9 2019 boot",
            "drwxr-xr-x.  20 root root 3280 9月   8 12:25 dev",
            "drwxr-xr-x.  80 root root 8192 9月  10 20:52 etc",
            "drwxr-xr-x.   5 root root   41 9月   8 16:22 home",
            "lrwxrwxrwx.   1 root root    7 3月   9 2019 lib -> usr/lib",
            "lrwxrwxrwx.   1 root root    9 3月   9 2019 lib64 -> usr/lib64",
            "drwxr-xr-x.   2 root root    6 4月  11 2018 media",
            "drwxr-xr-x.   2 root root    6 4月  11 2018 mnt",
            "drwxr-xr-x.   2 www  www     6 9月  10 15:31 opt",
            "dr-xr-xr-x. 128 root root    0 9月   8 12:25 proc",
            "dr-xr-x---.   9 root root 4096 9月  10 21:16 root",
            "drwxr-xr-x.  25 root root  740 9月  10 20:52 run",
            "lrwxrwxrwx.   1 root root    8 3月   9 2019 sbin -> usr/sbin",
            "drwxr-xr-x.   2 root root    6 4月  11 2018 srv",
            "dr-xr-xr-x.  13 root root    0 9月   8 12:25 sys",
            "drwxrwxrwt.  15 root root 4096 9月  16 11:52 tmp",
            "drwxr-xr-x.  13 root root  155 3月   9 2019 usr",
            "drwxr-xr-x.  21 root root 4096 9月  10 20:52 var"
        ]
    }
}
ok: [web02] => {
    "msg": {
        "changed": true,
        "cmd": "ls -l /",
        "delta": "0:00:00.005813",
        "end": "2019-09-16 11:52:16.495422",
        "failed": false,
        "rc": 0,
        "start": "2019-09-16 11:52:16.489609",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "总用量 24\nlrwxrwxrwx.   1 root root    7 3月   9 2019 bin -> usr/bin\ndr-xr-xr-x.   5 root root 4096 3月   9 2019 boot\ndrwxr-xr-x.  20 root root 3260 9月  10 09:47 dev\ndrwxr-xr-x.  80 root root 8192 9月  10 20:52 etc\ndrwxr-xr-x.   5 root root   41 9月   8 16:22 home\nlrwxrwxrwx.   1 root root    7 3月   9 2019 lib -> usr/lib\nlrwxrwxrwx.   1 root root    9 3月   9 2019 lib64 -> usr/lib64\ndrwxr-xr-x.   2 root root    6 4月  11 2018 media\ndrwxr-xr-x.   2 root root    6 4月  11 2018 mnt\ndrwxr-xr-x.   2 www  www     6 9月  10 15:31 opt\ndr-xr-xr-x. 128 root root    0 8月  15 15:10 proc\ndr-xr-x---.   6 root root  180 9月  10 21:16 root\ndrwxr-xr-x.  25 root root  740 9月  10 20:52 run\nlrwxrwxrwx.   1 root root    8 3月   9 2019 sbin -> usr/sbin\ndrwxr-xr-x.   2 root root    6 4月  11 2018 srv\ndr-xr-xr-x.  13 root root    0 8月  15 15:10 sys\ndrwxrwxrwt.  14 root root 4096 9月  16 11:52 tmp\ndrwxr-xr-x.  13 root root  155 3月   9 2019 usr\ndrwxr-xr-x.  21 root root 4096 9月  10 20:52 var",
        "stdout_lines": [
            "总用量 24",
            "lrwxrwxrwx.   1 root root    7 3月   9 2019 bin -> usr/bin",
            "dr-xr-xr-x.   5 root root 4096 3月   9 2019 boot",
            "drwxr-xr-x.  20 root root 3260 9月  10 09:47 dev",
            "drwxr-xr-x.  80 root root 8192 9月  10 20:52 etc",
            "drwxr-xr-x.   5 root root   41 9月   8 16:22 home",
            "lrwxrwxrwx.   1 root root    7 3月   9 2019 lib -> usr/lib",
            "lrwxrwxrwx.   1 root root    9 3月   9 2019 lib64 -> usr/lib64",
            "drwxr-xr-x.   2 root root    6 4月  11 2018 media",
            "drwxr-xr-x.   2 root root    6 4月  11 2018 mnt",
            "drwxr-xr-x.   2 www  www     6 9月  10 15:31 opt",
            "dr-xr-xr-x. 128 root root    0 8月  15 15:10 proc",
            "dr-xr-x---.   6 root root  180 9月  10 21:16 root",
            "drwxr-xr-x.  25 root root  740 9月  10 20:52 run",
            "lrwxrwxrwx.   1 root root    8 3月   9 2019 sbin -> usr/sbin",
            "drwxr-xr-x.   2 root root    6 4月  11 2018 srv",
            "dr-xr-xr-x.  13 root root    0 8月  15 15:10 sys",
            "drwxrwxrwt.  14 root root 4096 9月  16 11:52 tmp",
            "drwxr-xr-x.  13 root root  155 3月   9 2019 usr",
            "drwxr-xr-x.  21 root root 4096 9月  10 20:52 var"
        ]
    }
}

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
web01                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
web02                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

#只输出自己想要的内容
[root@m01 ~]# vim register.yml
- hosts: web_group
  tasks:
    - name: Test Register Vars
      shell: "ls -l /"
      register: list_dir

    - name: Return Result
      debug:
        msg: "{{ list_dir.stdout_lines }}"

#查看结果
[root@m01 ~]# ansible-playbook register.yml

PLAY [web_group] *****************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [web02]
ok: [web01]

TASK [Test Register Vars] ********************************************************************************************************************************************************************************************************************
changed: [web01]
changed: [web02]

TASK [Return Result] *************************************************************************************************************************************************************************************************************************
ok: [web01] => {
    "msg": [
        "总用量 28",
        "lrwxrwxrwx.   1 root root    7 3月   9 2019 bin -> usr/bin",
        "dr-xr-xr-x.   5 root root 4096 3月   9 2019 boot",
        "drwxr-xr-x.  20 root root 3280 9月   8 12:25 dev",
        "drwxr-xr-x.  80 root root 8192 9月  10 20:52 etc",
        "drwxr-xr-x.   5 root root   41 9月   8 16:22 home",
        "lrwxrwxrwx.   1 root root    7 3月   9 2019 lib -> usr/lib",
        "lrwxrwxrwx.   1 root root    9 3月   9 2019 lib64 -> usr/lib64",
        "drwxr-xr-x.   2 root root    6 4月  11 2018 media",
        "drwxr-xr-x.   2 root root    6 4月  11 2018 mnt",
        "drwxr-xr-x.   2 www  www     6 9月  10 15:31 opt",
        "dr-xr-xr-x. 128 root root    0 9月   8 12:25 proc",
        "dr-xr-x---.   9 root root 4096 9月  10 21:16 root",
        "drwxr-xr-x.  25 root root  740 9月  10 20:52 run",
        "lrwxrwxrwx.   1 root root    8 3月   9 2019 sbin -> usr/sbin",
        "drwxr-xr-x.   2 root root    6 4月  11 2018 srv",
        "dr-xr-xr-x.  13 root root    0 9月   8 12:25 sys",
        "drwxrwxrwt.  15 root root 4096 9月  16 11:54 tmp",
        "drwxr-xr-x.  13 root root  155 3月   9 2019 usr",
        "drwxr-xr-x.  21 root root 4096 9月  10 20:52 var"
    ]
}
ok: [web02] => {
    "msg": [
        "总用量 24",
        "lrwxrwxrwx.   1 root root    7 3月   9 2019 bin -> usr/bin",
        "dr-xr-xr-x.   5 root root 4096 3月   9 2019 boot",
        "drwxr-xr-x.  20 root root 3260 9月  10 09:47 dev",
        "drwxr-xr-x.  80 root root 8192 9月  10 20:52 etc",
        "drwxr-xr-x.   5 root root   41 9月   8 16:22 home",
        "lrwxrwxrwx.   1 root root    7 3月   9 2019 lib -> usr/lib",
        "lrwxrwxrwx.   1 root root    9 3月   9 2019 lib64 -> usr/lib64",
        "drwxr-xr-x.   2 root root    6 4月  11 2018 media",
        "drwxr-xr-x.   2 root root    6 4月  11 2018 mnt",
        "drwxr-xr-x.   2 www  www     6 9月  10 15:31 opt",
        "dr-xr-xr-x. 128 root root    0 8月  15 15:10 proc",
        "dr-xr-x---.   6 root root  180 9月  10 21:16 root",
        "drwxr-xr-x.  25 root root  740 9月  10 20:52 run",
        "lrwxrwxrwx.   1 root root    8 3月   9 2019 sbin -> usr/sbin",
        "drwxr-xr-x.   2 root root    6 4月  11 2018 srv",
        "dr-xr-xr-x.  13 root root    0 8月  15 15:10 sys",
        "drwxrwxrwt.  14 root root 4096 9月  16 11:54 tmp",
        "drwxr-xr-x.  13 root root  155 3月   9 2019 usr",
        "drwxr-xr-x.  21 root root 4096 9月  10 20:52 var"
    ]
}

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************
web01                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
web02                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

#debug模块常用参数
msg:            #调试输出的消息
var:            #将某个任务执行的输出作为变量传递给debug模块,debug会直接将其打印输出
verbosity:      #debug的级别(默认是0级,全部显示)       

Guess you like

Origin www.cnblogs.com/captain-jiang/p/12078552.html