• ansible笔记(一)


    感觉这个笔记还是不够全面,有些东西记下来之后也只能知道有这个东西,但是不知道如何去使用,还需要另行找资料,只能当做一个大纲去看,了解一些ansible的基本知识。2021年05月20日10:26:14
    我是先在本地用Markdown记的笔记,然后直接粘贴到博客园,可能格式上会有些问题,还请谅解。2021年05月18日15:27:46

    一、介绍

    ansible是一款自动化运维工具,可实现对服务器的批量管理。基于Python开发,通过SSH协议实现远程节点和管理节点之间的通信。

    ansible主要组件:

    • 主机清单 host inventory

      定义被管理的远程节点

    • 剧本 playbook

      可以理解成是一种脚本,定义远程节点要做的事

    • 模块 module

      每个模块实现相应的功能

    • 插件 plugins

    官网:https://www.ansible.com/

    官方文档:https://docs.ansible.com/

    中文文档:http://www.ansible.com.cn/index.html

    朱双印ansible笔记:https://www.zsythink.net/archives/tag/ansible/page/5

    二、安装ansible

    这里只记录centos安装ansible

    2.1 yum安装

    centos环境下安装ansible需要先配置epel-release

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

    暂时我只成功使用yum进行安装,其他安装方式没装好,等我成功后再补充(我是fw)

    三、主机清单 inventory文件

    yum安装的inventory文件默认为/etc/ansible/hosts

    3.1 简单地添加主机

    添加一台机器

    [group]
    192.168.0.2
    

    添加的机器的ssh端口不是22

    [group]
    192.168.0.2:2233
    

    添加一组机器

    [group]
    192.168.0.2
    192.168.0.3
    192.168.0.4
    

    添加带参数的机器

    [host3]
    192.168.5.67 ansible_sudo_pass='123'
    
    • 一台主机可以同时属于多个组
    • 主机可以使用ip和域名表示

    参数:

    参数 含义
    ansible_ssh_host 远程主机名
    ansible_ssh_port ssh端口
    ansible_ssh_user ssh用户
    ansible_ssh_pass ssh 密码
    ansible_sudo_pass sudo 密码
    ansible_sudo_exe sudo 命令路径
    ansible_connection 连接类型,可以是ssh, local, paramiko
    ansible_ssh_private_key_file 私钥文件
    ansible_shell_type shell类型
    ansible_python_interpreter Python解释器路径

    3.2 别名&主机变量

    [group]
    jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
    

    jumper是别名(主机变量),会连接到192.168.1.50:5555

    3.3 添加一组有规律的主机

    [webservers]
    www[1:50].example.com
    

    添加了www1.example.comwww50.example的50台主机

    [databases]
    db-[a:f].example.com
    

    添加了db-a.example.comdb-f.example.com的6台主机

    3.4 组变量

    [atlanta]
    host1
    host2
    
    [atlanta:vars]
    ntp_server=ntp.atlanta.example.com
    proxy=proxy.atlanta.example.com
    

    组变量将应用到组内所有成员

    3.5 给组添加组成员

    [all:children]  all组的两个子组:webs和db
    webs
    db
    
    [all:vars]      组的变量  这些变量可以给playbook使用,不能直接给ansible使用
    ansible_user=vagrant
    ansible_ssh_pass=vagrant
    
    [webs]      all组的子组webs的详细定义
    web1 ansible_host=10.1.1.11
    web2 ansible_host=10.1.1.12
    
    [db]        all组的子组db的详细定义
    dbserver ansible_host=10.1.1.21
    

    3.6 group_vars和hosts_vars

    在inventory文件统计目录下创建两个目录,分别为group_vars和host_vars,目录名是固定的。分别用来存储组变量文件和主机变量文件。

    3.6.1 引入group_vars目录中的组变量

    要给哪个组定义组变量,就在group_vars目录下创建一个与组同名的文件或目录。

    在group_vars目录中创建一个名为test的文件,内容如下

    testvar1: testGroupVar1
    

    相当于在inventory中给test同名组定义了一个组变量testvar1,变量值为testGroupVar1

    引用:

    - hosts: test71
      tasks:
      - debug:
          mag: "{{testvar1}}"
    

    如果在inventory中已经有一个同名变量,group_vars目录下的优先级更高

    3.6.2 引入host_vars目录中的主机变量

    用法差不多

    四、ansible使用格式及常用模块

    4.1 ansible使用格式

    常用选项

    -m 模块名

    -a 模块执行的参数

    -f 生成几个子进程执行

    -C 模拟执行,不实际执行

    -u 用户名

    -c 连接方式

    使用格式

    ansible all或分组名 -m 模块名 -a 参数

    all 所有主机

    使用示例

    # 在所有远程主机上使用shell模块执行ip a | grep eth0命令
    ansible all -m shell -a "ip a | grep eth0"
    
    # 主机部分可以直接写ip地址或主机名,多个ip用逗号或冒号隔开
    
    # 多个分组用逗号冒号隔开
    ansible webserver,dbserver -m shell -a "ip a"
    
    # 排队一个特定组:所有执行命令的机器必须隶属于webserver且不在dbserver组
    webserver:!dbserver
    
    # 指定两个组的交集
    webserver:&dbserver
    
    # 更复杂的条件:‘webservers’ 和 ‘dbservers’ 两个组中隶属于 ‘staging’ 组并且不属于 ‘phoenix’ 组的机器才执行命令”
    webservers:dbservers:&staging:!phoenix
    
    # 使用变量
    webservers:!{{excluded}}:&{{required}}
    
    # 支持通配符
    
    # 支持对应使用组编号,从0开始
    webserver[0]		# 该组第一个主机
    webserver[0:25]		# 该组第1到第26个主机
    
    # 支持正则,以~开头
    ~(web|db).*.example.com
    
    # 从文件中读取hosts,文件名以@做前缀
    ansible-playbook site.yml --limit @retry_hosts.txt
    

    4.2 ansible常用模块

    4.2.1 copy模块

    将本地文件推送到目标主机路径

    src 源文件路径

    dest 目标路径

    mode 文件权限

    content 自行填充的文件内容

    owner 属主

    group 属组

    示例

    ansible all -m copy -a "src=/root/test.txt dest=/test.txt"
    

    4.2.2 fetch模块

    从远程主机拉取文件到本地

    ansible all -m fetch -a "src=/root/test.txt dest=/root/"
    

    4.2.3 command模块

    在远程主机上执行命令

    ansible all -m command -a "ifconfig"
    

    4.2.4 shell模块

    在远程主机上执行命令

    ansible all -m shell -a "ifconfig"
    

    command模块和shell模块的区别

    • command不会通过shell处理命令,因此不支持像$HOME这样的变量和,以及<, >, |, ;&等都是无效的

    4.2.5 cron模块

    为远程主机添加计划任务

    minute

    hour

    day

    month

    weekday 星期

    name 计划任务名

    state 状态. present生成计划任务 absent删除计划任务

    job 任务

    ansible all -m cron -a "minute=*/3 job='sh test.sh' name=exec_script state=present"
    

    4.2.6 yum模块

    yum安装软件包的模块,可同时安装多个软件,使用逗号隔开

    name 包名

    state 状态. present|installed|latest 安装,absent|removed 删除

    ansible all -m yum -a "name=nginx state=installed"
    

    4.2.7 service模块

    服务管理

    name 服务名

    state 状态

    enabled 是否开机启动(true|false)

    runlevel 启动级别

    # 停止nginx服务但是设置开机启动
    ansible all -m service -a "name=nginx state=stopped enabled=true"
    

    4.2.8 script模块

    在远程主机上执行管理主机上的脚本,但是不把脚本文件传过去

    ansible all -m script -a "/root/test.sh"
    

    4.2.9 file模块

    创建文件、设置文件属性、删除数据

    path 目标路径

    state 指定类型.

    absent 删除文件和文件夹

    directory 新建目录

    touch 新建空文件

    link 新建软连接

    hard 新建硬链接

    owner 属主

    group 属组

    # 创建目录
    ansible all -m file -a "path=/var/tmp/hello.dir state=directory"
    
    # 创建软连接
    ansible all -m file -a "src=/tmp/hi.txt path=/var/tmp/hi.link state=link"
    
    # 创建空文件
    ansible all -m file -a "path=/tmp/1.txt state=touch"
    
  • 相关阅读:
    CodeForces
    hdu4003 树形dp
    hdu2196
    poj2486
    hdu1502 树形dp入门题
    cf 686D
    bzoj2763 分层图
    hdu4424 并查集+贪心+思维
    poj1734 最小环+输出路径
    集训题解1
  • 原文地址:https://www.cnblogs.com/CharrammaBlog/p/14767799.html
Copyright © 2020-2023  润新知