ansible-2

简介: ansible-2

ansible playbook:剧本

   由一个或多个模块组成,完成统一的目的,实现自动化操作

   剧本编写遵循yaml语法

   yaml的三要素:***

缩进:两个字符,默认的tab键是四个字符,所以要使用tab键,需要修改.vimrc

              vim /root/.vimrc

               添加:

               set tabstop=2

               保存退出

           

       冒号:冒号后面需要空格,除非以冒号结尾

       短横杠:列表项,后面跟空格

playbook语法结构

   ansible-playbook 选项 文件路径

   选项:

               -C  模拟预运行

               --list-hosts:列出清单

               --list-tasks:列出任务

               --list-tags:列出标签

               --syntax-check:语法检查

     


测试案例:通过playbook安装httpd,并修改端口号为8080

前提:安装httpd,复制httpd.conf到当前目录,修改端口为8080

vim httpd.yaml

添加:

- hosts: web
  tasks:
    - name: install httpd
      yum: name=httpd state=latest
    - name: httpd config
      copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: restart httpd
    - name: start httpd
      service: name=httpd state=started
  handlers:
    - name: restart httpd
      service: name=httpd state=restarted
保存退出
测试yaml: ansible-playbook -C httpd.yaml
执行yaml:ansible-playbook httpd.yaml


#####################################################################

playbook配置web--nfs--rsync架构环境

全局环境:修改各主机名:ansible、web、nfs、rsync

1.服务器配置

前提:

ifdown ens33;ifup ens33

systemctl stop firewalld

systemctl disable firewalld

setenforce 0


vim  /etc/hosts

192.168.8.10   ansible

192.168.8.20   web

192.168.8.30   nfs

192.168.8.40   rsync

保存退出


(1)安装ansible

   yum -y install epel-release

   yum -y install ansible

(2)ssh公钥

   ssh-keygen -t rsa

   ssh-copy-id root@web        #web服务器

   ssh-copy-id root@nfs        #nfs服务器

   ssh-copy-id root@rsync        #rsync服务器

(3)复制/etc/hosts到被管理端

   scp /etc/hosts root@web:/etc/

   scp /etc/hosts root@nfs:/etc/

   scp /etc/hosts root@rsync:/etc/

(4)创建ansible目录

   mkdir -p /etc/ansible/ansible_playbook/{conf,file,scripts,tools}

(5)创建ansible清单

vim /etc/ansible/hosts

添加:

[web]

192.168.8.20

 

[nfs]

192.168.8.30

 

[rsync]

192.168.8.40

 

保存退出


(6)使用ansible copy 复制/etc/hosts到所有主机

ansible all -m copy -a "src=/etc/hosts dest=/etc"



2.基础环境部署


(1)网络环境(关闭firewall  selinux)

(2)epel仓库

(3)安装rsync,nfs-utils

(4)创建组

(5)创建用户

(6)创建目录,并修改权限

(7)推送脚本

(8)推送rsync客户端密码文件,修改权限

(9)计划任务

vim /etc/ansible/ansible_playbook/base.yaml

添加:

- hosts: all
  tasks:
    - name: stop firewalld
      shell: systemctl stop firewalld
    - name: stop selinux 
      shell: setenforce 0 
    - name: clear repos.d
      file: path=/etc/yum.repos.d/ state=absent
    - name: create repos.d
      file: path=/etc/yum.repos.d/ state=directory
    - name: install base repo
      get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo
    - name: install epel repo
      get_url: url=http://mirrors.aliyun.com/repo/epel-7.repo dest=/etc/yum.repos.d/epel.repo
    - name: install rsync nfs-utils
      yum: name=rsync,nfs-utils state=installed
    - name: create group www
      group: name=www gid=666
    - name: create user www
      user: name=www uid=666 create_home=no shell=/sbin/nologin
    - name: create rsync client password
      copy: content='1' dest=/etc/rsync.pass mode=600
    - name: create scripts directory
      file: path=/server/scripts/ recurse=yes state=directory
    - name: push scripts
      copy: src=./scripts/rsync_backup.sh dest=/server/scripts
    - name: crontab
      cron: name="backup scripts" hour=01 minute=00 job="/usr/bin/bash /server/scripts/rsync_backup.sh &> /dev/null"
保存退出


3.rsync配置


(1)安装rsync

(2)配置

(3)启动

(4)脚本

(5)计划任务

vim /etc/ansible/ansible_playbook/rsync.yaml

添加:

- hosts: rsync
  tasks:
    - name: install rsync
      yum: name=rsync state=installed
    - name: config rsync
      copy: src=/etc/ansible/ansible_playbook/conf/rsyncd.conf dest=/etc/rsyncd.conf
      notify: restart rsync
    - name: create rsync local user
      copy: content='rsync_backup:1' dest=/etc/rsync.password mode=600
    - name: create data
      file: path=/data state=directory recurse=yes owner=www group=www mode=755
    - name: create backup
      file: path=/backup state=directory recurse=yes owner=www group=www mode=755
    - name: start rsync
      service: name=rsyncd state=started enabled=yes
    - name: push check scripts
      copy: src=./scripts/rsync_check.sh dest=/server/scripts
    - name: crond check scripts
      cron: name="check scripts" hour=05 minute=00 job="/usr/bin/bash /server/scripts/rsync_check.sh &> /dev/null"
  handlers:
    - name: restart rsync
      service: name=rsyncd state=restarted

保存退出


4.nfs部署


(1)安装nfs-utils

(2)配置

(3)启动

vim /etc/ansible/ansible_playbook/nfs.yaml

添加:

- hosts: nfs
  tasks:
    - name: install nfs
      yum: name=nfs-utils,rpcbind state=installed
    - name: config nfs
      copy: src=./conf/exports dest=/etc/exports
      notify: restart nfs
    - name: create data
      file: path=/data state=directory recurse=yes owner=www group=www mode=755
    - name: start nfs
      service: name=nfs-server state=started enabled=yes
  handlers:
    - name: restart nfs
      service: name=nfs-server state=restarted

保存退出


5.sersync部署


(1)在ansible服务器先下载sersync

(2)解压到/etc/ansible/ansible_playbook/并修改配置文件

(3)推送到nfs

(4)启动sersync

vim /etc/ansible/ansible_playbook/sersync.yaml

添加:

- hosts: nfs
  tasks:
    - name: scp sersync
      copy: src=./tools/sersync/ dest=/usr/local/sersync owner=www group=www mode=755
    - name: start sersync
      shell: pgrep sersync;
              [ $? -eq 0 ] || /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

保存退出


6.web部署


(1)本地安装httpd

(2)修改配置文件,复制到/etc/ansible/ansible_playbook/conf

(3)挂载

(4)启动

vim /etc/ansible/ansible_playbook/web.yaml

添加:

- hosts: web
  tasks:      
    - name: install httpd
      yum: name=httpd state=installed
    - name: mount nfs
      mount: src=nfs:/data path=/var/www/html fstype=nfs state=mounted      
    - name: config httpd
      copy: src=./conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: restart httpd
    - name: start httpd
      service: name=httpd state=started enabled=yes
  handlers:
    - name: restart httpd
      service: name=httpd state=restarted

保存退出


7.main.yaml

vim main.yaml

添加:

- import_playbook: base.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
- import_playbook: sersync.yaml
- import_playbook: web.yaml

保存退出


预检测:ansible-playbook -C main.yaml

执行:  ansible-playbook main.yaml                        



------------------------------------

ansible roles扩展参考:

https://blog.csdn.net/woshizhangliang999/article/details/106005990/


相关文章
|
7天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16778 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
1天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
6天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1201 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2280 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3775 22