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