ansible-自动化运维

ansible:

远程自动化运维

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

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

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

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

操作模式

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

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

ansible的特性:

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

ansible什么都不会做

ansible四大组件

1、inventory 主机清单 主机组

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

2modules 模块 学习核心

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

3、插件

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

模块和语法的学习

命令行

test41 192.168.65.41 安装ansible

docker1 192.168.65.51

docker2 192.168.65.52

安装ansible
#关闭防火墙
[root@test41 ~]# systemctl stop firewalld
[root@test41 ~]# setenforce 0
​
#安装epel-release
[root@test41 ~]# yum -y install epel-release
#安装ansible
[root@test41 ~]# yum -y install ansible
[root@test41 ~]# cd /etc/ansible/
[root@test41 ansible]# ls
ansible.cfg  hosts  roles
#修改连接的配置文件
[root@test41 ansible]# vim hosts 
20行 [web]
23行 192.168.65.51
​
33行 [xy102]
37行 192.168.65.52
​
#设置免密登录
[root@test41 ansible]# ssh-keygen -t rsa
#传密码
[root@test41 ansible]# sshpass -p '123' ssh-copy-id root@192.168.65.51
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
[root@test41 ansible]# sshpass -p '123' ssh-copy-id root@192.168.65.52
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
#列出ansible所有已安装的模块
[root@test41 ansible]# ansible-doc -l 
#command模块,基础模块,也是ansible的默认模块,不支持管道符和重定向操作,执行一般的linux命令。
​
#如果报错就把密码再传一次就能执行
[root@test41 ansible]# ansible 192.168.65.52 -m command -a "date"
192.168.65.52 | CHANGED | rc=0 >>          #可以用指定的ip地址
2024年 08月 21日 星期三 09:43:23 CST
[root@test41 ansible]# ansible 192.168.65.51 -m command -a "date"
192.168.65.51 | CHANGED | rc=0 >>
2024年 08月 21日 星期三 09:43:35 CST
[root@test41 ansible]# ansible web -m command -a "date"
192.168.65.51 | CHANGED | rc=0 >>          #也可以用组名
2024年 08月 21日 星期三 10:00:47 CST
[root@test41 ansible]# ansible xy102 -m command -a "date"
192.168.65.52 | CHANGED | rc=0 >>
2024年 08月 21日 星期三 10:01:01 CST
#所有声明的主机都会执行
[root@test41 ansible]# ansible all -m command -a "date"
192.168.65.52 | CHANGED | rc=0 >>
2024年 08月 21日 星期三 10:02:19 CST
192.168.65.51 | CHANGED | rc=0 >>
2024年 08月 21日 星期三 10:02:19 CST
​

#怎么使用ansible

ansible<组名/ip地址> -m 指定模块,不加就是默认使用command -a <参数或者命令>

常用的模块参数

1、command模块
#chdir在目标主机提前进入目录,然后执行指令
[root@test41 ansible]# ansible 192.168.65.51 -a 'chdir=/home ls'
192.168.65.51 | CHANGED | rc=0 >>
aaa
bbb
wbl
​
#creates判断文件是否存在,如果存在就不执行后面的指令
[root@test41 ansible]# ansible 192.168.65.51 -a 'creates=/opt/123 ls/opt'
192.168.65.51 | SUCCESS | rc=0 >>
skipped, since /opt/123 exists
​
#removes 判断文件是否存在,如果存在就会执行指令
[root@test41 ansible]# ansible 192.168.65.51 -a 'removes=/opt/123 ls /opt'
192.168.65.51 | CHANGED | rc=0 >>
123
docker-compose.yml
harbor
harbor-offline-installer-v2.8.1.tgz
mysql
mysql1
nginx
nginx1
nhtml
php
php1
​
2、shell模块 支持管道符和重定向,也可以用逻辑表达式&&且 ; 逻辑或
[root@test41 ansible]# ansible 192.168.65.51 -m shell -a 'useradd test'
192.168.65.51 | CHANGED | rc=0 >>
​
[root@test41 ansible]# ansible 192.168.65.51 -m shell -a 'echo 123456 | passwd --stdin test'
192.168.65.51 | CHANGED | rc=0 >>
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
​
[root@test41 ansible]# ansible 192.168.65.51 -m shell -a 'cat /opt/123'
192.168.65.51 | CHANGED | rc=0 >>
123
​
#逻辑且
[root@test41 ansible]# ansible 192.168.65.51 -m shell -a 'touch /opt/123.txt && echo 123 > /opt/123.txt && cat /opt/123.txt'
[WARNING]: Consider using the file module with state=touch rather than running
'touch'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
192.168.65.51 | CHANGED | rc=0 >>
123
​
#逻辑或       (前一个失败不影响后一个执行)
[root@test41 ansible]# ansible 192.168.65.51 -m shell -a 'cat /etc/passwdd ; cat /opt/123.txt'
192.168.65.51 | CHANGED | rc=0 >>
123cat: /etc/passwdd: 没有那个文件或目录
​

在目标主机创建一个脚本,在脚本中写一个命令ifcong,然后运行脚本

[root@test41 ansible]# ansible 192.168.65.51 -m shell -a 'echo -e "#!/bin/bash\nifconfig" > /opt/test.sh && sh /opt/test.sh'
192.168.65.51 | CHANGED | rc=0 >>
br-211011ad3135: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        inet6 fe80::42:41ff:fe8e:75ed  prefixlen 64  scopeid 0x20<link>
        ether 02:42:41:8e:75:ed  txqueuelen 0  (Ethernet)
        RX packets 12020  bytes 3861077 (3.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16415  bytes 1421992 (1.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
3、cron模块,定时任务模块 minute/hour/day/month/weekday 分/时/日/月/周
#创建定时任务,不指定名字就会报错
[root@test41 ansible]# ansible 192.168.65.51 -m cron -a 'minute=30 hour=8 day=* job="ls /opt"'
[DEPRECATION WARNING]: The 'name' parameter will be required in future 
releases.. This feature will be removed in version 2.12. Deprecation warnings 
can be disabled by setting deprecation_warnings=False in ansible.cfg.
192.168.65.51 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "None"
    ]
}
​
[root@test41 ansible]# ansible 192.168.65.51 -a 'crontab -l'
192.168.65.51 | CHANGED | rc=0 >>
#Ansible: None
30 8 * * * ls /opt
#指定文件名
[root@test41 ansible]# ansible 192.168.65.51 -m cron -a 'minute=30 hour=8 day=3  month=9 job="ls /opt" name="test1"'
192.168.65.51 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "None", 
        "test1"
    ]
​
[root@test41 ansible]# ansible 192.168.65.51 -a 'crontab -l'
192.168.65.51 | CHANGED | rc=0 >>
#Ansible: None
30 8 * * * ls /opt
#Ansible: test1
30 8 3 9 * ls /opt
​
#删除定时任务
[root@test41 ansible]# ansible 192.168.65.51 -m cron -a 'name="test1" state=absent'
192.168.65.51 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "None"
    ]
}
[root@test41 ansible]# ansible 192.168.65.51 -m cron -a 'name="None" state=absent'
192.168.65.51 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@test41 ansible]# ansible 192.168.65.51 -a 'crontab -l'
192.168.65.51 | CHANGED | rc=0 >>
​
4、user模块,用户管理模块

name用户名必选参数

state=present|absent present创建 absent删除

system=yes|no 创建用户时,是否是系统账号

uid指定用户的uid

group指定用户组

shell默认系统用户可以不加

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

password用户添加密码

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

[root@test41 ansible]# ansible 192.168.65.51 -m user -a 'name=xy102 system=no'
=no是普通用户
xy102:x:1002:1002::/home/xy102:/bin/bash
​
#指定用户、uid号、是否是系统用户
[root@test41 ansible]# ansible 192.168.65.51 -m user -a 'name=xy103 uid=900 shell=/sbin/nologin system=yes'
​
xy103:x:900:900::/home/xy103:/sbin/nologin
​
#删除用户
[root@test41 ansible]# ansible 192.168.65.51 -m user -a 'name=xy102 remove=yes state=absent'
192.168.65.51 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "force": false, 
    "name": "xy102", 
    "remove": true, 
    "state": "absent"
}
​

5、copy模块 复制模块,复制主机文件到目标主机。

src表示源文件 dest目标主机保存的路径 mode复制文件保存的权限 owner文件的所有者 group文件的所在组 content指定复制内容,就不能用src

[root@test41 opt]# ansible 192.168.65.52 -m copy -a 'src=/opt/xy102.txt dest=/opt/'
​
[root@docker2 opt]# cat xy102.txt 
123
​
#设置权限
[root@test41 opt]# ansible 192.168.65.52 -m copy -a 'src=/opt/xy102.txt dest=/opt/ mode=640'
root用户下的权限640
[root@docker2 opt]# ll
-rw-r-----. 1 root root         4 8月  21 11:37 xy102.txt
​
#指定文件的所有者
[root@test41 opt]# ansible 192.168.65.52 -m copy -a 'src=/opt/xy102.txt dest=/opt/ mode=640 owner=wbl group=wbl'
改变了所有者
[root@docker2 opt]# ll
-rw-r-----. 1 wbl  wbl          4 8月  21 11:37 xy102.txt
​
#
[root@test41 opt]# ansible 192.168.65.52 -m copy -a 'content="黑神话:悟空真好玩" dest=/opt/houzi.txt mode=777 owner=wbl group=wbl'
​
[root@docker2 opt]# cat houzi.txt 
黑神话:悟空真好玩
​
#修改文件名
[root@test41 opt]# ansible 192.168.65.52 -a 'mv /opt/houzi.txt /opt/孙悟空。txt'192.168.65.52 | CHANGED | rc=0 >>
​
[root@docker2 opt]# ll
改前的文件名
-rwxrwxrwx. 1 wbl  wbl         27 8月  21 11:43 houzi.txt
改后的文件名
-rwxrwxrwx. 1 wbl  wbl         27 8月  21 11:43 孙悟空。txt
​
权限:r/4读--w/2写--x/1执行
​
5、file模块 设置文件属性

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

#创建文件
[root@test41 opt]# ansible 192.168.65.51 -m file -a 'path=/opt/abc.txt state=touch mode=777 owner=wbl group=wbl'
​
[root@docker1 opt]# ll
-rwxrwxrwx. 1 wbl  wbl   0 8月  21 13:37 abc.txt
​
#创建链接文件
[root@test41 opt]# ansible 192.168.65.51 -m file -a 'path=/opt/abc.txt.link src=/opt/abc.txt state=link' 
​
[root@docker1 opt]# ll
-rwxrwxrwx. 1 wbl  wbl   0 8月  21 13:37 abc.txt
lrwxrwxrwx. 1 root root 12 8月  21 13:39 abc.txt.link -> /opt/abc.txt
​
#删除链接文件
[root@test41 opt]# ansible 192.168.65.51 -m file -a 'path=/opt/abc.txt.link state=absent' 
​
[root@docker1 opt]# ll
-rwxrwxrwx. 1 wbl  wbl   0 8月  21 13:37 abc.txt
​
6、hostname模块 设置远程主机的主机名
[root@test41 opt]# ansible 192.168.65.51 -m hostname -a "name=nginx1"
​
[root@docker1 opt]# su
[root@nginx1 opt]# 
​
7、ping模块 测试主机和主机名
#ping主机看连接是否正常
[root@test41 opt]# ansible all -m ping
192.168.65.51 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.65.52 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
​
8、yum模块 在目标主机安装软件 (只能安装和卸载)
#安装软件
[root@test41 opt]# ansible 192.168.65.51 -m yum -a 'name=httpd'
192.168.65.51 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "httpd-2.4.6-99.el7.centos.1.x86_64 providing httpd is already installed"
    ]
}
​
#卸载软件
[root@test41 opt]# ansible 192.168.65.51 -m yum -a 'name=httpd state=absent'
192.168.65.51 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "removed": [
            "httpd"
        ]
    }, 
​
#查看服务运行状态
[root@test41 opt]# ansible 192.168.65.51 -a 'systemctl status httpd'
192.168.65.51 | FAILED | rc=4 >>
Unit httpd.service could not be found.non-zero return code
​
9、service模块 用来管理目标主机上的软件的运行状态

name 服务名称

state=started|stopped|restarted

enabled=true 设置开机自启

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

[root@test41 opt]# ansible 192.168.65.51 -m yum -a 'name=nginx'
[root@test41 opt]# ansible 192.168.65.51 -m service -a 'name=nginx enabled=true state=started runlevel=60'
​

1、安装nginx 2、开启nginx 开机自启动 3、访问的内容是this is nginx1!

[root@test41 opt]# ansible 192.168.65.52 -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.65.52'
​
  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值