0
点赞
收藏
分享

微信扫一扫

03函数参数

林肯公园_97cc 2023-11-12 阅读 21

ansible基于Python开发,实现了批量系统配置,批量程序部署,批量运行命令等功能

ansible特点

  • 部署简单,只需在主控端部署Ansible环境,被控端无需做任何操作;
  • 默认使用ssh协议对设备进行管理;
  • 有大量常规运维操作模块,可实现日常绝大部分操作
  • 配置简单,功能强大,扩展性强
  • 支持API及自定义模块,可通过Python轻松扩展;
  • 通过Playbooks来定制强大的配置,状态管理
  • 轻量级,无需再客户端安装agent,更新时,只需在操作机上进行一次更新即可
  • 提供一个功能强大,操作性强的Web管理界面和REST API接口---AWX平台

ansible架构图

 一、ansible配置(yum安装)

1.yum安装

yum install epel-release -y
yum install ansible –y

2.ansible程序结构

3. absible配置文件

4.ansible主机清单

二、ansible常用命令

格式: ansible  主机清单中的主机组  -f 开启线程数  -m 模块  -a 模块需要的参数

 ansible配置公私钥

#1.生成私钥
[root@server ~]# ssh-keygen 
#2.向主机分发私钥
[root@server ~]# ssh-copy-id root@192.168.37.122
[root@server ~]# ssh-copy-id root@192.168.37.133
如果出现错误,那么便需要再安装一个包
[root@server ~]# yum -y install openssh-clients

三、ansible常用模块

1.主机连通性测试

[root@server ~]# ansible web -m ping
192.168.37.122 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.37.133 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

2.command模块

**可以直接在远程主机上执行命令,并将结果返回本主机**

[root@server ~]# ansible web -m command -a 'chdir=/data/ ls'  #先切换到/data/ 目录,再执行“ls”命令
[root@server ~]# ansible web -m command -a 'creates=/data/aaa.jpg ls'  #如果/data/aaa.jpg存在,则不执行“ls”命令
[root@server ~]# ansible web -m command -a 'removes=/data/aaa.jpg cat /data/a' #如果/data/aaa.jpg存在,则执行“cat /data/a”命令

3.shell模块(万金油模块)

**可以在主机上调用shell解释器运行命令,支持shell各种功能**

[root@server ~]# ansible web -m shell -a 'cat /etc/passwd |grep "keer"'

4.copy模块

**用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等**

1.复制文件
[root@server ~]# ansible web -m copy -a 'src=~/hello dest=/data/hello' 
2.给定内容生成文件,并指定权限
[root@server ~]# ansible web -m copy -a 'content="I am keer\n" dest=/data/name mode=666'
3.关于覆盖
[root@server ~]# ansible web -m copy -a 'content="I am keerya\n" backup=yes dest=/data/name mode=666'

 5.file模块

**主要用于设定文件的属性,比如创建文件,创建链接文件,删除文件等**

1.创建目录
[root@server ~]# ansible web -m file -a 'path=/data/app state=directory'
2.创建链接文件
[root@server ~]# ansible web -m file -a 'path=/data/bbb src=/data/aaa state=link'
3.删除文件
[root@server ~]# ansible web -m file -a 'path=/data/a state=absent'

 6.fetch模块

**用于从远程某主机获取(复制)文件到本地

[root@server ~]# ansible web -m fetch -a 'src=/data/hello dest=/data' 

 7.cron模块

**适用于管理cron计划任务的,语法于crontab文件中语法一致**

1.添加计划任务
[root@server ~]# ansible web -m cron -a 'name="ntp update every 5 min" minute=*/5 job="/sbin/ntpdate 127.0.0.1 &> /dev/null"'
2.删除计划任务:在添加计划任务后面加 state=absent
3.查看现有的计划任务
[root@server ~]# ansible web -m shell -a 'crontab -l'

 8.yum模块

**主要用于软件的安装**

[root@server ~]# ansible web -m yum -a 'name=nginx state=present'

 9.service模块

**用于服务程序的管理**

1.开启服务并设计自启动
[root@server ~]# ansible web -m service -a 'name=nginx state=started enabled=true' 
2.关闭服务
[root@server ~]# ansible web -m service -a 'name=nginx state=stopped'

 10.user模块

**用来管理用户账号**

1.添加一个用户并指定其uid
[root@server ~]# ansible web -m user -a 'name=mm uid=11111'
2.删除用户
root@server ~]# ansible web -m user -a 'name=mm state=absent'

 11.group模块

**用来添加或删除组**

1.创建组
[root@server ~]# ansible web -m group -a 'name=cc gid=12222'
2.删除组
[root@server ~]# ansible web -m group -a 'name=cc state=absent'

 12.script模块

**用于将本机的脚本在被管理端的机器上运行,该模块直接指定脚本的路径(脚本必须给执行权限)**

[root@server ~]# ansible web -m script -a '/tmp/df.sh'

13.setup模块

**主要用于收集信息,通过调用facts组件实现**

facts组件是Ansible用于采集被管机器设备信息的一个功能,我们可以使用setup模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。 facts就是变量,内建变量 。每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。

1.查看信息
[root@server ~]# ansible web -m setup -a 'filter="*mem*"'   #查看内存
2.保存信息
[root@server tmp]# ansible web -m setup -a 'filter="*mem*"' --tree /tmp/facts

14.get_url模块

**将文件或者软件从http,https,ftp下载到本地节点上**

[root@server ~]# ansible -i /etc/ansible/hosts  zabbix -m get_url -a "url=ftp://10.3.131.50/soft/wechat.py dest=/tmp"

 15.stat模块

**检查文件或文件系统状态**(windows目标,改为win _stat模块)

 16.unarchive模块

**从本机上复制存档后,将其解包**

说明:该unarchive模块将解压缩一个存档。默认情况下,它将在解包之前将源文件从本地系统复制到目标。设置remote_src=yes为解包目标上已经存在的档案。对于Windows目标,请改用win_unzip模块。

举报

相关推荐

0 条评论