当前位置:网站首页>ansible简单使用示例
ansible简单使用示例
2022-07-22 10:03:00 【雅冰石】
一 ansible介绍
在自动化工具中,最简单、易于上手,而且最流行的当属Ansible莫属了。
Ansible是一个开源配置管理工具,可以使用它来自动化任务,部署应用程序实现IT基础架构。Ansible可以用来自动化日常任务,比如,服务器的初始化配置、安全基线配置、更新和打补丁系统,安装软件包等。
1.1 Ansible优势
二 安装ansible
yum install ansible -y
三 使用ansible
3.1 创建专用账号
#在ansible节点及所有后端节点上创建该账号
chattr -ia /etc/passwd
chattr -ia /etc/group
chattr -ia /etc/shadow
chattr -ia /etc/gshadow
useradd ansible
usermod -aG wheel ansible
usermod -aG root ansible
passwd ansible
chown -R ansible:ansible /etc/ansible
#确保该用户可以被远程登录
vi /etc/ssh/sshd_config
在AllowUsers这一行后面添加能远程访问的用户,示例:
AllowUsers ansible
#重启ssh
service sshd restart
3.2 配置免密登录
配置ansible账号到后端服务器的免密登录
假如要在192.168.144.251上执行ansible,远程操作192.168.144.252及192.168.144.253。
则在192.168.144.251上生成秘钥:
su - ansible
ssh-keygen
#将秘钥拷贝到另外两台服务器上
ssh-copy-id 192.168.144.252
ssh-copy-id 192.168.144.253
#验证免密登录
ssh 192.168.144.252
Ssh 192.168.144.253
3.3 配置ansible
默认的配置文件位于/etc/ansible/ansible.cfg下。
vi /etc/ansible/hosts,添加:
[mariadb]
192.168.144.252
192.168.144.253
[ES]
192.168.144.254
3.4 简单使用示例
#ping下后端mariadb服务器,看是否能ping通:
ansible mariadb -m ping
ping不通的话会提示Failed to connect to the host via ssh:
#只对某个组下某个主机进行变更:
ansible mariadb -m ping --limit "192.168.144.252"
#对某个网段进行操作:
ansible 192.168.144.* -m ping
#ping所有服务器
ansible all -m ping
#查看某个组下的所有主机列表
ansible mariadb --list
#查看某个组中的主机的磁盘空间
ansible mariadb -m command -a "df -h"
3.5 shell模块
ansible mariadb -m shell -a "echo hello world" > /opt/d.txt
文件会保存在ansible服务器上,不是后端服务器上。
3.6 script模块
#使用scripts模块可以在本地写一个脚本,在远程服务器上执行:
ansible mariadb -m script -a "/opt/a.sh"
3.7 copy模块
#将本机上的d.txt拷贝到后端mariadb节点上
ansible mariadb -m copy -a "src=/opt/d.txt dest=/opt/d.txt"
本篇文章参考了Ansible自动化入门,只要这一篇就够了
边栏推荐
- Operator overloading
- Wanxing PDF expert v8.3.8.1253 Professional Edition
- SQLite UDF/UDAF 的 PHP 实现
- Si12t touch key chip replaces tms12
- 记一次GCEasy的垃圾收集分析
- Force deduction solution summary 735 planetary collision
- Doctoral application | Professor Ni Zhuoxian of the University of Hong Kong enrolls doctoral students with the full award in the direction of Internet of things and machine learning
- C WinForm excel automatically inserts / edits annotations according to the current selection
- 解决Couldn‘t determine repo type for URL
- 虚函数
猜你喜欢
随机推荐
【带你学c带你飞】第3章 分支结构(练习3.1 简单的猜数游戏)
Sentry nodestore_node 表中的数据查看
树和二叉树:树的概念
项目执行过程中有几个关键注意事项?
自然语言处理NLP文本分类顶会论文阅读笔记(二)
NFT数字藏品系统搭建—app开发
"Pilot Cup" is coming! Summon the strongest brain in scientific computing, 360000 prize pool waiting for you
[learn C and fly] Chapter 3 branch structure (exercise 3.1 simple guessing game)
7.21 permutation and binary
C WinForm excel automatically inserts / edits annotations according to the current selection
[Standard Specification] what is the concept and definition of low code? Is no code a low code?
消息的可靠性与重复性
Firewall CMD common operation commands
SQL Design Teaching Management Library
C # automatically generates a dictionary (when there is a lot of data)
virtual function
MySQL 查看表结构最后变更时间
Vector
Si14t touch key chip is compatible with tms12
PHP curl 请求中设置 proxy 方法