当前位置: 首页> 娱乐> 影视 > 快看看小程序入口_南京市网站seo整站优化_营销咨询公司排名前十_seo一个关键词多少钱

快看看小程序入口_南京市网站seo整站优化_营销咨询公司排名前十_seo一个关键词多少钱

时间:2025/7/14 14:17:49来源:https://blog.csdn.net/qq_50247813/article/details/144334508 浏览次数:0次
快看看小程序入口_南京市网站seo整站优化_营销咨询公司排名前十_seo一个关键词多少钱
  1. 需求一: 要在所有的远程主机上执行一下收集脚本,并在控制台打印出来
    脚本如下:
[root@192 Playbooks]# cat CollectServerInformation.sh
#!/bin/bash
# Collect server information
echo "Collecting server information..."# OS Information
echo "Operating System Information:"
cat /etc/*release# Kernel Version
echo "Kernel Version:"
uname -recho "Network Configuration:"
ip -4 a

编辑ansible-playbook
v1版本:

[root@192 Playbooks]# cat AnsiblePlay-CollectServerInformation.yaml
---- name: Execute scriptgather_facts: nobecome: yesbecome_user: roothosts: alltasks:- name: Execute scriptscript:cmd: /opt/Playbooks/CollectServerInformation.sh
  • gather_facts: no : 不搜集主机 facts 可以加快执行速度
  • become: yes : 是否使用sudo
  • become_user: root :切换的用户

执行效果:

[root@192 Playbooks]# ansible-playbook AnsiblePlay-CollectServerInformation.yaml

在这里插入图片描述
可以看出虽然脚本执行成功了,但是并没有在控制台打印出来。

v2版本:

[root@192 Playbooks]# cat AnsiblePlay-CollectServerInformation.yaml
---- name: Execute scriptgather_facts: nobecome: yesbecome_user: roothosts: alltasks:- name: Execute scriptscript:cmd: /opt/Playbooks/CollectServerInformation.shregister: script_result- name: Output resultdebug:msg: "{{ script_result.stdout  }}"
  • register: script_result : 将脚本的值注册到 script_result 中
    debug: # 调用debug模块打印 script_result 变量中的内容
    msg: “{{ script_result.stdout }}”

在这里插入图片描述
2. 需求二: 要求把脚本的输出结果写入到ansible主机,并以远程主机名命名
这里的远程主机名指定是 inventory 清单中的主机名
如下:
在这里插入图片描述

编写ansible-play:

---
- name: Execute remote script and save output on Ansible control nodehosts: allgather_facts: nobecome: yesbecome_user: roottasks:- name: Execute script on remote hosts and save output to a temporary filescript:cmd: /opt/Playbooks/hardware_info.shregister: script_result- name: Save script output with hostname as filenamecopy:content: "{{ script_result.stdout }}"dest: "/root/{{ inventory_hostname }}_output.txt"when: script_result.stdout is defined- name: Fetch script output from remote hostsfetch:src: "/root/{{ inventory_hostname }}_output.txt"dest: "/opt/Playbooks/output/"flat: yeswhen: script_result.stdout is defined	

上述ansible-play中定义了3个 task
task1:
将脚本的输出结果注册到变量script_result
task2:
将远程主机的script_result.stdout 中的内容复制到 /root/{{ inventory_hostname }}_output.txt 文件,inventory_hostname 指定是在ansible清单中,对应的主机名。
task3:
将远程主机上/root/{{ inventory_hostname }}_output.txt 文件拷贝到ansible主机 /opt/Playbooks/output/ 目录下这里最后面记得加上/

  • when: script_result.stdout is defined : 在执行task前先判断 script_result.stdout 变量是否被定义
关键字:快看看小程序入口_南京市网站seo整站优化_营销咨询公司排名前十_seo一个关键词多少钱

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: