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'
  • 16
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ansible的find模块是用来在远程主机上查找文件和目录的。它提供了灵活的搜索选项,可根据文件名、路径、权限等条件来查找文件。通过指定不同的参数,可以实现对文件的查找、过滤和处理等操作。 你可以在Ansible官方文档的模块页面中找到关于find模块的详细信息。在该页面上,你将找到有关find模块的完整参数列表、示例用法以及与其他相关模块的比较。 另外,在Ansible文档的模块分类信息页面中,你也可以找到关于find模块的分类信息。这个页面列出了Ansible的所有模块按照不同的类别进行了分类,以方便用户查找和使用。 总结来说,Ansible的find模块是一个强大的工具,可以帮助你在远程主机上查找和处理文件。你可以根据需要使用不同的参数来定制你的查找条件,以满足你的具体需求。 :https://docs.ansible.com/ansible/latest/user_guide/modules.html :https://docs.ansible.com/ansible/latest/modules/modules_by_category.html :https://docs.ansible.com/ansible/2.9/user_guide/modules_intro.html<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [金鱼哥说AnsibleAnsible临时命令这招功夫就应该要这样耍](https://blog.csdn.net/qq_41765918/article/details/121722471)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值