[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的的插件还有很多,上面只是说了一些平常有用到的,大家有用到哪些插件呢?欢迎分享!生活加油....

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Ansible是一种自动化工具,可以用于配置和管理IT基础设施。在Ansible中,变量是一种用于存储和传递数据的机制。它们可以在playbooks(剧本)和roles(角色)中使用,以便根据需要自定义配置。 Ansible变量使用方式有以下几种: 1. 主机变量(Host Variables):可以为每个主机定义特定的变量。这些变量可以在inventory文件中定义,也可以在playbook中使用`vars`关键字定义。例如,在inventory文件中定义一个变量`my_var`: ``` [web_servers] web1 ansible_host=192.168.1.10 my_var=example_value ``` 然后在playbook中使用这个变量: ``` - name: Example playbook hosts: web_servers tasks: - name: Print variable debug: var: my_var ``` 2. 组变量(Group Variables):可以为一组主机定义共享的变量。这些变量可以在inventory文件中定义,也可以在playbook中使用`vars`关键字定义。例如,在inventory文件中定义一个变量`my_group_var`: ``` [web_servers] web1 ansible_host=192.168.1.10 [web_servers:vars] my_group_var=example_value ``` 然后在playbook中使用这个变量: ``` - name: Example playbook hosts: web_servers tasks: - name: Print variable debug: var: my_group_var ``` 3. 全局变量(Global Variables):可以在playbook中定义全局变量,以便在整个playbook中使用。这些变量可以使用`vars`关键字定义。例如,在playbook中定义一个全局变量`my_global_var`: ``` - name: Example playbook hosts: all vars: my_global_var: example_value tasks: - name: Print variable debug: var: my_global_var ``` 4. Facts变量Ansible会自动收集关于主机的信息,并将其存储在facts变量中。这些变量可以在playbook中使用,例如: ``` - name: Example playbook hosts: all tasks: - name: Print facts debug: var: ansible_facts ``` 以上是Ansible变量的几种使用方式。通过使用这些变量,您可以根据需要自定义配置,并实现更灵活和可重用的自动化部署。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小肖同学..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值