18.ansible

ansible部署和模块

特点

1.ansible相当于自动化运维工具,只需要在主控端部署即可,之后就可以给被控主机部署任务

2.基于python语言,可以进行API拓展,并可以使用playbooks的指定配置进行动态管理,可以服务云计算、大数据平台,拥有web界面

3.通过ssh对目标进行主从集中化管理,后续通过构建yml文件配置ssh免密登录

安装ansible

主控主机:192.168.157.136
被控主机:192.168.157.137

关闭SELinux和防火墙

setenforce 0
systemctl stop firewalld

修改epel,安装ansible

yum -y install epel-release.noarch
yum -y install ansible

进入/etc/ansible下,查看文件

ansible.cfg:ansible的配置文件
hosts:控制目标的配置
roles:用来放playbook的目录

做ssh认证,使得主控端免密码登录被控主机

ssh-keygen -t rsa   # 默认保存在/root/.ssh/id_rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.157.137

添加被管理主机

vi /etc/ansible/hosts

# 在配置文件最下面直接添加
[client]   # 定义一个client组
192.168.157.137   # 被控主机IP,多余的用回车区分

检测client组是否可以ping通(非ICMP的ping)

ansible client -m ping   

查看ansible支持的模块

ansible-doc -l

按q结束,对于支持的模块可以通过ansible-doc [模块名]进行详细查看

ansible常用模块

命令控制模块(被控主机批量执行命令)

ansible client -m command -a "whoami" 

命令控制模块(被控主机批量执行主控机shell文件)

touch test1.sh
vi test1.sh

ifconfig
mkdir haha

chmod 777 test1.sh
ansible client -m script -a "./test1.sh"

命令控制模块(被控主机批量执行自身文件)

ansible client -m shell -a "./test2.sh"

被控主机默认目录在其用户目录,root用户就在root,其他用户就在home

拷贝模块(主控机文件复制到被控主机并赋予权限)

ansible client -m copy -a "src=./test1.sh dest=/tmp/ owner=root group=root mode=0755"

文件状态模块

ansible client -m stat -a "path=/etc/passwd"

实现远程下载模块

ansible client -m get_url -a "url=http://www.baidu.com dest=/root/index.html mode=0440 force=yes"

强制下载百度的页面保存到/root/index.html并赋予0440权限

软件包管理

ansible client -m yum -a "name=curl state=latest"   # 远程更新curl

curl为Linux自带的网页端请求工具,本身也是个软件包

进行crontab配置

ansible client -m cron -a "name='time1' hour='1,2' job='mkdir haha'"

此时被控主机出现备注为Ansible: time1* 1,2 * * * mkdir haha的定时服务

远程服务管理

ansible client -m service -a "name=nginx state=restart"

远程让名为nginx的服务重启

用户管理(创建新用户)

ansible client -m user -a "name=user1 password=123456 home=/home/user1 uid=1123 group=test1 comment='I am user1' system=yes"

生成一个用户,用户名为user1,密码为123456,home目录在/home/user1下,用户uid为1123,组是已经存在的test1组,备注是I am user1,是系统用户

用户管理(删除系统用户)

ansible client -m user -a "name=user1 state=absent remove=yes"

ansible剧本编写

yaml语法编写playbooks,结尾生成yml文件,和python一样,一定要对其,而且只能用空格

核心组件

tasks:任务
variables:变量
templates:模板
handlers:处理器
roles:角色

编写和使用剧本

简单的yml文档,类似树状图(模块+要做的任务)

vi /root/t1.yml

- hosts: all
  remote_user: root
  vars: httpd_port=80
  
  tasks:
  - name:install httpd
    yum: name=httpd state=present   # 安装httpd
  - name:install php
    yum: name=php state=present    # 安装php
  - name:start apache
    service: name=httpd state=started enable=true  # 安装apache并开机启动

使用jinja模板

vi /root/t2.yml

- hosts: client
  remote_user: root
  vars:
    username: user1
    password: 123456
  
  tasks:
  - name: add users   
    user: name={{ username }} state=present  # 与定义变量username对应
  - name: add password
    shell: echo {{ password }} | passwd --stdin {{ username }}  # 与定义变量password对应 

包含其他剧本

vi /root/t3.yml

- name: include
  hosts: client
  remote_user: root

  tasks:
  - include_tasks: /root/t2.yml

t2.yml可以单独执行成功,不过t3.yml包含t2.yml后发送成功但无法执行,显示语法错误

使用剧本

ansible-playbook t2.yml --syntax-check   # 检查t2.yml语法是否出错
ansible-playbook t2.yml   # 使用剧本执行yml文件
posted @ 2022-09-06 11:09  icui4cu  阅读(77)  评论(0)    收藏  举报