首页 > 其他分享 >ansible模块

ansible模块

时间:2024-08-31 23:26:03浏览次数:5  
标签:ops client1 webserver ansible hosts ssh 模块

ansible基础

  1. 配置ansible的hosts文件
sudo vim /etc/ansible/hosts

添加以下内容

client1
client2
client3
  1. 测试ansible能否通
    命令
ansible [主机名] -m ping -o
# -O 简洁输出

例:

ansible localhost -m ping -o
ansible client1 -m ping -o

可以看到结果如下

  • ping自己可以通
    img
  • ping client1可以通
    img
  • ping client2和client3不可以通,因为没有配置免密,没有授权,无法识别到
    img
    如果要连接,可以在后面加上账号密码
    img
  • ping client4未匹配到,因为前面在hosts文件没有添加上
    img

Inventory-主机清单

含义:清查;存货清单,财产目录,主机清单

  1. 增加主机组,增加后可以通过组进行管理主机
sudo vim /etc/ansible/hosts

添加以下内容

[webserver]  #组名,可自定义
client1
client2
client3
client4

测试结果

ansible webserver -m ping -o -u ops -k  --ssh-common-args="-o StrictHostKeyChecking=no"
#--ssh-common-args="-o StrictHostKeyChecking=no"作用是忽略ssh首次连接手动输入yes/no来进行公钥确认,也可以不写

img

  1. 增加用户名 密码
sudo vim /etc/ansible/hosts

添加以下内容

[webserver]
client1 ansible_ssh_user='ops' ansible_ssh_pass='password'
client2 ansible_ssh_user='ops' ansible_ssh_pass='password'
client3 ansible_ssh_user='ops' ansible_ssh_pass='password'
client4 ansible_ssh_user='ops' ansible_ssh_pass='password'

[webserver]
client[1:4] ansible_ssh_user='ops' ansible_ssh_pass='password'

img
3. 增加端口
修改client1的ssh端口

vim /etc/ssh/sshd_config

修改以下内容

#Port 22改为Port 2222

重启ssh服务

systemcetl restart sshd

到ansible服务器测试能否通,结果失败
img
添加端口

vim /etc/ansible/hosts
[webserver]
client1 ansible_ssh_user='ops' ansible_ssh_pass='password' ansible_ssh_port='222'
client2 ansible_ssh_user='ops' ansible_ssh_pass='password'
client3 ansible_ssh_user='ops' ansible_ssh_pass='password'
client4 ansible_ssh_user='ops' ansible_ssh_pass='password'

再次到ansible服务器测试能否通,结果成功
img
4. 组:变量
修改组变量

[webserver]
client1
client2
client3
client4
[webserver:vars]
ansible_ssh_user='ops'
ansible_ssh_pass='password'

img
常用组变量

参数 用途 例子
ansible_ssh_host 定义 hosts ssh 地址 ansible_ssh_host=192.168.1.100
ansible_ssh_port 定义 hosts ssh 端口 ansible_ssh_port=3000
ansible_ssh_user 定义 hosts ssh 认证用户 ansible_ssh_user=user
ansible_ssh_pass 定义 hosts ssh 认证密码 ansible_ssh_pass=pass
ansible_sudo 定义 hosts sudo 密码 ansible_sudo=www
ansible_sudo_exe 定义 hosts sudo 路径 ansible_sudo_exe=/usr/bin/sudo
ansible_connection 定义 hosts 连接方式 ansible_connection=local
ansible_ssh_private_key_file 定义 hosts 私钥 ansible_ssh_private_key_file=/root/key
ansible_ssh_shell_type 定义 hosts shell类型 ansible_ssh_shell_type=bash
ansible_python_interpreter 定义 hosts 任务执行python路径 ansible_python_interpreter=/usr/bin/python2.6
ansible_*_interpreter 定义 hosts其他语言解析路径 ansible_*_interpreter=/usr/bin/ruby
  1. 子分组
    将不同的组合并到一个组
[apacheserver]
client1
client2
[tomcatserver]
client3
client4
[webserver:children]
apacheserver
tomcatserver

测试
img
img
img
6. 自定义主机列表
把hosts文件移动到家目录下

sudo mv /etc/ansible/hosts ~

img
使用-i参数调用家目录下的hosts文件

ansible -i hosts webserver -m ping -o -u ops -k

img
做完实验后切记把hosts文件还原回去,以免影响下面的实验

Ad-Hoc-点对点模式

简介

  1. shell模块

  2. 复制模块

  • 帮助
ansible-doc copy
  • 案例
    (1)将服务器的/etc/ansible/hosts文件分发到各个client
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777' -u ops -k

到各个client查看

[ops@ansible-client1 ~]$ cat /tmp/2.txt

img
(2)将服务器的/etc/ansible/hosts文件分发到各个client并备份

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777' -u ops -k backup=yes
#backup=yes表示当有同名的原文件存在时则进行备份
  1. 用户模块
  • 创建用户
ansible webserver -m user -a 'name=xiaodunan state=present' -u ops -k --become-user root -K --become
#-k:ssh用户的密码;-K:sudo的密码;--become-user:用sudo的用户(默认是root,可不写);--become:提升权限

img
到client验证用户是否存在

id xiaodunan

img

  • 修改密码
    (1)生成加密密码
echo "123456" | openssl passwd -1 -stdin

img
(2)修改密码

ansible webserver -m user -a 'name=xiaodunan password="$1$aQkYQKlU$VSaWvQPHXVopR/KBrGbwA1"' -b -K -u ops -k

验证
img
img

  • 修改shell
ansible webserver -m user -a 'name=xiaodunan shell=/sbin/nologin append=yes' -o -k -b -K

img
验证
img

  • 删除用户
ansible webserver -m user -a 'name=xiaodunan state=absent' -o -k -b -K

验证
img
4. 软件包管理

  • 对httpd进行更新
ansible webserver -m yum -a 'name="httpd" state=latest' -o -u ops -k -b -K

img
验证
img

  • 卸载
ansible webserver -m yum -a 'name="httpd" state=absent' -o -u ops -k -b -K

img
验证
img
5. 服务模块

  • 重新安装httpd服务
  • 调用httpd服务
ansible webserver -m service -a 'name=httpd state=started' -u ops -k -b -K

验证
img

  1. 文件模块
  • 创建一个文件
ansible webserver -m file -a 'path=/tmp/88.txt mode=777 state=touch' -u ops -k -b -K

img
验证
img

  • 创建一个目录
ansible webserver -m file -a 'path=/tmp/99 mode=777 state=directory' -u ops -k -b -K

验证
img
7. 收集模块

  • 收集client1的q全部信息
ansible client1 -m setup
  • 收集client1的ansible的ipv4信息
ansible client1 -m setup -a 'filter=ansible_all_ipv4_addresses' -k -b -K

img
8. fetch模块
fetch从远程某主机获取文件到本地

  • 参数

dest:用来存放文件的目录,例如存放目录为backup,源文件名称为/etc/profile;那么在主机中,保存为/backup/master/etc/profile
src:在远程拉取的文件,并且必须是一个file,不能是目录

  • 案例
    在client1创建一个file23文件
[ops@ansible-client1 ~]$ touch /tmp/file23.txt
[ops@ansible-client1 ~]$ vim /tmp/file23.txt

在server用ansible把client1的file23文件拉取过来

 ansible client1 -m fetch -a 'src=/tmp/file23.txt dest=/tmp' -u ops -k -b -K

img
验证
img
9. cron模块

  1. group模块

YAML-YAML Ain't Markup Language-非标记语言

Role-角色扮演

Homework

标签:ops,client1,webserver,ansible,hosts,ssh,模块
From: https://www.cnblogs.com/xiaodunan/p/18390898

相关文章

  • ts---模块
    模块:就是可以将公用的一些方法,变量,函数,类型,命名空间等,单离出去形成一个模块。使用的时候,通过引入进行使用。简单示例://utils.tsexportfunctionfunca(){}//外部使用import{funca}from'./moduls/utils.ts'可以是多个://utils.tsexportfunctionfunca(){}expor......
  • 20240831_174849 scratch 画笔模块入门必会
    20240904_015445scratch认识画笔模块画笔模块属于软件的内置模块使用前需要导入一下功能是可以实现绘画功能相关积木20240904_025445scratch绘制一条直线需求绘制一条直线实现20240904_035445scratch绘制一条粗直线需求实现20240904_045445scrat......
  • 20240831_175038 scratch 画笔模块基础图形绘制
    20240904_095445scratch绘制正方形需求实现20240904_105445scratch绘制长方形需求实现注意长方形的长与宽不一样所以不要使用重复执行4次20240904_115445scratch绘制圆形需求实现20240904_125445scratch绘制正三角形需求实现202......
  • 20240831_172424 scratch 自制积木与画图模块 技能
    认识自制积木定义普通自制积木定义带一参数的自制积木定义带多参数的自制积木认识画笔模块绘制一条直线绘制一条粗直线绘制一条有色直线绘制一条变色直线绘制一组平行线图章功能两个角色分别绘制绘制正方形绘制长方形绘制圆形绘制三角形绘制五边形绘制六边形绘制......
  • YOLOv8改进 | 模块缝合 | C2f融合卷积重参数化OREPA【CVPR2022】
    秋招面试专栏推荐 :深度学习算法工程师面试问题总结【百面算法工程师】——点击即可跳转......
  • Ansible自动化运维项目
    Ansible自动化运维项目是一个复杂而强大的解决方案,它利用Ansible这一开源自动化平台来简化IT基础设施的配置管理、应用部署和任务自动化过程。以下是一个关于Ansible自动化运维项目的详细探讨,包括其基本概念、架构、优势、应用场景、实施步骤、最佳实践以及未来发展等方面。......
  • 海康二次开发学习笔记9-通讯触发及模块列表获取
    通讯触发及模块列表获取模块列表获取获取流程中所有模块的模块名,添加下拉框用于显示模块名1.处理Combox2的DropDown事件///<summary>///模块列表获取///</summary>///<paramname="sender"></param>///<paramname=......
  • 使用 nuxi build-module 命令构建 Nuxt 模块
    title:使用nuxibuild-module命令构建Nuxt模块date:2024/8/31updated:2024/8/31author:cmdragonexcerpt:nuxibuild-module命令是构建Nuxt模块的核心工具,它将你的模块打包成适合生产环境的格式。通过使用--stub选项,你可以在开发过程中加快模块构建速度,但在发......
  • Broker服务器模块
    一.Broker模块介绍二.Broker模块具体实现1.类的成员变量与构造函数成员变量事件循环和TCP服务器:muduo::net::EventLoop_baseloop;muduo::net::TcpServer_server;这些是muduo库提供的核心组件,负责处理网络事件和管理TCP连接。消息分发和编码:muduo::net::Protobuf......
  • ansible 命令及其部分模块
    ansible管理:ansible进行远程管理的两个方法:adhoc临时命令。就是在命令行上执行管理命令。playbook剧本。把管理任务用特定格式写到文件中。无论哪种方式,都是通过模块加参数进行管理。adhoc临时命令语法:ansible主机或组列表-m模块-a"参数"#-a是可选的通过ping模块测试到远......