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: init
  roles: 
    - ../roles/init

vim roles/init/tasks/main.yml

---
# 关闭防火墙
- name: Stop_firewalld
  service:
    name: firewalld
    state: stopped
    enabled: no
  when: ansible_distribution == "CentOS" or ansible_distribution == "Rocky"

# 临时关闭seLinux 并配置重启后永久关闭
- name: Setenforce_0
  shell: "setenforce 0"
  failed_when: false
  when: ansible_distribution == "CentOS" or ansible_distribution == "Rocky"

- name: Set_selinux_disabled_centos7
  replace:
    path: /etc/selinux/config
    regexp: '^SELINUX=.*'
    replace: 'SELINUX=disabled'
    backup: yes
#    ingnore_errors: true
  when: ansible_distribution == "CentOS" or ansible_distribution == "Rocky"

- name: Set_selinux_disabled_rockylinux9
  shell: "grubby --update-kernel ALL --args selinux=0"
  when:
    - ansible_distribution == "Rocky"
    - ansible_distribution_major_version == "9"
# 配置时区
- name: Set_timezone
  shell: "timedatectl set-timezone 'Asia/Shanghai'"
# 配置主机名
- name: Set_hostname
  shell: "hostnamectl set-hostname {{hostname}}"

执行playbook 初始化命令

ansible-playbook -i hosts deployment/init.yml

posted on 2025-10-28 11:07  luokeli  阅读(0)  评论(0)    收藏  举报

导航