ansible安装、点对点Ad-Hoc、模块、剧本Playbook

news/2024/7/15 20:45:09 标签: ansible

DevOps:

官网:https://docs.ansible.com

自动化运维工具对比

C/S 架构:客户端/服务端

Puppet:基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱

SaltStack:基于 Python 开发,采用 C/S 架构,YAML使得配置脚本更简单.需要配置客户端及服务器端;每台被控制节点需要安装agent

Ansible:基于 Python开发,分布式,无需客户端,轻量级,配置语法使用YAML语言,更强的远程命令执行操作 (优点)

Ansible简介(是什么、做什么、特点)

ansible是新出现的自动化运维工具,基于Python开发,分布式,无需客户端,轻量级,实现了批量系统配置、批量程序部署、批量运行命令等功能,ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架

一、安装

245(ansible

vim /etc/hosts   (做解析)

IP+名字

配置ssh公钥认证:控制节点需要发送ssh公钥给所有非被控制节点(做免密)

ssh-keygen

yum list | grep ansible  //有ansible 仓库

yum install -y ansible   //下载ansible

ansible --version   //查看版本、配置文件、python版本

ansible --help   //帮助手册

二、主机清单inventory

vim /etc/ansible/hosts (要管理谁就写入谁)

web-1 //为主机

[db] 为主机组

查看组内主机列表

语法:ansible  组名  --list-hosts

ansible web --list-hosts   //查看组里的主机

[root@ansible-server ~]# ansible -i /opt/hostlist all -m ping -o
-i:指定清单文件

-m:调用模块

all:所有组

-o:改变输出格式

三:点对点Ad-Hoc

ansible-doc -l   //列出所有模块

ansible-doc -s yum   //yum的使用方法

用户管理模块:user

添加用户并设置密码:

[root@ansible-server ~]# ansible webservers1 -m user -a "name=liudehua password=`echo 1234 | openssl passwd -1 -stdin`" -o
"name=   "  #如:指定的用户名,要安装的软件
-1 MD5加密算法

删除用户:

[root@ansible-server ~]# ansible webservers1 -m user -a "name=liudehua state=absent" -o
adsent #删除用户,但是不会删除家目录

组管理模块:group 

gid:为组设置的可选GID
name:要管理的组的名称
state:该组是否应该存在于远程主机上;absent不在/默认是present在
system:如果是,表示创建的组是系统组;默认时no

[root@ansible-server ~]# ansible all -m group -a 'name=somegroup state=present'

[root@ansible-server ~]# ansible all -m group -a 'name=somegroup state=absent' //删除组

软件包管理模块:yum

config_file:yum的配置文件 
disable_gpg_check:关闭gpg_check 
disablerepo:不启用某个源 
enablerepo:启用某个源
name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径 
state:状态(present,absent,latest)

[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=latest" -o
state=     #状态是什么,干什么
state=absent        用于remove安装包
state=latest       表示最新的
state=removed      表示卸载

卸载软件:

[root@ansible-server ~]# ansible webservers1 -m yum -a "name=httpd state=removed" -o

服务管理模块:service

[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started" #启动
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=stopped" #停止
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=restarted" #重启
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=yes" #开机启动
[root@ansible-server ~]# ansible webservers1 -m service -a "name=httpd state=started enabled=no"  #开机关闭

文件模块:file

[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/88.txt mode=777 state=touch' #创建一个文件
[root@ansible-server ~]# ansible webservers1 -m file -a 'path=/tmp/99 mode=777 state=directory' #创建一个目录

收集信息模块:setup

[root@ansible-server ~]# ansible web1 -m setup -a 'filter=ansible_all_ipv4_addresses'

#只查询ipv4的地址
filter:过滤

[root@ansible-server ~]# ansible web1 -m setup -a 'filter=ansible_*_mb'       #内存的信息
[root@ansible-server ~]# ansible -i /home/ansible/hostlist web -m setup -a 'filter=ansible_processor_cores'       #磁盘的信息
[root@ansible-server ~]# ansible all -m setup --tree /tmp/facts 

文件复制模块:copy

[root@ansible-server ~]# ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"
[root@ansible-server ~]# ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"

计划任务模块:cron

获取每台主机的IP地址

[root@xingdian ~]# ansible -i /home/ansible/hostlist web -m shell -a "ip a | grep eth0| awk 'NR==2{print $2}'" -o > a.txt && cat a.txt |awk '{print $9,$NF}'

shell、command后跟在终端敲的命令

一个典型的例子就是 shell 和 command 模块. 这两个模块在很多情况下都能完成同样的工作, 以下是两个模块之前的区别:
command 模块命令将不会使用 shell 执行. 因此, 像 $HOME 这样的变量是不可用的。还有像 |,& 都将不可用
shell 模块通过shell程序执行, 默认是/bin/sh, <, >, |, ;, & 可用

获取每台主机的内存

[root@xingdian ~]# ansible -i /home/ansible/hostlist web -m shell -a "free -m | awk 'NR==2'" > b.txt -o && cat b.txt | awk '{print $10}'

四:剧本Playbook


案列一:          

          touch file.yaml

          vim file.yaml

         

         检查语法错误

         

         执行

         ansible-playbook file.yaml

案列二:

         handlers:由特定条件触发的Tasks

         安装ftp

         vim ftp.yaml

         

          ansible-playbook --syntax-check ftp.yaml

          ansible-playbook ftp.yaml

          删除ftp

         

         

案列三

         [root@ansible-server ansible]# vim /home/ansible/yum.yml
---
 - hosts: web
   user: root
   tasks:
   - name: install nginx
     yum: name=nginx state=latest
     notify: diandian
   handlers:
   - name: diandian
     service: name=nginx state=started

ansible-playbook --syntax-check /home/ansible/yum.yml

ansible-playbook /home/ansible/yum.yml

案例四:

循环:迭代,需要重复执行的任务

对迭代项的引用,固定变量名为”item”,使用with_items属性给定要迭代的元素

元素:1.列表 2.字符串 3.字典

基于字符串列表元素实战

vim php.yaml

- hosts: db
  user: root
  tasks:
  - name: install packages
    yum: name={{ item }} state=latest         #相当于for循环里面的i 
    with_items:                               #取值 。但是不支持通配符
     - httpd
     - php
     - php-mysql
     - php-mbstring
     - php-gd 

验证

vim user.yaml

案列四

vim tags.yaml

上传植物僵尸  

解压 unzip 

vim apache.yaml

ansible-playbook --syntax-check apache.yaml 

ansible-playbook apache.yaml 

apache 虚拟主机配置文件

 vi jspvz.conf 

   

   


http://www.niftyadmin.cn/n/5048335.html

相关文章

巨人互动|Facebook海外户Facebook的特点优势

Facebook作为全球最大的社交媒体平台之一&#xff0c;同时也是最受欢迎的社交网站之一&#xff0c;Facebook具有许多独特的特点和优势。本文小编将说一些关于Facebook的特点及优势。 1、全球化 Facebook拥有数十亿的全球用户&#xff0c;覆盖了几乎所有国家和地区。这使得人们…

Java基础常考知识点(基础、集合、异常、JVM)

作者&#xff1a;逍遥Sean 简介&#xff1a;一个主修Java的Web网站\游戏服务器后端开发者 主页&#xff1a;https://blog.csdn.net/Ureliable 觉得博主文章不错的话&#xff0c;可以三连支持一下~ 如有需要我的支持&#xff0c;请私信或评论留言&#xff01; Java基础常考知识点…

【Java】基于物联网技术的智慧工地源码(项目端、监管端、APP端、智慧大屏)

智慧工地是将云计算、大数据、物联网、移动技术和智能设备等信息化技术手段&#xff0c;聚集在建筑工地施工管理现场&#xff0c;围绕人员、机械、物料、环境等关键要素&#xff0c;建立智能信息采集、高效协同管理、数据科学分析、过程智慧预测&#xff0c;最终实现建筑工地的…

android获取RAM、CPU频率、系统版本、CPU核数

参考链接&#xff1a; https://www.jianshu.com/p/76d68d13c475 https://github.com/matthewYang92/Themis 获取CPU频率、核数的逻辑&#xff0c;是通过文件操作实现的&#xff0c;Android是基于Linux系统的&#xff0c;所以该方式针对不同的手机都是可靠的操作方式。 RAM&am…

较难算法美丽塔时间复杂度O(n)

题目 给你一个长度为 n 下标从 0 开始的整数数组 maxHeights 。你的任务是在坐标轴上建 n 座塔。第 i 座塔的下标为 i &#xff0c;高度为 heights[i] 。 如果以下条件满足&#xff0c;我们称这些塔是 美丽 的&#xff1a; 1 < heights[i] < maxHeights[i] heights 是一个…

使用Nginx搭建流媒体

文章目录 使用Nginx搭建流媒体安装 ffmpeg1、下载安装ffmpeg2、安装依赖3、编译安装 ffmpeg4、创建全局链接5、验证 安装nginx1、下载依赖包2、 解压安装nginx3、配置启动脚本5、 启动 nginx6、上传视频测试验证 使用Nginx搭建流媒体 参考地址 https://blog.csdn.net/u013416…

Mysql 数据类型、运算符

数据类型 数据类型的选择不是越大越好&#xff0c;因为我们业务层一般都是在内存上工作的&#xff0c;效率以及速度是比较快的&#xff0c;但是我们的数据库涉及磁盘的IO操作磁盘的IO操作相对来说是要慢很多的&#xff0c;所以我们在定义表结构的时候每一个字段的数据类型还是比…

h3c 网络设备清理所有配置

恢复出厂配置 # 删除下次启动配置文件。 <Sysname> reset saved-configuration The saved configuration file will be erased. Are you sure? [Y/N]:y Configuration file in flash: is being cleared. Please wait ........... Configuration file is cleared. …