RHCE-8-管理变量和事实/任务控制
管理变量和事实
1、使用debug模块,显示当前受管主机的dns服务器的ip地址。
yaml配置
---
- name: Obtaining the dnshosts: alltasks:- name: print dnsdebug:var: ansible_facts.dns.nameservers
执行结果
[xiaoming@master-85 ~]$ ansible-playbook dns.yml PLAY [Obtaining the dns] *****************************************************************************************************************************************************************************************TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]TASK [print dns] *************************************************************************************************************************************************************************************************
ok: [node1] => {"ansible_facts.dns.nameservers": ["192.168.21.2"]
}
ok: [node2] => {"ansible_facts.dns.nameservers": ["192.168.21.2"]
}PLAY RECAP *******************************************************************************************************************************************************************************************************
node1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2、将createuser.fact文件传输到受管主机上作为自定义事实变量文件(/etc/ansible/facts.d/),该文件的内容如下:
文件内容
[general]
username = wujing
mima=$6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.
配置文件
---
- name: touch /etc/ansible/facts.d/createuser.facthosts: alltasks:- name: facts.dfile:path: /etc/ansible/facts.dstate: directory- name: copy contentcopy:content: |[general]username=wujingmima=$6$UAxRbhT3kyc=$AxQfYYP8dhCv750tH.rmrmv690ugT/lZU8OGEqSs7xZR0rEvSIurs4w/W88wUiY3hNnZBWS4uCaGUCdztI9An.dest: /etc/ansible/facts.d/createuser.fact- name:user:name: "{{ ansible_facts.ansible_local.createuser.general.username }}"password: "{{ ansible_facts.ansible_local.createuser.general.mima }}"- name:command: id {{ ansible_facts.ansible_local.createuser.general.username }}register: whoname- debug:var: whoname
执行结果
[xiaoming@master-85 ~]$ ansible-playbook user.yml PLAY [touch /etc/ansible/facts.d/createuser.fact] ***************************************************************************************************************************************************************TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]TASK [facts.d] ***************************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]TASK [copy content] **********************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]TASK [user] ******************************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]TASK [command] ***************************************************************************************************************************************************************************************************
changed: [node2]
changed: [node1]TASK [debug] *****************************************************************************************************************************************************************************************************
ok: [node1] => {"whoname": {"changed": true,"cmd": ["id","wujing"],"delta": "0:00:00.004530","end": "2022-11-26 15:17:56.484263","failed": false,"rc": 0,"start": "2022-11-26 15:17:56.479733","stderr": "","stderr_lines": [],"stdout": "uid=2002(wujing) gid=2002(wujing) groups=2002(wujing)","stdout_lines": ["uid=2002(wujing) gid=2002(wujing) groups=2002(wujing)"]}
}
ok: [node2] => {"whoname": {"changed": true,"cmd": ["id","wujing"],"delta": "0:00:00.002787","end": "2022-11-26 15:17:56.714160","failed": false,"rc": 0,"start": "2022-11-26 15:17:56.711373","stderr": "","stderr_lines": [],"stdout": "uid=2002(wujing) gid=2002(wujing) groups=2002(wujing)","stdout_lines": ["uid=2002(wujing) gid=2002(wujing) groups=2002(wujing)"]}
}PLAY RECAP *******************************************************************************************************************************************************************************************************
node1 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3、向受管主机的/home/file文件里面写入内容如下:
内容
hostname=当前主机的名字
memory=当前主机的内存大小
BIOS version=当前主机的bios的版本
distribution=当前linux主机的发行版本信息
Size of disk device is 当前主机的磁盘大小
配置文件
---
- name: /home/file to node1hosts: alltasks:- name: file to Redhat8copy:content: |hostname={{ ansible_facts.hostname }}memory={{ ansible_facts.memtotal_mb }}BIOS version={{ ansible_facts.bios_version }}distribution={{ ansible_facts.distribution }}Size of disk device is {{ ansible_facts.devices.nvme0n1.size }}dest: /home/filewhen:- ansible_distribution == "RedHat"- ansible_distribution_major_version == "8"- name: cat file1command: cat /home/fileregister: file1when:- ansible_distribution == "RedHat"- ansible_distribution_major_version == "8"- debug:var: file1when:- ansible_distribution == "RedHat"- ansible_distribution_major_version == "8"- name: /home/file to node2hosts: alltasks:- name: file to Centos7copy:content: |hostname={{ ansible_facts.hostname }}memory={{ ansible_facts.memtotal_mb }}BIOS version={{ ansible_facts.bios_version }}distribution={{ ansible_facts.distribution }}Size of disk device is {{ ansible_facts.devices.sda.size }}dest: /home/filewhen:- ansible_distribution == "CentOS"- ansible_distribution_major_version == "7"- name: cat file2command: cat /home/fileregister: file2when:- ansible_distribution == "CentOS"- ansible_distribution_major_version == "7"- debug:var: file2when:- ansible_distribution == "CentOS"- ansible_distribution_major_version == "7"
执行结果
[xiaoming@master-85 ~]$ ansible-playbook copy.yml PLAY [/home/file to node1] *****************************************************************************************************************************************************************************************************************TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]TASK [file to Redhat8] *********************************************************************************************************************************************************************************************************************
skipping: [node2]
ok: [node1]TASK [cat file1] ***************************************************************************************************************************************************************************************************************************
skipping: [node2]
changed: [node1]TASK [debug] *******************************************************************************************************************************************************************************************************************************
ok: [node1] => {"file1": {"changed": true,"cmd": ["cat","/home/file"],"delta": "0:00:00.002454","end": "2022-11-27 21:58:29.977069","failed": false,"rc": 0,"start": "2022-11-27 21:58:29.974615","stderr": "","stderr_lines": [],"stdout": "hostname=node-1\nmemory=1790\nBIOS version=6.00\ndistribution=RedHat \nSize of disk device is 20.00 GB","stdout_lines": ["hostname=node-1","memory=1790","BIOS version=6.00","distribution=RedHat ","Size of disk device is 20.00 GB"]}
}
skipping: [node2]PLAY [/home/file to node2] *****************************************************************************************************************************************************************************************************************TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]TASK [file to Centos7] *********************************************************************************************************************************************************************************************************************
skipping: [node1]
ok: [node2]TASK [cat file2] ***************************************************************************************************************************************************************************************************************************
skipping: [node1]
changed: [node2]TASK [debug] *******************************************************************************************************************************************************************************************************************************
skipping: [node1]
ok: [node2] => {"file2": {"changed": true,"cmd": ["cat","/home/file"],"delta": "0:00:00.002673","end": "2022-11-27 21:58:31.753503","failed": false,"rc": 0,"start": "2022-11-27 21:58:31.750830","stderr": "","stderr_lines": [],"stdout": "hostname=node-2\nmemory=1823\nBIOS version=6.00\ndistribution=CentOS\nSize of disk device is 20.00 GB","stdout_lines": ["hostname=node-2","memory=1823","BIOS version=6.00","distribution=CentOS","Size of disk device is 20.00 GB"]}
}PLAY RECAP *********************************************************************************************************************************************************************************************************************************
node1 : ok=5 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
node2 : ok=5 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
任务控制
1、如果当前受管主机的根分区容量大于1G,则安装httpd和mariadb-server软件包,如果httpd和mariadb服务未运行则运行该服务。
配置文件
---
- name: pan duan /hosts: alltasks:- name: install packagesyum:name: "{{ packages }}"vars:packages:- httpd- mariadb-serverloop: "{{ ansible_mounts }}"when:- item.mount == "/"- item.size_available > 1024*1024*1024- name: start packagesservice:name: "{{ item }}"enabled: truestate: startedloop:- httpd- mariadb
执行结果
[xiaoming@master-85 ~]$ ansible-playbook server.yml PLAY [pan duan /] **************************************************************************************************************************************************************************************************************************TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]TASK [install packages] ********************************************************************************************************************************************************************************************************************
skipping: [node2] => (item={'block_used': 39957, 'uuid': '4ec34393-baab-4005-875a-eb662814fd65', 'size_total': 206213120, 'block_total': 50345, 'mount': '/boot', 'block_available': 10388, 'size_available': 42549248, 'fstype': 'xfs', 'inode_total': 83552, 'options': 'rw,seclabel,relatime,attr2,inode64,noquota', 'device': '/dev/sda1', 'inode_used': 328, 'block_size': 4096, 'inode_available': 83224})
ok: [node2] => (item={'block_used': 1967476, 'uuid': '5d2e1730-7a29-4ead-8251-08bf79501600', 'size_total': 21250441216, 'block_total': 5188096, 'mount': '/', 'block_available': 3220620, 'size_available': 13191659520, 'fstype': 'xfs', 'inode_total': 10381312, 'options': 'rw,seclabel,relatime,attr2,inode64,noquota', 'device': '/dev/mapper/centos-root', 'inode_used': 117933, 'block_size': 4096, 'inode_available': 10263379})
ok: [node1] => (item={'mount': '/', 'device': '/dev/mapper/rhel-root', 'fstype': 'xfs', 'options': 'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 20935868416, 'size_available': 15540719616, 'block_size': 4096, 'block_total': 5111296, 'block_available': 3794121, 'block_used': 1317175, 'inode_total': 10227712, 'inode_available': 10102457, 'inode_used': 125255, 'uuid': 'c531d66b-450c-4a7c-a094-227858955630'})
skipping: [node1] => (item={'mount': '/boot', 'device': '/dev/nvme0n1p1', 'fstype': 'xfs', 'options': 'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 518684672, 'size_available': 260423680, 'block_size': 4096, 'block_total': 126632, 'block_available': 63580, 'block_used': 63052, 'inode_total': 256000, 'inode_available': 255690, 'inode_used': 310, 'uuid': 'd5580ebf-c87b-4677-9164-a4c941565b89'}) TASK [start packages] **********************************************************************************************************************************************************************************************************************
ok: [node2] => (item=httpd)
ok: [node1] => (item=httpd)
changed: [node1] => (item=mariadb)
changed: [node2] => (item=mariadb)PLAY RECAP *********************************************************************************************************************************************************************************************************************************
node1 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [xiaoming@master-85 ~]$ ansible-playbook server.yml PLAY [pan duan /] **************************************************************************************************************************************************************************************************************************TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************
ok: [node1]
ok: [node2]TASK [install packages] ********************************************************************************************************************************************************************************************************************
skipping: [node2] => (item={'block_used': 39957, 'uuid': '4ec34393-baab-4005-875a-eb662814fd65', 'size_total': 206213120, 'block_total': 50345, 'mount': '/boot', 'block_available': 10388, 'size_available': 42549248, 'fstype': 'xfs', 'inode_total': 83552, 'options': 'rw,seclabel,relatime,attr2,inode64,noquota', 'device': '/dev/sda1', 'inode_used': 328, 'block_size': 4096, 'inode_available': 83224})
ok: [node2] => (item={'block_used': 1967486, 'uuid': '5d2e1730-7a29-4ead-8251-08bf79501600', 'size_total': 21250441216, 'block_total': 5188096, 'mount': '/', 'block_available': 3220610, 'size_available': 13191618560, 'fstype': 'xfs', 'inode_total': 10381312, 'options': 'rw,seclabel,relatime,attr2,inode64,noquota', 'device': '/dev/mapper/centos-root', 'inode_used': 117943, 'block_size': 4096, 'inode_available': 10263369})
ok: [node1] => (item={'mount': '/', 'device': '/dev/mapper/rhel-root', 'fstype': 'xfs', 'options': 'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 20935868416, 'size_available': 15529017344, 'block_size': 4096, 'block_total': 5111296, 'block_available': 3791264, 'block_used': 1320032, 'inode_total': 10227712, 'inode_available': 10102445, 'inode_used': 125267, 'uuid': 'c531d66b-450c-4a7c-a094-227858955630'})
skipping: [node1] => (item={'mount': '/boot', 'device': '/dev/nvme0n1p1', 'fstype': 'xfs', 'options': 'rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota', 'size_total': 518684672, 'size_available': 260423680, 'block_size': 4096, 'block_total': 126632, 'block_available': 63580, 'block_used': 63052, 'inode_total': 256000, 'inode_available': 255690, 'inode_used': 310, 'uuid': 'd5580ebf-c87b-4677-9164-a4c941565b89'}) TASK [start packages] **********************************************************************************************************************************************************************************************************************
ok: [node2] => (item=httpd)
ok: [node1] => (item=httpd)
ok: [node2] => (item=mariadb)
ok: [node1] => (item=mariadb)PLAY RECAP *********************************************************************************************************************************************************************************************************************************
node1 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2、将example.conf.j2文件复制到/etc/httpd/conf.d/目录,example.conf.j2文件内容如下:
文件
<virtualhost {{ ansible_facts.default_ipv4.address }}:80>
servername {{ ansible_facts.default_ipv4.address }}
documentroot /var/www/html
</virtualhost><directory /var/www/html>
allowoverride none
require all granted
</directory>
如果/etc/httpd/conf.d/目录下的文件更新,则重启httpd服务。配置/var/www/html/index.html文件内容如下:
zuoye
配置文件
---
- name: copy example.confhosts: alltasks:- name:template:src: /home/xiaoming/example.conf.j2dest: /etc/httpd/conf.d/example.confnotify:- restart httpd server- name:template:src: /home/xiaoming/index.htmldest: /var/www/html/index.htmlhandlers:- name: restart httpd serverservice:name: httpdstate: restarted
执行结果
[xiaoming@master-85 ~]$ ansible-playbook file-server.yml PLAY [copy example.conf] *****************************************************************************************************************************************************************************************************************************************************TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [node1]
ok: [node2]TASK [template] **************************************************************************************************************************************************************************************************************************************************************
changed: [node2]
changed: [node1]TASK [template] **************************************************************************************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]RUNNING HANDLER [restart httpd server] ***************************************************************************************************************************************************************************************************************************************
changed: [node1]
changed: [node2]PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
node1 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3、创建一个playbook,要求如下: 该playbook运行在所有受控节点 该playbook覆盖/etc/message文件的内容 在dev主机组的主机上,内容是:Development 在test主机组的主机上,内容是:Test
配置文件
---
- name: playhosts: alltasks:- name: copy Developmentcopy:content: "Development\n"dest: /etc/messagewhen: inventory_hostname in groups.dev- name: copy Testcopy:content: "Test\n"dest: /etc/messagewhen: inventory_hostname in groups.test- name: yan zhengcommand: cat /etc/messageregister: message- debug:var: message
执行结果
[xiaoming@master-85 ~]$ ansible-playbook play.yml PLAY [play] ******************************************************************************************************************************************************************************************************TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [node2]
ok: [node1]TASK [copy Development] ******************************************************************************************************************************************************************************************
skipping: [node2]
ok: [node1]TASK [copy Test] *************************************************************************************************************************************************************************************************
skipping: [node1]
ok: [node2]TASK [yan zheng] *************************************************************************************************************************************************************************************************
changed: [node2]
changed: [node1]TASK [debug] *****************************************************************************************************************************************************************************************************
ok: [node1] => {"message": {"changed": true,"cmd": ["cat","/etc/message"],"delta": "0:00:00.002647","end": "2022-11-26 17:45:12.265146","failed": false,"rc": 0,"start": "2022-11-26 17:45:12.262499","stderr": "","stderr_lines": [],"stdout": "Development","stdout_lines": ["Development"]}
}
ok: [node2] => {"message": {"changed": true,"cmd": ["cat","/etc/message"],"delta": "0:00:00.002167","end": "2022-11-26 17:45:12.576830","failed": false,"rc": 0,"start": "2022-11-26 17:45:12.574663","stderr": "","stderr_lines": [],"stdout": "Test","stdout_lines": ["Test"]}
}PLAY RECAP *******************************************************************************************************************************************************************************************************
node1 : ok=4 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node2 : ok=4 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
RHCE-8-管理变量和事实/任务控制相关推荐
- Ansible的管理变量、机密和事实
文章目录 管理变量.机密和事实 1.管理变量 1.1Ansible变量简介 1.1.1 命令变量 1.12 定义变量 1.2 playbook中的变量 1.2.1在Playbook中定义变量 1.2. ...
- python内存管理 变量无需事先声明_python 内存管理
内存管理 包括: 变量无须事先声明 变量无须指定类型 不用关心内存管理 变量名会被"回收" del 语句能够直接释放资源 变量定义 python中, 变量在第一次被赋值时自动声明, ...
- 5G RRC——为NAS层提供连接管理,消息传递等服务; 对接入网的底层协议实体提供参数配置的功能; 负责UE移动性管理相关的测量、控制等功能...
from:http://www.cnblogs.com/kkdd-2013/p/3868676.html 1 RRC协议功能 为NAS层提供连接管理,消息传递等服务: 对接入网的底层协议实体提供参数配 ...
- C语言删掉无关变量无输出,C语言变量类型与输出控制用法实例教程
本文实例讲述了C语言变量类型与输出控制用法,有助于读者很好的对其进行总结与归纳.该实例分享给大家供大家参考借鉴之用.具体如下: 完整实例代码如下: /************************* ...
- mac你没有权限打开应用程序_如何管理Mac的隐私权限控制
在使用MAC电脑清理软件的时候,经常会出现需要权限问题,在没有权限的情况下,我们不能对一些文件进行更改和删除,那么该如何管理Mac的隐私权限控制呢?下面的文章就来告诉大家该如何设置隐身权限问题. 第一 ...
- 【UiPath2022+C#】UiPath 练习和解决方案-变量、数据类型和控制流程
[UiPath2022+C#]UiPath 练习-变量.数据类型和控制流程 文章目录 [UiPath2022+C#]UiPath 练习-变量.数据类型和控制流程 环境 练习 练习 1 - 遍历循环和 ...
- [高项]管理干系人参与VS控制干系人参与
管理干系人参与 管理干系人参与是项目执行过程组的一个过程,是项目管理团队按照干系人管理计划 中的规定,应用沟通方法,人际关系技能和其它管理技能,来实实在在地与干系人打交道, 引导他们合理参与项目. 如 ...
- Uipath 认识变量及如何管理变量
Uipath 管理变量 文章目录 Uipath 管理变量 前言 一.认识变量 二.管理变量 1.创建变量 2.删除变量 前言 本节主要介绍Uipath Studio 变量使用及如何进行管理变量. 一. ...
- 作为技术总监对项目经理和项目组长管理以及对项目的控制(一)
作为技术总监,如果只负责一个项目,那么他必须对整个项目了解度到100%,无一遗漏,哪怕只是下属发的工作文档里有提到.但是技术总监身兼多个项目,而且大小不一,需求五花八门,那么可以根据实际而定. 作为项 ...
最新文章
- python __builtins__ complex类 (13)
- C++运行程序出现的一些问题
- MySQL—相关子查询
- 【杂谈】从学生到讲师,我如何20天里在有三AI赚3万
- MySQL-InnoDB究竟如何巧妙实现,4种事务的隔离级别
- VTK:标签放置映射器用法实战
- Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
- 计算机打印中 进纸盘2,软件、计算机和打印机接口问题-HP.PDF
- 聊聊Election Algorithms
- java jdk学习_Java学习第一步:JDK环境搭建(纯小白向)
- Android设备读写NFC标签
- uploadify组件文件上传那些事
- vue项目element-ui中el-select回车键隐藏下拉框,实现按回车键查询
- js根据身份证获取性别、年龄、出生日期及根据出生日期获取年龄
- 北京周末游周边 —— 延庆世园会
- bittorrent下载_面向初学者的BitTorrent:如何开始下载Torrent
- 安装wsl kali 遇到WslRegisterDistribution failed with error: 0x80070057 Error: 0x80070057解决
- 对女朋友说早安的情话100句,很甜很撩,打动人心
- SpringMVC源码分析迷你书
- KVM管理平台选型与开源企业级虚拟化平台oVirt详解
热门文章
- Could not resolve placeholder 占位符不能被解析
- 【LabVIEW懒人系列教程-小白入门】1.13LabVIEW程序结构之事件结构
- Ubuntu 14.04+Ros indigo+ORB_SLAM2 的平台搭建
- 误删了win10下面的winsock和winsock2
- CSS #38; JS
- 哪有那么多BAT的逆袭?
- B站 URL转16进制防止评论贴URL被屏蔽
- Bootstrap.yml 和 application.yml
- brpc源码分析——线程模型
- 天然肠衣数学建模matlab代码,数学建模天然肠衣搭配问题.doc