[Ansible系列]使用lookup生成变量

目录

一.    简介

二.    lookup使用方式

2.1    file

2.2    pipe

2.3    lines 

2.4    url 

2.5    env

2.6    template

2.7    password

 2.8    dnstxt

 2.9    redis_kv


一.    简介

         在通常情况下,所有的配置信息都会被作为ansible的变量进行保存,而且可以保存在ansible允许定义 变量的各种地方,如playbook中的vars区段, vars_files 加载的文件中,以及host_vars和group_vars目录中。 但在有些时候,我们希望从诸如文本文件或者.csv文件中收集数据作为ansible的变量,或者直接获取 某些命令的输出作为ansible的变量(之前有说过registery来获取,但是有时候registery也是会有局限性),甚至从redis或者etcd这样的键值存储中取得相应的值作为 ansible的变量。这个时候,我们就需要通过ansible的lookup插件来从这些数据源中读取配置数据, 传递给ansbile变量,并在playbook或者模板中使用这些数据。 ansible支持一套从不同数据源获取数据的lookup,包括file, password, pipe, env, template, csvfile, dnstxt, redis_kv, etcd等

二.    lookup使用方式

2.1    file

         使用file lookup可以在控制节点上从文本文件中获取数据,并在这些数据传递给ansible变量,在task或者jinja2模 板中进行引用。
 

示例:从文本文件中获取/tmp/xhz.txt内容,并作为变量;

##play-book文件
- hosts: init_server
  gather_facts: no

  tasks:
    - name: get file content with /tmp/xhz.txt 
      set_fact:
        xhz: "{
   
   { lookup('file', '/tmp/xhz.txt') }}"

    - name: register
      shell: cat /tmp/xhz.txt
      register: register_key

    - name: debug xhz and register_key
      debug:
        msg:
          - "{
   
   { xhz }}"
          - "{
   
   { register_key['stdout'] }}"

##执行结果
[root@clinet ansible_2]# ansible-playbook yum_file/obtain_sshkey.yml 

PLAY [init_server] **************************************************************************************************************************************************

TASK [get file content with /tmp/xhz.txt] ***************************************************************************************************************************
ok: [192.168.194.129]
ok: [192.168.194.130]
ok: [192.168.194.131]

TASK [register] *****************************************************************************************************************************************************
changed: [192.168.194.130]
changed: [192.168.194.131]
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
changed: [192.168.194.129]

TASK [debug xhz and register_key] ***********************************************************************************************************************************
ok: [192.168.194.129] => {
    "msg": [
        "client", 
        "route"
    ]
}
ok: [192.168.194.130] => {
    "msg": [
        "client", 
        "lvs-1"
    ]
}
ok: [192.168.194.131] => {
    "msg": [
        "client", 
        "lvs-2"
    ]
}

PLAY RECAP **********************************************************************************************************************************************************
192.168.194.129            : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.194.130            : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.194.131            : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet ansible_2]# 

 注意:

       1.  file读取的文本文件是ansible主控端的文本文件;

        2.  register方式获取文本文件中的内容作为变量,是获取的ansoble被控端中的文本文件;;

2.2    pipe

         使用pipe lookup可以在控制节点上调用外部命令,并将命令执行的结果打印到标准输出,作为ansible变量。 下面的例子通过pipe调用date指令拿到一个以时间数字组成的字串

示例:将执行的hostname命令,返回的结果作为变量值;

## play-book
- hosts: init_server
  gather_facts: no

  tasks:
    - name: loopup with pipe
      set_fact:
        pipe_value: "{
   
   { lookup('pipe', 'hostname')}}"

    - name: debug
      debug:
        msg:
          - "{
   
   { pipe_value }}"



##执行结果
[root@clinet ansible_2]# ansible-playbook yum_file/lookup/pipe.yml 

PLAY [init_server] **************************************************************************************************************************************************

TASK [loopup with pipe] *********************************************************************************************************************************************
ok: [192.168.194.130]
ok: [192.168.194.129]
ok: [192.168.194.131]

TASK [debug] ********************************************************************************************************************************************************
ok: [192.168.194.131] => {
    "msg": [
        "clinet"
    ]
}
ok: [192.168.194.129] => {
    "msg": [
        "clinet"
    ]
}
ok: [192.168.194.130] => {
    "msg": [
        "clinet"
    ]
}

PLAY RECAP **********************************************************************************************************************************************************
192.168.194.129            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.194.130            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.194.131            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet ansible_2]# 

2.3    lines 

         lines和pipe用法一致,都是在ansible的控制节点上运行命令,并返回输出,lines是将命令的输出拆分为行;pipe是返回命令的原始输出。

 示例: 使用执行ls  -l和hostname命令,用lines接收,并对比与pipe接收的区别。

##plsybook
- hosts: init_server
  gather_facts: no

  tasks:
    - name: loopup with pipe
      set_fact:
        pipe_value: "{
   
   { lookup('pipe', 'hostname', 'ls -l') }}"
        lines_value: "{
   
   { lookup('lines', 'hostname', 'ls -l') }}"

    - name: debug1
      debug:
        msg:
          - "{
   
   { pipe_value }}"
          - "{
   
   {  lines_value }}"


##执行结果:
[root@clinet ansible_2]# ansible-playbook yum_file/lookup/pipe.yml 

PLAY [init_server] **************************************************************************************************************************************************

TASK [loopup with pipe] *********************************************************************************************************************************************
ok: [192.168.194.129]
ok: [192.168.194.131]
ok: [192.168.194.130]

TASK [debug1] *******************************************************************************************************************************************************
ok: [192.168.194.129] => {
    "msg": [
        "clinet,total 8\n-rw-r--r--. 1 root root 379 Dec 11 14:57 file.yml\n-rw-r--r--. 1 root root 334 Dec 12 16:26 pipe.yml", 
        "clinet,total 8,-rw-r--r--. 1 root root 379 Dec 11 14:57 file.yml,-rw-r--r--. 1 root root 334 Dec 12 16:26 pipe.yml"
    ]
}
ok: [192.168.194.130] => {
    "msg": [
        "clinet,total 8\n-rw-r--r--. 1 root root 379 Dec 11 14:57 file.yml\n-rw-r--r--. 1 root root 334 Dec 12 16:26 pipe.yml", 
        "clinet,total 8,-rw-r--r--. 1 root root 379 Dec 11 14:57 file.yml,-rw-r--r--. 1 root root 334 Dec 12 16:26 pipe.yml"
    ]
}
ok: [192.168.194.131] => {
    "msg": [
        "clinet,total 8\n-rw-r--r--. 1 root root 379 Dec 11 14:57 file.yml\n-rw-r--r--. 1 root root 334 Dec 12 16:26 pipe.yml", 
        "clinet,total 8,-rw-r--r--. 1 root root 379 Dec 11 14:57 file.yml,-rw-r--r--. 1 root root 334 Dec 12 16:26 pipe.yml"
    ]
}

PLAY RECAP **********************************************************************************************************************************************************
192.168.194.129            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.194.130            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.194.131            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet ansible_2]# 

 注意:

"msg": [
        "clinet,total 8\n-rw-r--r--. 1 root root 379 Dec 11 14:57 file.yml\n-rw-r--r--. 1 root root 334 Dec 12 16:26 pipe.yml", 
        "clinet,total 8,-rw-r--r--. 1 root root 379 Dec 11 14:57 file.yml,-rw-r--r--. 1 root root 334 Dec 12 16:26 pipe.yml"
    ]

pipe执行的输出遇到换行的时候是有\n换行符的,而lines没有,直接作为了一行。

2.4    url 

         url是从指定的url中获取内容,返回的是url的html内容

 示例:获取百度的htnl信息

- hosts: init_server
  gather_facts: no

  tasks:
    - name: loopup with pipe
      set_fact:
        url_value: "{
   
   { lookup('url', 'http://www.baidu.com') }}"

    - name: debug1
      debug:
        msg:
          - " {
   
   { url_value }}"

2.5    env

         env lookup实际就是获取在控制主机上的某个环境变量的值。

 示例:获取SSH_CONNECTION环境变量的值。        

- hosts: init_server
  gather_facts: no

  tasks:
    - name: loopup with pipe
      set_fact:
        env_valus: "{
   
   { lookup('env', 'SSH_CONNECTION') }}"

    - name: debug1
      debug:
        msg:
          - "{
   
   { env_valus }}"

2.6    template

         template lookup可以指定一个jinja2模板,然后返回这个模板中的变量被替换以后的结果。

 示例: 设定一个username.j2模板,然后通过template进行替换。

##username.j2文件
[root@clinet ansible_2]# cat yum_file/lookup/username.j2 
my name is {
   
   { ansible_env['USER'] }}
[root@clinet ansible_2]#



##playbook
- hosts: init_server
  gather_facts: yes

  tasks:
    - name: loopup with pipe
      set_fact:
        template_values: "{
   
   { lookup('template', 'username.j2')}}"

    - name: debug1
      debug:
        msg:
          - "{
   
   { template_values }}"

#执行结果:
[root@clinet ansible_2]# ansible-playbook yum_file/lookup/pipe.yml 

PLAY [init_server] **************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.194.129 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.194.129]
ok: [192.168.194.130]
ok: [192.168.194.131]

TASK [loopup with pipe] *********************************************************************************************************************************************
ok: [192.168.194.129]
ok: [192.168.194.130]
ok: [192.168.194.131]

TASK [debug1] *******************************************************************************************************************************************************
ok: [192.168.194.129] => {
    "msg": [
        "my name is root"
    ]
}
ok: [192.168.194.131] => {
    "msg": [
        "my name is root"
    ]
}
ok: [192.168.194.130] => {
    "msg": [
        "my name is root"
    ]
}

PLAY RECAP **********************************************************************************************************************************************************
192.168.194.129            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.194.130            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.194.131            : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@clinet ansible_2]# 

2.7    password

         password lookup会随机生成一个密码,并将这个密码写入到参数指定的文件中。

示例: 随机生成一个密码,并保存到xhz_password.txt文件中。 

- hosts: init_server
  gather_facts: no

  tasks:
    - name: loopup with pipe
      set_fact:
        password_value: "{
   
   { lookup('password', 'xhz_password.txt') }}"

    - name: debug1
      debug:
        msg:
          - "{
   
   { password_value }}"

 2.8    dnstxt

        dnstxt lookup用于获取指定域名的TXT记录。需要在主控端安装python-dns。

示例:生成一个aliyun.com的A记录解析。

- hosts: init_server
  gather_facts: no

  tasks:
    - name: loopup with pipe
      set_fact:
        dnstxt_valus: "{
   
   { lookup('dnstxt', 'aliyun.com') }}"

    - name: debug1
      debug:
        msg:
          - "{
   
   { dnstxt_valus }}"

 2.9    redis_kv

        redis_kv lookup 可以直接从redis存储中来获取一个key的value,key必须是一个字符串,如同 Redis GET指令一样。需要注意的是,要使用 redis_kv lookup ,需要在主控端安装python的redis客 户端,在centos上,软件包为python-redis 。

示例:从本地的redis中取中一个key为weather的值。 

- hosts: init_server
  gather_facts: no

  tasks:
    - name: loopup with pipe
      set_fact:
        redis_value: "{
   
   { lookup('redis_kv', 'redis://localhost:6379,weather') }}"

    - name: debug1
      debug:
        msg:
          - "{
   
   { redis_value }}"

 注意:

        URL部分如果不指定,该模块会默认连接到 redis://localhost:6379 。因此可以写为:{ { lookup('redis_kv', 'weather')}}


         这篇文章就输这么多把!当然lookup的的插件还有很多,上面只是说了一些平常有用到的,大家有用到哪些插件呢?欢迎分享!生活加油....

猜你喜欢

转载自blog.csdn.net/qq_43714097/article/details/128264952