当前位置: 首页 > news >正文

ansible init 初始化实例

ansible init 初始化实例

解耦的目录结构

hosts
deployment- init.yml
roles- init- tasks-main.yml- templater- files

hosts

[init]
192.168.106.130 hostname=rocky90-106-130[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=kc@123456
ansible_ssh_port=22

vim deployment/init.yml

---
- hosts: initroles: - ../roles/init

vim roles/init/tasks/main.yml

---
# 关闭防火墙
- name: Stop_firewalldservice:name: firewalldstate: stoppedenabled: nowhen: ansible_distribution == "CentOS" or ansible_distribution == "Rocky"# 临时关闭seLinux 并配置重启后永久关闭
- name: Setenforce_0shell: "setenforce 0"failed_when: falsewhen: ansible_distribution == "CentOS" or ansible_distribution == "Rocky"- name: Set_selinux_disabled_centos7replace:path: /etc/selinux/configregexp: '^SELINUX=.*'replace: 'SELINUX=disabled'backup: yes
#    ingnore_errors: truewhen: ansible_distribution == "CentOS" or ansible_distribution == "Rocky"- name: Set_selinux_disabled_rockylinux9shell: "grubby --update-kernel ALL --args selinux=0"when:- ansible_distribution == "Rocky"- ansible_distribution_major_version == "9"
# 配置时区
- name: Set_timezoneshell: "timedatectl set-timezone 'Asia/Shanghai'"
# 配置主机名
- name: Set_hostnameshell: "hostnamectl set-hostname {{hostname}}"

执行playbook 初始化命令

ansible-playbook -i hosts deployment/init.yml