ansible服务
填写hosts指定主机范围和控制节点后创建一个脚本,可以利用简化脚本
1. 在linux1上安装系统自带的ansible-core,作为ansible控制节点,linux2-linux7作为ansible的受控节点
Linux1
Linux1-7 Yum install ansible-core -y
Vi /etc/ansible/hosts
添加
[linux1] linux1.skills.lan [linux2-linux7] linux2.skills.lan linux3.skills.lan linux4.skills.lan linux5.skills.lan linux6.skills.lan linux7.skills.lan
在linux1编写/root/skills.yul剧本,仅在linux1节点运行,实现如下功能:为主控节点和受控节点同时安装lrzsz软件
vim /etc/ansible/ansible.cfg添加忽略验证 [defaults] host_key_checking = False
**[root@linux1 ~]#
ansible-playbook /root/skills.yml
(2)编写/root/host.yml 剧本,实现在所有linux主机的/root 目录下创建一个host.txt 文件,文件内容为:“ansible脚本测试,主机名为:hostname”,hostname为各对应主机的FQDN全名。
--- - hosts: alltasks:- name: 1file:path: /root/host.txtstate: touch - name: 1lineinfile:path: /root/host.txtline: "Hostname is: {{ ansible_fqdn }}"create: yes
- hosts: servergather_facts: notasks:- name: touch filefile: path="/root/ansible.txt" state=touch - hosts: clientgather_facts: notasks:- name: copy filecopy: src="/root/ansible.txt" dest="/root/"
2022国赛
5.在linux1 上安装ansible,作为ansible的控制节点。linux2-linux7作 为ansible 的受控节点。在linux1编写/root/skills.yaml剧本,实现在linux1 创建文件/root/ansible.txt,并将该文件复制到所有受控节点的/root目录。
- hosts: alltasks:- name: 123copy:content: "123"dest: /root/ansible.txt
在linux1编写cron.yaml脚本实现linux2-linux7每格一小时定时备份/var/log/message/到data_log下,备份策略为递归
- hosts: alltasks:- name: Ensure directory exists for logfile:path: /data_logstate: directory - name: Schedule cron job for log rotationcron:name: Rotate Logminute: "0"hour: "*/1"day: "*"month: "*"weekday: "*"job: "cat /var/log/messages >> /data_log/message && echo '' > /var/log/messages"
竞赛培训请联系主页!
https://gw.mobei.space