• 【运维知识进阶篇】Ansible自动化运维-ad-hoc详解


    啥是ad-hoc,简而言之就是一条命令,执行完即结束,并不会保存。适用于在多台机器查看某个进程活动,或者拷贝指定文件到本地,这种临时的,只需要执行一次的命令。

    ad-hoc模式的命令构成

    1. #命令+主机名称+'-m'指定模块参数+模块名称+'-a模块动作'+'df -h'具体命令
    2. [root@Ansible ~]# ansible web01 -m command -a 'df -h'
    3. web01 | CHANGED | rc=0 >>
    4. Filesystem Size Used Avail Use% Mounted on
    5. devtmpfs 476M 0 476M 0% /dev
    6. tmpfs 487M 0 487M 0% /dev/shm
    7. tmpfs 487M 7.7M 479M 2% /run
    8. tmpfs 487M 0 487M 0% /sys/fs/cgroup
    9. /dev/sda3 19G 2.5G 17G 13% /
    10. /dev/sda1 197M 110M 88M 56% /boot
    11. tmpfs 98M 0 98M 0% /run/user/0

    ad-hoc结果返回颜色

    绿色:代表没有被管理端主机修改

    黄色:代表被管理端主机变更

    红色:代表出现故障,注意查看提示

    ad-hoc常用模块

    1. command # 执行shell命令(不支持管道等特殊字符)
    2. shell # 执行shell命令
    3. scripts # 执行shell脚本
    4. yum_repository # 配置yum仓库
    5. yum # 安装软件
    6. copy # 变更配置文件
    7. file # 建立目录或文件
    8. service # 启动与停止服务
    9. mount # 挂载设备
    10. cron # 定时任务
    11. get_url #下载软件
    12. firewalld #防火墙
    13. selinux #selinux

    Ansible-doc帮助手册

    1. [root@Ansible ~]# ansible-doc -l #查看所有可用模块
    2. [root@Ansible ~]# ansible-doc -copy #查看指定模块方法
    3. [root@Ansible ~]# ansible-doc -s copy #查看指定模块参数

    Ansible命令模块

    1、command

    1. #默认模块,执行命令
    2. [root@Ansible ~]# ansible web_group -a 'hostname'
    3. web02 | CHANGED | rc=0 >>
    4. Web02
    5. web01 | CHANGED | rc=0 >>
    6. Web01

    2、shell

    1. #如果需要管道操作,则使用shell
    2. [root@Ansible ~]# ansible web_group -m shell -a 'ps -ef|grep nginx' -f 50
    3. #-f 在多台主机运行的并发数量

    3、script

    1. #编写脚本
    2. [root@Ansible ~]# cat yum.sh
    3. #!/usr/bin/bash
    4. yum install -y vsftpd
    5. #该脚本文件在本机即可实现远程执行,不需要将脚本文件推送目标主机执行
    6. [root@Ansible ~]# ansible web_group -m script -a '/root/yum.sh'

    Ansible软件管理模块

    4、yum

    1. [root@Ansible ~]# ansible web_group -m yum -a "name=httpd state=present"
    2. name #指定对象
    3. httpd #软件包名称
    4. file:// #本地安装路径(相当于yum localinstall本地rpm包)
    5. http:// #指定yum源(远程仓库获取rpm包)
    6. state #进行的操作
    7. installed、present #安装软件包
    8. removed、absent #移除软件包
    9. latest #安装最新软件包
    10. [root@Ansible ~]# ansible-doc yum
    11. exclude=kernel*、foo* #排除某些包
    12. list=ansible #类似于yum list查看是否可以安装
    13. disablerepo="epel,ol7_latest" #禁用指定的yum仓库
    14. download_only=true #只下载不安装,类似于yum install d

    5、yum_repository

    1. name #指定仓库名,没有则用file当仓库文件名
    2. description #描述
    3. baseurl #指定yum源
    4. file #配置文件名,当仓库名与配置文件名不同时候使用
    5. mirrorlist #镜像列表
    6. state #行为
    7. gpgcheck #指定检查秘钥
    8. enable #是否启用仓库

    Ansible文件管理模块

    适用于生产场景,统一管理配置

    6、copy

    1. #拷贝文件模块
    2. [root@Ansible ~]# ansible web_group -m copy -a 'src=/etc/passwd dest=/tmp/pass.txt'
    3. #在推送覆盖客户端文件前,对客户端已有文件进行备份(按照时间信息备份)
    4. [root@Ansible ~]# ansible web_group -m copy -a 'src=/etc/passwd dest=/tmp/pass.txt backup=yes'
    5. #直接向客户端文件中写入数据信息,并且会覆盖客户端文件内原有的数据信息
    6. [root@Ansible ~]# ansible web_group -m copy -a 'content='123' dest=/tmp/pass.txt'
    7. [root@Web01 ~]# cat /tmp/pass.txt
    8. 123
    9. [root@Ansible ~]# ansible web_group -m copy -a 'content='12345' dest=/tmp/pass.txt'
    10. [root@Web01 ~]# cat /tmp/pass.txt
    11. 12345
    12. src #源文件路径
    13. dest #目标路径
    14. backup #对推送传输的文件进行备份
    15. content #添加覆盖内容内容
    16. group #指定文件属组
    17. owner #指定文件属主
    18. mode #指定文件权限

    7、file

    1. #创建目录
    2. [root@Ansible ~]# ansible web_group -m file -a 'path=/root/test state=directory'
    3. #创建文件并指定权限位、属主、属组
    4. [root@Ansible ~]# ansible web_group -m file -a 'path=/root/test.txt state=touch mode=0555 owner=root group=root'
    5. [root@Web01 ~]# ll test.txt
    6. -r-xr-xr-x 1 root root 0 Apr 17 15:43 test.txt
    7. #客户端创建软链接
    8. [root@Ansible ~]# ansible web_group -m file -a 'src=/root/test path=/root/test_link state=link'
    9. #创建目录,指定属组、属主、权限授权位,并进行递归授权
    10. [root@Ansible ~]# ansible web_group -m file -a 'path=/root/test state=directory owner=www group=www mode=0700 recurse=yes'
    11. path #指定远程主机目录或文件信息
    12. recurse #递归授权
    13. state
    14. directory #在客户端创建目录
    15. touch #在客户端创建文件
    16. link #link表示创建软链接文件
    17. absent #表示删除文件或目录
    18. mode #设置文件或目录权限
    19. owner #设置文件或目录属主信息
    20. group #设置文件或目录属组信息

    8、get_url

    1. [root@Ansible ~]# ansible web_group -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-agent-3.4.0-1.el7.x86_64.rpm dest=/root mode=0644'
    2. url #指定下载地址
    3. dest #指定客户端下载目录
    4. mode #指定下载文件权限
    5. checksum #检验加密算法
    6. md5
    7. sha256

    Ansible服务管理模块

    9、service、systemd

    1. #启动crond并加入开机自启动
    2. [root@Ansible ~]# ansible web_group -m service -a 'name=crond state=started enable=yes'
    3. [root@Ansible ~]# ansible web_group -m systemd -a 'name=crond state=started enable=yes'
    4. #停止crond并删除开机自启动
    5. [root@Ansible ~]# ansible web_group -m service -a 'name=crond state=stoped enable=no'
    6. [root@Ansible ~]# ansible web_group -m systemd -a 'name=crond state=stoped enable=no'
    7. name #定义要启动服务的名称
    8. state #指定服务状态
    9. started #启动服务
    10. stopped #停止服务
    11. restarted #重启服务
    12. reloaded #重载服务
    13. enabled #开机自启动
    14. yes
    15. no

    Ansible用户管理模块

    Ansible管理用户与组,通常使用user、group模块

    10、group

    1. [root@Ansible ~]# ansible web_group -m group -a 'name=www gid=888'
    2. name #组名
    3. gid #组gid
    4. state
    5. absent #移除客户机组
    6. present #创建客户机组(默认)

    11、user

    1. #创建用户,指定uid、gid,不创建家目录,不允许用户登录
    2. [root@Ansible ~]# ansible web_group -m user -a 'name=www uid=888 group=www shell=/sbin/nologin create_home=false'
    3. #创建用户并生成秘钥对
    4. [root@Ansible ~]# ansible web_group -m user -a 'name=www uid=888 group=www shell=/bin/bash generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa'
    5. #将明文密码加密,然后进行用户创建
    6. [root@Ansible ~]# ansible web_group -m debug -a "{{'koten'|password_hash('sha512','salt')}}"
    7. #创建用户
    8. [root@Ansible ~]# ansible web_group -m user -a 'name=koten password=xxxx create_home=true shell=/bin/bash'
    9. uid #指定用户uid
    10. group #指定用户组名称
    11. groups #指定附加组名称
    12. password #给用户添加密码(-a后需要用单引号)
    13. shell #指定用户登录shell
    14. create_home #是否创建家目录
    15. yes
    16. false

    Ansible定时任务模块

    12、cron

    1. #正常使用crond服务
    2. [root@Ansible ~]# crontab -l
    3. no crontab for root
    4. #使用ansible添加一条定时任务
    5. [root@Ansible ~]# ansible web_group -m cron -a "minute=* hour=* day=* mounth=* weekday=* job='/bin/sh /root/test.sh'"
    6. #不定义默认每分钟执行一次
    7. [root@Ansible ~]# ansible web_group -m cron -a "job='/bin/sh /root/test.sh'"
    8. #设置定时任务注释信息,可以用name设定
    9. [root@Ansible ~]# ansible web_group -m cron -a "name='cron01' job='/bin/sh /root/test.sh'"
    10. #删除相应定时任务
    11. [root@Ansible ~]# ansible web_group -m cron -a "name='cron01' job='/bin/sh /root/test.sh' state=absent"
    12. #注释相应定时任务,使定时任务失效
    13. [root@Ansible ~]# ansible web_group -m cron -a "name='cron01' job='/bin/sh /root/test.sh' disabled=no"

    Ansible磁盘挂载模块

    13、mount

    1. #指定源目录与挂载目录,挂载类型,挂载方式,传递给挂载命令的参数
    2. [root@Ansible ~]# ansible web_group -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
    3. opts #传递给挂载命令的参数
    4. present #开机挂载,仅将挂载配置写入/etc/fstab
    5. mounted #挂载设备,并将配置写入/etc/fstab
    6. unmounted #卸载设备,不会清除/etc/fstab写入的配置
    7. absent #卸载设备,会清理/etc/fstab写入的配置

    Ansible防火墙模块

    14、selinux

    1. #修改配置文件关闭selinux,重启后生效
    2. [root@Ansible ~]# ansible web_group -m selinux -a 'state=disabled'
    3. #临时关闭selinux
    4. [root@Ansible ~]# ansible web_group -m shell -a 'setenforce 0'
    5. #获取selinux当前状态
    6. [root@Ansible ~]# ansible web_group -m shell -a 'getenforce'

    15、firewalld

    1. #开启http服务的防火墙,并设置为开机自启动
    2. [root@Ansible ~]# ansible web_group -m firewalld -a 'service=http permanent=yes state=enabled'
    3. #开启http服务的防火墙,并设置为开机自启动,临时生效
    4. [root@Ansible ~]# ansible web_group -m firewalld -a "service=http immediate=yes permanent=yes state=enabled"
    5. 开启端口8080-8090端口tcp协议的防火墙,并设置为开机自启动,临时生效
    6. [root@Ansible ~]# ansible web_group -m firewalld -a "port=8080-8090/tcp immediate=yes permanent=yes state=enabled"
    7. service #指定开放或关闭的服务名称
    8. port #指定开放或关闭的端口
    9. permanent #是否添加永久生效
    10. state #开启或者关闭
    11. enabled
    12. disabled
    13. zone #指定配置某个区域
    14. rich_rule #配置辅规则
    15. masquerade #开启地址伪装
    16. immediate #临时生效
    17. source #指定来源IP

    Ansible主机信息模块

    在以下场景中会经常用到主机信息

    1、根据不同主机不同IP创建对应不同的IP目录

    2、根据不同主机不同主机名创建对应主机名的目录

    3、自动化运维平台需要自动获取到主机的IP地址,内存信息,磁盘信息,主机名等等

    4、如果安装数据库,分配内存为物理内存的80%,此时有3台不同物理内存的机器2G、4G、16G,写playbook的话,可以获取对应主机的内存做出计算,写判断进行内存分配。

    16、setup查看所有详细信息

    [root@Ansible ~]# ansible Web01 -m setup

    17、setup获取IP地址

    [root@Ansible ~]# ansible Web01 -m setup -a 'filter=ansible_default_ipv4'

    18、setup获取主机名

    [root@Ansible ~]# ansible Web01 -m setup -a 'filter=ansible_fqdn'

    19、setup获取内存信息

    [root@Ansible ~]# ansible Web01 -m setup -a 'filter=ansible_memory_mb'

    20、setup获取磁盘信息

    [root@Ansible ~]# ansible Web01 -m setup -a 'filter=ansible_devices'

    21、其他信息参数

    1. ansible_all_ipv4_addresses:仅显示ipv4的信息。
    2. ansible_devices:仅显示磁盘设备信息。
    3. ansible_distribution:显示是什么系统,例:centos,suse等。
    4. ansible_distribution_major_version:显示是系统主版本。
    5. ansible_distribution_version:仅显示系统版本。
    6. ansible_machine:显示系统类型,例:32位,还是64位。
    7. ansible_eth0:仅显示eth0的信息。
    8. ansible_hostname:仅显示主机名。
    9. ansible_kernel:仅显示内核版本。
    10. ansible_lvm:显示lvm相关信息。
    11. ansible_memtotal_mb:显示系统总内存。
    12. ansible_memfree_mb:显示可用系统内存。
    13. ansible_memory_mb:详细显示内存情况。
    14. ansible_swaptotal_mb:显示总的swap内存。
    15. ansible_swapfree_mb:显示swap内存的可用内存。
    16. ansible_mounts:显示系统磁盘挂载情况。
    17. ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
    18. ansible_processor_vcpus:显示cpu个数(只显示总的个数)。

    注意:此处的匹配规则支持通配符,后面我们用playbook,会参考这些内置变量使用


    我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

     

  • 相关阅读:
    金昌JCH文件批量转BMP/JPG图片脚本
    HTML中的基础标签(适合于新手)
    yolov5多个框重叠问题
    Linux基础篇
    接物游戏demo
    JAVA SPI
    vuex使用
    学会Linux,看完这篇就行了!
    力扣第35天----第1049题、第494题、第474题
    在centos上安装Anaconda
  • 原文地址:https://blog.csdn.net/qq_37510195/article/details/130188674