community.general.redis_data 模块 – 在 Redis 中设置键值对

注意

此模块是 community.general 集合(版本 10.1.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.general。您需要更多要求才能使用此模块,请参阅 要求 了解详细信息。

要在 playbook 中使用它,请指定:community.general.redis_data

community.general 3.7.0 中的新增功能

概要

  • 在 Redis 数据库中设置键值对。

要求

执行此模块的主机上需要以下要求。

  • redis

  • certifi

参数

参数

注释

ca_certs

字符串

根证书文件的路径。如果未设置且 tls 设置为 true,将使用 certifi ca 证书。

client_cert_file

字符串

在 community.general 9.3.0 中添加

客户端证书文件的路径。

client_key_file

字符串

在 community.general 9.3.0 中添加

客户端私钥文件的路径。

existing

布尔值

仅在键已存在时设置键。

选项

  • false

  • true

expiration

整数

过期时间(毫秒)。设置此标志将始终导致数据库发生更改。

keep_ttl

布尔值

保留与键关联的生存时间。

选项

  • false

  • true

key

字符串 / 必需

数据库键。

login_host

字符串

指定运行数据库的目标主机。

默认值: "localhost"

login_password

字符串

指定用于身份验证的密码。

当目标是 localhost 时,通常不使用。

login_port

整数

指定要连接的端口。

默认值: 6379

login_user

字符串

指定用于身份验证的用户。

需要 redis >= 3.4.0。

non_existing

布尔值

仅在键尚不存在时设置键。

选项

  • false

  • true

state

字符串

键的状态。

选项

  • "present" ← (默认)

  • "absent"

tls

布尔值

指定是否对连接使用 TLS。

选项

  • false

  • true ← (默认)

validate_certs

布尔值

指定是否验证 TLS 证书。

仅应为个人控制的站点或以 localhost 作为目标时关闭此项。

选项

  • false

  • true ← (默认)

value

字符串

键应设置的值。

属性

属性

支持

描述

check_mode

支持: 完整

可以在 check_mode 中运行并返回更改状态预测,而无需修改目标。

diff_mode

支持:

在差异模式下运行时,将返回有关已更改内容(或可能需要在 check_mode 中更改的内容)的详细信息。

注意

注意

另请参阅

另请参阅

community.general.redis_data_incr

递增 Redis 中的键。

community.general.redis_data_info

获取 Redis 数据库中键的值。

community.general.redis

各种 redis 命令、副本和刷新。

示例

- name: Set key foo=bar on localhost with no username
  community.general.redis_data:
    login_host: localhost
    login_password: supersecret
    key: foo
    value: bar
    state: present

- name: Set key foo=bar if non existing with expiration of 30s
  community.general.redis_data:
    login_host: localhost
    login_password: supersecret
    key: foo
    value: bar
    non_existing: true
    expiration: 30000
    state: present

- name: Set key foo=bar if existing and keep current TTL
  community.general.redis_data:
    login_host: localhost
    login_password: supersecret
    key: foo
    value: bar
    existing: true
    keep_ttl: true

- name: Set key foo=bar on redishost with custom ca-cert file
  community.general.redis_data:
    login_host: redishost
    login_password: supersecret
    login_user: someuser
    validate_certs: true
    ssl_ca_certs: /path/to/ca/certs
    key: foo
    value: bar

- name: Delete key foo on localhost with no username
  community.general.redis_data:
    login_host: localhost
    login_password: supersecret
    key: foo
    state: absent

返回值

常见的返回值记录在 此处,以下是此模块特有的字段

描述

msg

字符串

简短的消息。

返回: 始终

示例: "将 键: foo 设置为 bar"

old_value

字符串

设置之前的键的值。

返回: on_success 如果 state=present 并且键存在于数据库中。

示例: "old_value_of_key"

value

字符串

键设置的值。

返回: 如果 state=present 成功,则返回。

示例: "new_value_of_key"

作者

  • Andreas Botzner (@paginabianca)