ansible Roles与优化

目录

[1.include 文件包含](#1.include 文件包含)

1.nfs_server_tasks.yml

2.nfs_client_tasks.yml

3.main_nfs.yml

2.Roles

3.vault

4.Galaxy

5.优化

5.1性能优化

5.2安全


1.include 文件包含

当一个ansible剧本的内容过多时 涉及到多个play时 较为复杂时 可以通过include把小的剧本拼接成大的剧本

例如我们可以将NFS 服务客户端和服务端 两个剧本写到一个剧本中通过include_tasks引入

例如

1.nfs_server_tasks.yml

复制代码
---
- name: Install NFS server on CentOS
  yum:
    name: nfs-utils
    state: present

- name: Start and enable NFS service
  service:
    name: nfs-server
    state: started
    enabled: true

- name: Configure firewall for NFS on CentOS
  firewalld:
    service: nfs
    permanent: true
    state: enabled
    immediate: true

2.nfs_client_tasks.yml

复制代码
---
- name: Install NFS client on CentOS
  yum:
    name: nfs-utils
    state: present

- name: Create mount point directory on client
  file:
    path: /mnt/nfs
    state: directory

- name: Mount NFS share on client
  mount:
    name: /mnt/nfs
    src: "{{ nfs_server_ip }}:/export/nfs"
    fstype: nfs
    opts: defaults

3.main_nfs.yml

复制代码
---
- name: Configure NFS client and server on CentOS
  hosts: all
  become: true

  tasks:
    - name: Install NFS packages on server
      yum:
        name:
          - nfs-utils
          - nfs-utils-lib
          - nfs-kernel-server
      when: inventory_hostname in groups['nfs-server']

    - name: Create export directory on server
      file:
        path: /export/nfs
        state: directory
      when: inventory_hostname in groups['nfs-server']

    - name: Set up NFS server exports
      nfs_export:
        path: /export/nfs
        clients:
          - client_ip_or_network/24(rw,sync,no_subtree_check)
      when: inventory_hostname in groups['nfs-server']

    - name: Restart NFS server
      service:
        name: nfs-server
        state: restarted
      when: inventory_hostname in groups['nfs-server']

    - name: Install NFS client packages on client
      yum:
        name: nfs-utils
      when: inventory_hostname in groups['nfs-client']

  # Include tasks for NFS server configuration
  - include_tasks: nfs_server_tasks.yml
    when: inventory_hostname in groups['nfs-server']

  # Include tasks for NFS client configuration
  - include_tasks: nfs_client_tasks.yml
    when: inventory_hostname in groups['nfs-client']

2.Roles

当我们使用剧本时发现 存放混乱这时候我们可以使用 Roles让文件剧本存放的更为简洁明了

实际上就是换个地方存放目录更为简洁 更为规范 是一种模块化思想

3.vault

加密指定文件 ansible-vault用于加密高敏感信息

hosts 加密

变量文件加密

4.Galaxy

在官网上下载别人写好的剧本 进行使用十分方便简洁

5.优化

5.1性能优化

ssh连接速度优化,关闭UseDNS,GSSAPIAuthcation ...

不要让ansible运行交互式的命令,非要用使用命令的非交互模式

需要使用ans,yum安装软件,可以自建本地yum仓库,然后ans安装.(自建yum源,自己制作的rpm包)

调整ansible并发数量(-f 调整并发数量 默认是5 ansible.cfq forks=5,实际调整根据负载情况。)

给ansible配置缓存(redis)队列.缓存facts.

给主机进行分组操作与管理.

关闭gather_facts,如果不用facts变量可以关闭,剧本中:qather_facts: false配置文件:gathering=explicit

关闭host,key,check 一般使用密码认证的时候需要关闭,如果不关闭 ansible配置文件host key checking = False

5.2安全

配置sudo用户 ans ALL=(ALL) NOPASSWD:ALL 密码是1,ssh端口是 22

配合vpn,jms一起使用

用户--->vpn---->ims(跳板机)---->ansible。

用户的密码,进行加密( hash,ansible-vault)

相关推荐
tianyuanwo2 天前
Ansible自动化运维全解析:从设计哲学到实战演进
运维·自动化·ansible
哆啦A梦15882 天前
在golang中如何将已安装的依赖降级处理,比如:将 go-ansible/[email protected] 更换为 go-ansible/@v1.1.7
开发语言·golang·ansible
lifeng43212 天前
在 CentOS 上将 Ansible 项目推送到 GitHub 的完整指南
centos·github·ansible
运维成长记7 天前
ansible-playbook 进阶 接上一章内容
linux·服务器·ansible
半桶水专家8 天前
Ansible 配置Playbook文件格式、关键字和语法详解
ansible
遇见火星10 天前
Ansible模块——Ansible配置文件!
linux·git·ansible
安顾里11 天前
Ansible安装
linux·运维·自动化·ansible
爱莉希雅&&&13 天前
运维Linux之Ansible详解学习(更新中)
linux·运维·ansible
筑梦之路19 天前
centos 9 Kickstart + Ansible自动化部署 —— 筑梦之路
centos·自动化·ansible
遇见火星19 天前
Ansible模块——通过 URL 下载文件
ansible