ansible:

ansible:

远程自动化运维

ansible是基于python开发的配置管理和应用部署工具。

也是自动化运维的重要工具。

可以批量配置,部署,管理上千台主机。

只需要在一台主机配置ansible就可以完成其他主机的操作。

操纵模式:

1、模块化操作,命令行执行

2、playbook,剧本,也是把命令行脚本化。脚本的格式是yaml格式

ansible的特性:幂等性

幂等性:多次操作或者是多次执行,对系统的影响不会发送变化,无论执行多少次结果都是一样的。

ansible什么都不会做

restart不是幂等性,restart是先停再起。

httpd get 幂等性

post 上传数据,发生变化

ansible的四大组件:

1、lnventory 主机清单 主机组

必须是要声明管理主机的地址或者其他配置,不声明ansible无法对目标主机进行操作

2、modules 模块 学习的核心

ansible的功能是靠模块来实现的

3、插件

4、playbooks 剧本------------脚本(复用)

模块和语法的学习:

命令行

192.168.60.70 ansible

192.168.60.80 被管理端

192.168.60.90 被管理端

安装ansible
复制代码
#先安装epel语言
[root@test7 ~]# yum -y install epel-release
#安装ansible
[root@test7 ~]# yum -y install ansible
[root@test7 ~]# cd /etc/ansible/
[root@test7 ansible]# vim host
 20 [web]
 23 192.168.60.80
 33 [xy102]
 37 192.168.60.90
[root@test7 ansible]# ssh-keygen -t rsa         #一路回车
#给80传密码
[root@test7 ansible]# sshpass -p '123' ssh-copy-id root@192.168.60.80
#给90传密码
[root@test7 ansible]# sshpass -p '123' ssh-copy-id root@192.168.60.90
[root@test7 ansible]# ansible-doc -l        #列出ansible所有已安装的模块
#如果报错就再传一次密码
command,ansible的默认模块就是command
复制代码
[root@test7 ansible]# ansible 192.168.60.80 -m command -a "date"
[root@test7 ansible]# sshpass -p '123' ssh-copy-id root@192.168.60.80
#如果报错就再传一次密码
[root@test7 ansible]# ansible 192.168.60.80 -m command -a "date"
[root@test7 ansible]# sshpass -p '123' ssh-copy-id root@192.168.60.90
#所有组中的所有主机都执行
[root@test7 ansible]# ansible all -m command -a "date"
[root@test7 opt]# ansible 192.168.60.90 -a 'tar -xf /opt/nginx-1.22.0.tar.gz -C /opt'
复制代码
[root@test7 ansible]# ansible-doc -l        #列出ansible所有已安装的模块
​
1、command模块 基础模块,也是ansible的默认模块 不支持管道符和重定向操作。执行一般的linux命令。

ansible的执行命令

复制代码
ansible  <组名/ip地址> -m 指定模块,不加-m,默认使用command -a <参数/命令>

常用的参数:

1、chdir 在目标主机提前进入目录,然后执行指令。
复制代码
[root@test7 ansible]# ansible 192.168.60.80 -a 'chdir=/home ls'
#解压压缩包
[root@test7 opt]# ansible 192.168.60.90 -a 'chdir=/opt tar -xf /opt/nginx-1.22.0.tar.gz'
2、creates 判断文件是否存在,如果存在,就不执行后面的指令
复制代码
[root@test8 opt]# ls
123 
​
[root@test7 ansible]# ansible 192.168.60.80 -a 'creates=/opt/123 ls /opt'
192.168.60.80 | SUCCESS | rc=0 >>
skipped, since /opt/123 exists
3、removes 判断文件是否存在,如果存在,执行命令
复制代码
[root@test8 opt]# ls
123 
​
[root@test7 ansible]# ansible 192.168.60.80 -a 'removes=/opt/123 ls /opt'
192.168.60.80 | CHANGED | rc=0 >>
123
elasticsearch-6.7.2.rpm
jenkins-2.396-1.1.noarch.rpm
test
4、shell模块 支持管道符和重定向,也可以用逻辑表达式 &&(且) ;(逻辑或)
复制代码
[root@test7 ansible]# ansible 192.168.60.80 -m shell -a 'useradd test'
192.168.60.80 | CHANGED | rc=0 >>
​
[root@test7 ansible]# ansible 192.168.60.80 -m shell -a 'echo 123456 | passwd --stdin test'
192.168.60.80 | CHANGED | rc=0 >>
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
复制代码
[root@test7 ansible]# ansible 192.168.60.90 -m shell -a 'touch /opt/123.txt && echo 123 > /opt/123.txt && cat /opt/123.txt'
ansible.cfg to get rid of this message.
192.168.60.90 | CHANGED | rc=0 >>
123
复制代码
##目标主机创建一个脚本,在脚本中写#!/bin/bash ifconfig 然后运行脚本,在一条命令中完成。
[root@test7 ansible]# ansible 192.168.60.90 -m shell -a 'echo -e "#!/bin/bash\nifconfig" > /opt/test.sh && sh /opt/test.sh'
​
[root@test7 ansible]# ansible 192.168.60.80 -m shell -a 'echo "#!/bin/bash" >> /opt/abc.sh && echo "ifconfig" >> /opt/abc.sh && sh /opt/abc.sh'
复制代码
#安装httpd源
[root@test7 opt]# ansible 192.168.60.80 -m shell -a 'yum -y install httpd'
5、cron模块 定时任务模块 minute/hour/day/month/weekday
复制代码
#创建定时任务一定要创建名字
[root@test7 ansible]# ansible 192.168.60.80 -m cron -a 'minute=30 hour=8 day=* job="ls /opt" name="test1"'      #job=表示定时任务执行的命令
[root@test8 opt]# crontab -l
#Ansible: None
30 8 * * * ls /opt
#删除定时任务
[root@test7 ansible]# ansible 192.168.60.80 -m cron -a 'name="test1" state=absent'
6、user模块 用户管理模块

name(用户名)必选参数

state=present|absent present 创建 absent删除

system=yes|no 创建用户时,no是普通用户,yes是程序用户

uid 指定用户的uid

group 指定用户组

shell 默认是系统用户可以不加

create_home=yes|no 不是默认的家目录/home。/opt/test1家目录 create_home=yes 创建,no就是不创建

password 用户添加密码

remove=yes|no state=absent删除用户,删除用户时是否删除家目录。

复制代码
#创建普通用户
[root@test7 ansible]# ansible 192.168.60.80 -m user -a 'name=xy102 system=no'
#创建程序用户
[root@test7 ansible]# ansible 192.168.60.80 -m user -a 'name=xy104 shell=/sbin/nologin system=yes'
#创建普通用户并指定家目录位置和密码
[root@test7 ansible]# ansible 192.168.60.80 -m user -a 'name=xy105 home=/opt/xy105 create_home=yes password=123456'
#删除用户并删除家目录
[root@test7 ansible]# ansible 192.168.60.80 -m user -a 'name=xy103 remove=yes state=absent'
7、copy 复制模块,主机的文件复制到目标主机。
复制代码
[root@test7 opt]# ansible 192.168.60.80 -m copy -a 'src=/opt/xy102.txt dest=/opt/'

src 表示源文件 dest 目标主机的保存路径

mode 复制文件时,表示权限

owner 文件的所有者 属于主

group 文件的所在组 属于组

content 指定复制内容,就不能用src

复制代码
#给xy102.txt指定读写执行权限,指定所有者和所在组
[root@test7 opt]# ansible 192.168.60.80 -m copy -a 'src=/opt/xy102.txt dest=/opt/ mode=640 owner=dn group=dn'
#创建应该文件名为houzi.txt,在里面写入黑神话悟空,真好玩!,并指定权限和所有者和所在组
[root@test7 opt]# ansible 192.168.60.80 -m copy -a 'content="黑神话悟空,真好玩!" dest=/opt/houzi.txt mode=777 owner=dn group=dn'
#修改文件名
[root@test7 opt]# ansible 192.168.60.80 -m shell -a 'mv /opt/houzi.txt /opt/sunwukong.txt'
[root@test7 opt]# ansible 192.168.60.80 -a 'mv /opt/houzi.txt /opt/sunwukong.txt'
8、file模块 设置文件属性

mode owner group state=touch|absent touch(创建) absent(删除)

复制代码
#创建一个文件,权限为777,所有者是dn,所在组是dn
[root@test7 opt]# ansible 192.168.60.80 -m file -a 'path=/opt/abc.txt state=touch mode=777 owner=dn group=dn'
#创建一个软链接文件,链接/opt/abc.txt,
[root@test7 opt]# ansible 192.168.60.80 -m file -a 'path=/opt/abc.txt.link src=/opt/abc.txt state=link'
#删除文件
[root@test7 opt]# ansible 192.168.60.80 -m file -a 'path=/opt/abc.txt.link state=absent'
9、hostname模块 设置远程主机的主机名
复制代码
[root@test7 opt]# ansible 192.168.60.80 -m hostname -a "name=test8"
10、ping模块 测试与远程主机通不通
复制代码
[root@test7 opt]# ansible all -m ping
#success就是通
11、yum模块 在目标主机安装软件和卸载软件

yum模块只能安装和卸载软件

复制代码
[root@test7 opt]# ansible 192.168.60.80 -m yum -a 'name=httpd'
[root@test7 opt]# ansible 192.168.60.80 -m yum -a 'name=httpd state=absent'
12、service模块 用来管理目标主机上的软件的运行状态

name 服务名称

state=started|stopped|restarted

enabled=true #设置开机自启

runlevel=40 #设置运行级别,设置了开机自启就需要声明运行级别

复制代码
#开启nginx并设置为开机自启,运行级别是60
[root@test7 opt]# ansible 192.168.60.80 -m service -a 'name=nginx enabled=true state=started runlevel=60'
复制代码
#1、安装nginx 2、开启nginx  开机自启动 3、访问的内容是this  is  nginx!
方法一:
[root@test7 opt]# ansible 192.168.60.80 -m shell -a 'yum -y install nginx && systemctl start nginx && systemctl enable nginx && echo "this is nginx!" > /usr/share/nginx/html/index.html && curl 192.168.60.80'
方法二:
[root@test7 opt]# ansible 192.168.60.90 -m yum -a 'name=httpd'
[root@test7 opt]# ansible 192.168.60.90 -m service -a 'name=nginx enabled=true state=started'
[root@test7 opt]# ansible 192.168.60.90 -m shell -a 'echo "this is nginx!" > /usr/share/nginx/html/index,html && curl 192.168.60.90'
13、防火墙和网络模块
iptables
复制代码
#拒绝test9    ping    test8
[root@test7 ~]# ansible 192.168.60.80 -m iptables -a 'chain=INPUT protocol=ICMP source=192.168.60.90 jump=REJECT' -b

-b 后台运行

复制代码
#将test8主机的80端口禁用
[root@test7 ~]# ansible 192.168.60.80 -m iptables -a 'chain=INPUT protocol=tcp destination_port=80 jump=REJECT' -b
复制代码
#将test8主机的80端口放空
[root@test7 ~]# ansible 192.168.60.80 -m iptables -a 'chain=INPUT protocol=tcp destination_port=80 jump=ACCEPT' -b
复制代码
#删除防火墙策略
[root@test7 ~]# ansible 192.168.60.80 -m iptables -a 'chain=INPUT protocol=tcp destination_port=80 jump=REJECT state=absent' -b
firewalld
复制代码
[root@test8 ~]# firewall-cmd --get-services | grep nginx
#放空test8主机防火墙的80端口
[root@test7 ~]# ansible 192.168.60.80 -m firewalld -a "service=nginx zone=public permanent=true state=enabled immediate=true" -b

immedeiate=true 立即生效

复制代码
#删除test8放空的80端口
[root@test7 ~]# ansible 192.168.60.80 -m firewalld -a "port=80/tcp  zone=public permanent=true state=disabled immediate=true" -
14、配置网卡
复制代码
[root@test7 ~]# ansible 192.168.60.90 -m ansible.builtin.lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-ens33 regexp='^IPADDR' line='IPADDR=192.168.60.89'" -b

regexp='^IPADDR' 匹配以IPADDR开头的整行

line='IPADDR=192.168.60.89' 替换整行

复制代码
#重启test9的网卡
[root@test7 ~]# ansible 192.168.60.90 -m shell -a 'systemctl restart network'
复制代码
#将89的IP地址加入ansible的hosts配置文件中
[root@test7 ~]# vim /etc/ansible/hosts
 33 [xy102]
 34 ## 
 35 ## db01.intranet.mydomain.net
 36 ## db02.intranet.mydomain.net
 37 192.168.60.90
 38 192.168.60.89
复制代码
#将IP地址89改为90
[root@test7 ~]# ansible 192.168.60.89 -m ansible.builtin.lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-ens33 regexp='^IPADDR' line='IPADDR=192.168.60.90'" -b
#重启IP地址为89的网卡配置
[root@test7 ~]# ansible 192.168.60.89 -m shell -a 'systemctl restart network'
15、script模块 运行本地的脚本,把脚本运行的结果输出到目标主机。

脚本的位置是在本机上,运行是目标主机上运行

复制代码
[root@test7 opt]# vim test1.sh
 #!/bin/bash
 echo "Hello world" >/opt/test1.txt
[root@test7 opt]# chmod 777 test1.sh 
[root@test7 opt]# ansible 192.168.60.80 -m script -a '/opt/test1.sh'
[root@test8 opt]# cat test1.txt 
Hello world
16、setup模块 查看目标主机的信息。IP地址、cpu、内核、系统信息
复制代码
[root@test7 opt]# ansible 192.168.60.80 -m setup

查看目标主机的cpu信息

复制代码
[root@test7 opt]# ansible 192.168.60.80 -m setup -a 'filter=ansible_*processor*'

查看目标主机的内核版本

复制代码
[root@test7 opt]# ansible 192.168.60.80 -m setup -a 'filter=ansible_proc_cmdline'

查看目标主机的内存

复制代码
[root@test7 opt]# ansible 192.168.60.80 -m setup -a 'filter=ansible_mem*'

查看目标主机的系统信息

复制代码
[root@test7 opt]# ansible 192.168.60.80 -m setup -a 'filter=ansible_system'
相关推荐
m0_6090004241 分钟前
向日葵好用吗?4款稳定的远程控制软件推荐。
运维·服务器·网络·人工智能·远程工作
小安运维日记2 小时前
Linux云计算 |【第四阶段】NOSQL-DAY1
linux·运维·redis·sql·云计算·nosql
CoolTiger、5 小时前
【Vmware16安装教程】
linux·虚拟机·vmware16
m0_741768855 小时前
使用docker的小例子
运维·docker·容器
学习3人组6 小时前
CentOS 中配置 OpenJDK以及多版本管理
linux·运维·centos
厨 神6 小时前
vmware中的ubuntu系统扩容分区
linux·运维·ubuntu
Karoku0666 小时前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
geek_Chen016 小时前
虚拟机共享文件夹开启后mnt/hgfs/下无sharefiles? --已解决
linux·运维·服务器
(⊙o⊙)~哦7 小时前
linux 解压缩
linux·运维·服务器
牧小七8 小时前
Linux命令---查看端口是否被占用
linux