ansible系列(21)--ansible的变量注册Register
1. 变量注册Register
register 关键字可以将某个 task 任务结果存储至变量中,最后使用 debug模块 输出变量内容,可以用于后续排障;
示例一:
register的基本使用:#playbook文件:
[root@xuzhichao ansible_var]# cat test7.yml
---
- hosts: 192.168.20.23
remote_user: root tasks:
- name: Shell Command
shell: netstat -ntlp
register: port_status - name: Get Port Status
debug: msg={{ port_status.stdout_lines }} <==stdout_lines作用是只输出命令的内容。
#运行playbook:
[root@xuzhichao ansible_var]# ansible-playbook test7.yml PLAY [192.168.20.23] ***************************************************************************************************************************************** TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.20.23] TASK [Shell Command] *****************************************************************************************************************************************
changed: [192.168.20.23] TASK [Get Port Status] ***************************************************************************************************************************************
ok: [192.168.20.23] => {
"msg": [
"Active Internet connections (only servers)",
"Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name ",
"tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 668/rpcbind ",
"tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 15615/rpc.mountd ",
"tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8981/nginx: master ",
"tcp 0 0 0.0.0.0:40372 0.0.0.0:* LISTEN - ",
"tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1112/sshd ",
"tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1288/master ",
"tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 8981/nginx: master ",
"tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN - ",
"tcp 0 0 0.0.0.0:55651 0.0.0.0:* LISTEN 1122/rpc.statd ",
"tcp6 0 0 :::111 :::* LISTEN 668/rpcbind ",
"tcp6 0 0 :::40688 :::* LISTEN - ",
"tcp6 0 0 :::20048 :::* LISTEN 15615/rpc.mountd ",
"tcp6 0 0 :::56211 :::* LISTEN 1122/rpc.statd ",
"tcp6 0 0 :::22 :::* LISTEN 1112/sshd ",
"tcp6 0 0 ::1:25 :::* LISTEN 1288/master ",
"tcp6 0 0 :::2049 :::* LISTEN - "
]
} PLAY RECAP ***************************************************************************************************************************************************
192.168.20.23 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
示例二:使用
register关键字完成jumpserver key的创建;[root@xuzhichao ansible_var]# cat test8.yaml
- hosts: webservers
tasks: - name: Run Shell Command Random string
shell:
cmd: 'if ! grep "SECRET_KEY" ~/.bashrc; then
SECRET_KEY=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 50`;
echo "SECRET_KEY=$SECRET_KEY" >> ~/.bashrc;
echo $SECRET_KEY;
else
echo $SECRET_KEY;
fi'
register: SECRET_KEY - name: Run Shell Command BOOTSTRAP_TOKEN
shell:
cmd: 'if ! grep "BOOTSTRAP_TOKEN" ~/.bashrc; then
BOOTSTRAP_TOKEN=`cat /dev/urandom | tr -dc A-Za- z0-9 | head -c 16`;
echo "BOOTSTRAP_TOKEN=$BOOTSTRAP_TOKEN" >> ~/.bashrc;
echo $BOOTSTRAP_TOKEN;
else
echo $BOOTSTRAP_TOKEN;
fi'
register: BOOTSTRAP_TOKEN - name: Copy Jms Configure
template:
src: ./j-config.yml
dest: /tmp/jms_config.yml - name: Copy Koko Configure
template:
src: ./k-config.yml
dest: /tmp/koko_config.yml # 配置文件中:
SECRET_KEY: {{ SECRET_KEY.stdout.split('=')[1] }}
BOOTSTRAP_TOKEN: {{ BOOTSTRAP_TOKEN.stdout.split('=')[1] }}
ansible系列(21)--ansible的变量注册Register的更多相关文章
- Ansible系列(六):各种变量定义方式和变量引用
本文目录:1.1 ansible facts1.2 变量引用json数据的方式 1.2.1 引用json字典数据的方式 1.2.2 引用json数组数据的方式 1.2.3 引用facts数据1.3 设 ...
- Ansible系列(五):各种变量定义方式和变量引用
Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.1 ansible facts facts组件是用来收集被管理节点信息的 ...
- Ansible系列(六):循环和条件判断
本文目录:1. 循环 1.1 with_items迭代列表 1.2 with_dict迭代字典项 1.3 with_fileglob迭代文件 1.4 with_lines迭代行 1.5 with_ne ...
- Ansible系列(五):playbook应用和roles自动化批量安装示例
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Ansible系列(三):YAML语法和playbook写法
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Ansible系列(七):执行过程分析、异步模式和速度优化
本文目录:1.1 ansible执行过程分析1.2 ansible并发和异步1.3 ansible的-t选项妙用1.4 优化ansible速度 1.4.1 设置ansible开启ssh长连接 1.4. ...
- Ansible系列(四):playbook应用和roles自动化批量安装示例
Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html playbook是ansible实现批量自动化最重要的手段.在其中可以使用变 ...
- Ansible系列(二):选项和常用模块
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Ansible系列(一):基本配置和使用
本文目录:1.1 安装Ansible1.2 配置Ansible 1.2.1 环境配置 1.2.2 SSH互信配置 1.2.3 简单测试1.3 inventory Ansible是一种批量.自动部署工具 ...
- SpringBoot系列教程web篇Servlet 注册的四种姿势
原文: 191122-SpringBoot系列教程web篇Servlet 注册的四种姿势 前面介绍了 java web 三要素中 filter 的使用指南与常见的易错事项,接下来我们来看一下 Serv ...
随机推荐
- 福州大学MEM 备考总结
自己的基本情况 2022年8月2日,当天觉得休息的差不多了,思来想去,觉得考研是个不错的选择,和女朋友聊了一下,得到她的支持,于是乎定下目标.接着就是开始在网络上查找相关的材料,先把要报考高校和专业的 ...
- Linux下Bochs,NASM安装和使用
安装环境 以Ubuntu为例,先更新一下: sudo apt-get update sudo apt-get upgrade 然后安装Bochs环境: sudo apt-get install bui ...
- Visual Studio 2022的安装 - 编程手把手系列文章
工欲善其事,必先利其器. 今天讲的是编程开发工具Visual Studio 2022的安装.作为手把手系列的开始,需要先对进行编程所使用的工具进行了解.此博文从下面几个步骤入手,对VS 2022这个开 ...
- #博弈论#HDU 2516 取石子游戏
题目 \(n\)个石子,两人轮流取.先取者第1次可以取任意多个, 但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍. 取完者胜.先取者负输出"Second win".先取 ...
- Array and Set work process
目录 Array work principle 分析Array操作步骤数 read find insert delete Set work principle 分析Set操作步骤数 read find ...
- C# Replace方法
例子: string tStw = "Run Status"; string tStw1 = tStw.Replace("Run Status", " ...
- RabbitMQ 05 直连模式-Spring Boot操作
Spring Boot集成RabbitMQ是现在主流的操作RabbitMQ的方式. 官方文档:https://docs.spring.io/spring-amqp/docs/current/refer ...
- 第十六篇:jQuery基础
一.jQuery和Dom的关系 http://jquery.cuishifeng.cn/ 模块,类库 DOM/BOM/JavaScript的类库: 二.jQuery选择器 1.查找元素 DOM: 10 ...
- Luogu P3007 奶牛议会
观前须知 本题解使用 CC BY-NC-SA 4.0 许可. 同步发布于 Luogu 题解区. 更好的观看体验 请点这里. 笔者的博客主页 正文 Luogu P3007 [USACO11JAN] Th ...
- 基于 Serverless 打造如 Windows 体验的个人专属家庭网盘
简介:虽然现在市面上有些网盘产品, 如果免费试用,或多或少都存在一些问题, 可以参考文章<2020 国内还能用的网盘推荐>.本文旨在使用较低成本打造一个 "个人专享的.无任何限 ...