自动化运维工具-Ansible之2-ad-hoc

Ansible ad-hoc

ad-hoc就是“临时命令”,执行完即结束,并不会保存


ad-hoc模式使用场景

比如在多台机器上查看某个进程是否启动,或拷贝指定文件到本地,等等


ad-hoc模式的命令使用

  1. #批量查看磁盘信息
  2. [root@m01 ~]# ansible web_group -m command -a 'df -h' -i ./hosts
  3. web02 | CHANGED | rc=0 >>
  4. 文件系统 容量 已用 可用 已用% 挂载点
  5. /dev/sda3 18G 1.1G 17G 6% /
  6. devtmpfs 981M 0 981M 0% /dev
  7. tmpfs 992M 0 992M 0% /dev/shm
  8. tmpfs 992M 9.5M 982M 1% /run
  9. tmpfs 992M 0 992M 0% /sys/fs/cgroup
  10. /dev/sda1 1014M 124M 891M 13% /boot
  11. tmpfs 199M 0 199M 0% /run/user/0
  12. web01 | CHANGED | rc=0 >>
  13. 文件系统 容量 已用 可用 已用% 挂载点
  14. /dev/sda3 18G 1.1G 17G 6% /
  15. devtmpfs 981M 0 981M 0% /dev
  16. tmpfs 992M 0 992M 0% /dev/shm
  17. tmpfs 992M 9.5M 982M 1% /run
  18. tmpfs 992M 0 992M 0% /sys/fs/cgroup
  19. /dev/sda1 1014M 124M 891M 13% /boot
  20. tmpfs 199M 0 199M 0% /run/user/0
  21. #批量查看内存信息
  22. [root@m01 ~]# ansible web_group -m command -a 'free -m' -i ./hosts
  23. web01 | CHANGED | rc=0 >>
  24. total used free shared buff/cache available
  25. Mem: 1982 143 1688 9 150 1668
  26. Swap: 1023 0 1023
  27. web02 | CHANGED | rc=0 >>
  28. total used free shared buff/cache available
  29. Mem: 1982 142 1684 9 155 1666
  30. Swap: 1023 0 1023

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@m01 ~]# ansible-doc -l # 查看所有模块说明
  2. [root@m01 ~]# ansible-doc copy # 查看指定模块方法
  3. [root@m01 ~]# ansible-doc -s copy # 查看指定模块参数

Ansible命令模块

command默认模块, 执行shell命令,不支持管道等特殊字符

  1. [root@m01 ~]# ansible web01 -a "hostname"

shell执行shell命令,支持管道等特殊字符,使用$需要\转义

  1. [root@m01 ~]# ansible web01 -m shell -a "ps -ef|grep nginx" -f 50
  1. [root@m01 ~]# ansible 'web01' -m shell -a "ifconfig eth0 | awk 'NR==2 {print $2}'"
  2. web01 | CHANGED | rc=0 >>
  3. inet 10.0.0.7 netmask 255.255.255.0 broadcast 10.0.0.255
  4. [root@m01 ~]# ansible 'web01' -m shell -a "ifconfig eth0 | awk 'NR==2 {print \$2}'"
  5. web01 | CHANGED | rc=0 >>
  6. 10.0.0.7

script执行shell脚本

  1. # 编写脚本
  2. [root@m01 ~]# vim /root/yum.sh
  3. #!/usr/bin/bash
  4. yum install -y vsftpd
  5. #在本地运行模块,等同于在远程执行,不需要将脚本文件进行推送目标主机执行
  6. [root@m01 ~]# ansible web01 -m script -a "/root/yum.sh"

Ansible软件管理模块

yum安装软件

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

yum_repository配置yum仓库

  1. # 添加yum仓库
  2. [root@m01 ~]# ansible web01 -m yum_repository -a "name=oldboy_epel description=EPEL baseurl=https://download.fedoraproject.org/pub/epel/$releasever/$basearch/" -i ./hosts
  3. # 仓库名和配置文件名不同
  4. [root@m01 ~]# ansible web01 -m yum_repository -a 'name=oldboy_epel description=EPEL file=test_oldboy baseurl=https://download.fedoraproject.org/pub/base/$releasever/$basearch/ gpgcheck=no' -i ./hosts
  5. # 添加mirrorlist
  6. [root@m01 ~]# ansible web01 -m yum_repository -a 'name=oldboy_epel description=EPEL file=test_oldboy baseurl=https://download.fedoraproject.org/pub/base/$releasever/$basearch/ gpgcheck=no mirrorlist=http://mirrorlist.repoforge.org/el7/mirrors-rpmforge enabled=no' -i ./hosts
  7. # 删除yum仓库及文件
  8. [root@m01 ~]# ansible web01 -m yum_repository -a 'name=oldboy_epel file=test_oldboy state=absent' -i ./hosts
  9. # 开启gpgcheck
  10. [root@m01 ~]# ansible web01 -m yum_repository -a 'name=oldboy_epel description=EPEL file=test_oldboy baseurl=https://download.fedoraproject.org/pub/base/$releasever/$basearch/ gpgcheck=yes gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7' -i ./hosts
  11. name #yum源里[]中的内容
  12. description #yum源里name的值
  13. file #yum源的文件名
  14. mirrorlist #yum源的列表
  15. baseurl # yum源中的仓库地址
  16. gpgcheck # yum源是否检查检查秘钥
  17. no
  18. yes
  19. state
  20. absent # 删除
  21. present # 添加(默认)
  22. enabled # 是否启用仓库
  23. no
  24. yes

Ansible文件管理模块

copy

  1. src # 推送数据的源文件信息
  2. dest # 推送数据的目标路径
  3. backup: # 目标文件是否备份
  4. yes # 备份
  5. no # 不备份
  6. follow: # 是否识别软链接
  7. yes
  8. no
  9. content # 直接在被管理端文件中添加内容
  10. group # 将本地文件推送到远端,指定文件属组信息
  11. owner # 将本地文件推送到远端,指定文件属主信息
  12. mode # 将本地文件推送到远端,指定文件权限信息
  1. # 推送文件并授权
  2. [root@m01 ~]# ansible web01 -m copy -a "src=/etc/passwd dest=/tmp/oldboy.txt owner=www group=www mode=777"
  3. # 在推送覆盖远程端文件前,对远端已有文件按照时间信息备份
  4. [root@m01 ~]# ansible web01 -m copy -a "src=/etc/passwd dest=/tmp/oldboy.txt backup=yes"
  5. # 直接向远端文件内写入数据信息,并且会覆盖远端文件内原有数据信息
  6. [root@m01 ~]# ansible web01 -m copy -a "content='oldboy' dest=/tmp/oldboy.txt"
  7. #识别软链接
  8. [root@m01 ~]# ansible 'web01' -m copy -a 'src=/root/test dest=/tmp owner=nginx group=nginx mode=0644 follow=yes'
  9. [root@m01 ~]# ansible 'web01' -m copy -a 'src=/root/test dest=/tmp owner=nginx group=nginx mode=0644 follow=no'

file

  1. src: # 源文件(如果做软链接就是远程机器上的文件)
  2. dest: # 目标文件(如果做软链接就是远程机器上的链接文件)
  3. path # 指定远程主机目录或文件信息
  4. recurse # 递归授权
  5. yes
  6. state
  7. directory # 在远端创建目录(默认递归)
  8. touch # 在远端创建文件
  9. link # link或hard表示创建链接文件
  10. absent # 表示删除文件或目录
  11. mode # 设置文件或目录权限
  12. owner # 设置文件或目录属主信息
  13. group # 设置文件或目录属组信息
  1. # 递归创建目录并授权
  2. [root@m01 ~]# ansible web01 -m file -a "path=/tmp/tmp1/oldboy_dir state=directory mode=0777 owner=root group=root"
  3. [root@m01 ~]# ansible web01 -m file -a "path=/tmp/tmp/oldboy_dir state=directory owner=www group=www mode=0700 recurse=yes"
  4. # 创建文件并授权
  5. [root@m01 ~]# ansible web01 -m file -a "path=/tmp/oldboy_file state=touch mode=0555 owner=root group=root"
  6. # 做软连接
  7. [root@m01 ~]# ansible web01 -m file -a "src=/tmp/oldboy_dir dest=/tmp/oldboy_dir_link state=link"
  8. # 删除文件
  9. [root@m01 ~]# ansible web01 -m file -a 'path=/tmp/oldboy_dir_link state=absent'
  10. # 递归授权目录

get_url

  1. [root@m01 ~]# ansible-doc get_url
  2. url # 指定下载地址
  3. dest # 指定下载目录
  4. mode # 指定权限
  5. checksum # 校验加密算法
  6. md5
  7. sha256
  1. # 下载包并授权
  2. [root@m01 ~]# ansible web01 -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=/tmp mode=0644'
  3. #下载包时验证
  4. [root@web01 /tmp]# md5sum zabbix-agent-3.4.0-1.el7.x86_64.rpm
  5. ba1f2511fc30423bdbb183fe33f3dd0f index.html
  6. [root@m01 ~]# ansible web01 -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=/opt mode=0644 checksum=md5:ba1f2511fc30423bdbb183fe33f3dd0f'

Ansible服务管理模块

service

  1. [root@m01 ~]# ansible-doc service
  2. EXAMPLES:
  3. - name: Start service httpd, if not started
  4. service:
  5. name: httpd
  6. state: started
  7. enabled: yes
  8. name: httpd # 服务的名字
  9. state:
  10. started # 启动服务
  11. stopped # 停止服务
  12. restarted # 重启服务
  13. reloaded # 重载服务
  14. enabled: # 开机自启
  15. yes
  16. no

systemd

  1. [root@m01 ~]# ansible-doc systemd
  2. EXAMPLES:
  3. - name: Start service httpd, if not started
  4. systemd:
  5. name: httpd
  6. state: started
  7. enabled: yes
  8. daemon_reload: yes
  9. name: httpd # 服务的名字
  10. state:
  11. started # 启动服务
  12. stopped # 停止服务
  13. restarted # 重启服务
  14. reloaded # 重载服务
  15. enabled: # 开机自启
  16. yes
  17. no
  18. daemon_reload # 后台启动
  1. # 停止nginx服务
  2. [root@m01 ~]# ansible web01 -m service -a 'name=nginx state=stopped'
  3. # 启动httpd服务,并加入开机自启
  4. [root@m01 ~]# ansible web01 -m systemd -a 'name=httpd state=started enabled=yes'

Ansible用户管理模块

group

  1. name #指定创建的组名
  2. gid #指定组的gid
  3. state
  4. absent #移除远端主机的组
  5. present #创建远端主机的组(默认)
  1. # 创建用户组
  2. [root@m01 ~]# ansible web01 -m group -a 'name=www state=present gid=777'
  3. # 删除用户组
  4. [root@m01 ~]# ansible web01 -m group -a 'name=www state=absent'

user

  1. - name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
  2. user:
  3. name: johnd # 用户名
  4. comment: John Doe # 用户的注释
  5. uid: 1040 # 用户uid
  6. group: admin # 用户的组名称
  7. groups: admins,developers # 指定附加组名称
  8. shell: /bin/bash # 指定登录脚本
  9. append: yes # 添加附加组时使用
  10. remove: yes # 移除家目录
  11. generate_ssh_key: yes # 是否生成密钥对
  12. ssh_key_bits: 2048 # 秘钥加密的位数
  13. ssh_key_file: .ssh/id_rsa # 秘钥文件
  14. expires: 1422403387 # 用户的有效时间
  15. state:
  16. present # 添加用户(默认)
  17. absent # 删除用户
  18. create_homeyes/no # 是否创建家目录
  19. password # 给用户添加密码(单引号)
  1. # 创建用户指定uid和gid,不创建家目录也不允许登陆
  2. [root@m01 ~]# ansible web01 -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=false'
  3. # 仅删除用户
  4. [root@m01 ~]# ansible web01 -m user -a 'name=www state=absent'
  5. # 删除用户及用户组
  6. [root@m01 ~]# ansible web01 -m user -a 'name=www state=absent remove=yes'
  7. # 将明文密码进行hash加密,然后创建用户并设定密码(密码必须是密文)
  8. [root@m01 ~]# ansible web01 -m debug -a "msg={{ 'oldboy' | password_hash('sha512', 'salt') }}"
  9. web01 | SUCCESS => {
  10. "msg": "$6$salt$xaunY8IjwsGxX14Fn5MU7iRza9R7crMbDiMUAG0b7Ku0f9pE.Am4ScvdCxURf.y0hsHX4o5bo3JSn/.DDXP8u1"
  11. }
  12. [root@m01 ~]# ansible web01 -m user -a 'name=oldboy1 password=$6$salt$xaunY8IjwsGxX14Fn5MU7iRza9R7crMbDiMUAG0b7Ku0f9pE.Am4ScvdCxURf.y0hsHX4o5bo3JSn/.DDXP8u1 create_home=true shell=/bin/bash'

注意:

  • 如果用户名字跟组名字相同,删除用户是会将组也删除
  • 当组下面有多个用户,删除的与组同名的用户也不会删除组
  1. # 创建用户并生成秘钥对
  2. [root@m01 ~]# ansible web01 -m user -a "name=oldboy uid=888 group=root shell=/bin/bash generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa" -i ./hosts
  3. web01 | CHANGED => {
  4. "ansible_facts": {
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "changed": true,
  8. "comment": "",
  9. "create_home": true,
  10. "group": 0,
  11. "home": "/home/oldboy",
  12. "name": "oldboy",
  13. "shell": "/bin/bash",
  14. "ssh_fingerprint": "2048 SHA256:WEMHCpSjxxqFwlzrCk1FqrPqeq6N/SHxL1gFTSqHlGM ansible-generated on web01 (RSA)",
  15. "ssh_key_file": "/home/oldboy/.ssh/id_rsa",
  16. "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRx+bCYGh4FqpKoPzyXrR8ef9GwoY6l6QEFQ0+XPynR22fd9Lbs1eUxWDm5aH4ZO8sPaI8a5xmj88Sipwl0FxlQTjD2X/vreZNEDbwFWrbZ24VvPkfPSSWBh5SxLH6pJt8pGQpPVWuLRMx6yOOxRB1hh9bGFzQNg5z8xqzeogTOoI7cxSFZVuUb5affNj8H5mCw2nAvblV+HNhRzbMlwr+9/EWcCWHDnlVYcELHXjpNJcyGB3VFOu1MPkmLaSTcaB73O0eRvZQkYMBePKJC44tvjHihGhvCk9rzh8qvzHxvMgoMD/+0uKAlIwEvOyfAczb7fxllU0rDtbyPtjbuLsR ansible-generated on web01",
  17. "state": "present",
  18. "system": false,
  19. "uid": 888
  20. }
  21. web02 | CHANGED => {
  22. "ansible_facts": {
  23. "discovered_interpreter_python": "/usr/bin/python"
  24. },
  25. "changed": true,
  26. "comment": "",
  27. "create_home": true,
  28. "group": 0,
  29. "home": "/home/oldboy",
  30. "name": "oldboy",
  31. "shell": "/bin/bash",
  32. "ssh_fingerprint": "2048 SHA256:IepfOosi2Xm8kfr4nOPAhG3fec6o8kpMnJ0/RwN+0F8 ansible-generated on web02 (RSA)",
  33. "ssh_key_file": "/home/oldboy/.ssh/id_rsa",
  34. "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEcO9iDKg4X8ya/y9E0eDelAFMp/rxiDSzW31r+REawaQyF4oywcdIagpz0MTg2BeF2WdaYUmHmtmSTfSOMif26+R1FLcL9f9NYu3io/0388jukcTfyN02diXWgqoKtt4Gbm8Bq8sWE4tX/FSYl42fG6bX1AyDSMzzB7ERr2AD/Y9KuKt7cEXDinGjqTFEXw6+x1wBHpotkUisYiZCci+1Nx4YSznVRBveZTlpxMUYmKgwkUXQIt+RoOYzjgD++0md8O7lwJGgODZkahlrf2pOQnmpS4isLi9or4N+DVnqD+cXb/RjgJzPIJZYazgRY3vtAU9DDqm5i049x/VxEqFj ansible-generated on web02",
  35. "state": "present",
  36. "system": false,
  37. "uid": 888
  38. }

Ansible定时任务模块

cron

  1. - name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /d
  2. cron:
  3. name: "check dirs" # 定时任务的注释
  4. minute: "0" # 分钟
  5. hour: "5,2" # 小时
  6. day: "2" # 日
  7. month: "2" # 月
  8. weekday: "2" # 周
  9. job: "ls -alh > /dev/null" # 定时任务的内容
  10. state:
  11. absent # 删除定时任务
  12. present # 添加定时任务
  1. # 添加定时任务
  2. [root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" minute=*/10 job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'
  3. # 修改定时任务(名字相同,只修改内容)
  4. [root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'
  5. # 删除相应定时任务(只能用name参数)
  6. [root@m01 ~]# ansible web01 -m cron -a "name='时间同步' state=absent"
  7. # 注释相应定时任务
  8. [root@m01 ~]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null" disabled=yes'

Ansible磁盘挂载模块

mount

  1. - name: Mount DVD read-only
  2. mount:
  3. path: /mnt/dvd # 挂载的目录(nfs客户端)
  4. src: /dev/sr0 # 远端被挂载的目录 (nfs服务端)
  5. fstype: nfs # 挂载类型
  6. opts: ro,noauto # 自动挂载的参数
  7. state:
  8. present # 开机挂载,仅将挂载配置写入/etc/fstab
  9. unmounted # 卸载设备,不会清除/etc/fstab写入的配置
  10. mounted # 卸载设备,会清理/etc/fstab写入的配置(常用)
  11. absent # 取消临时挂载,并且清理自动挂载(常用)
  1. [root@m01 ~]# ansible web01 -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=present"
  2. [root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted"
  3. [root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=unmounted"
  4. [root@m01 ~]# ansible web02 -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=absent"

Ansible防火墙模块

selinux

  1. # 关闭selinux修改配置文件,必须重启
  2. [root@m01 ~]# ansible web01 -m selinux -a 'state=disabled'
  3. [WARNING]: SELinux state temporarily changed from 'enforcing' to 'permissive'. State change will take effect next reboot.
  4. web01 | CHANGED => {
  5. "ansible_facts": {
  6. "discovered_interpreter_python": "/usr/bin/python"
  7. },
  8. "changed": true,
  9. "configfile": "/etc/selinux/config",
  10. "msg": "Config SELinux state changed from 'enforcing' to 'disabled'",
  11. "policy": "targeted",
  12. "reboot_required": true,
  13. "state": "disabled"
  14. }
  15. # 临时关闭
  16. [root@m01 ~]# ansible web01 -m shell -a 'setenforce 0'
  17. web01 | CHANGED | rc=0 >>
  18. [root@m01 ~]# ansible web01 -m shell -a 'getenforce'
  19. web01 | CHANGED | rc=0 >>
  20. Permissive

firewalld

  1. service: https # 指定开放或关闭的服务名称
  2. permanent:
  3. yes # 永久生效
  4. no # 临时生效
  5. state:
  6. enabled # 开启
  7. disable # 关闭
  8. port: 8081/tcp 161-162/udp # 防火墙配置的端口
  9. zone: dmz # 指定配置空间
  10. rich_rule: # 辅规则
  11. source: 192.0.2.0/24 # 防火墙配置的源ip
  12. masquerade:
  13. yes # 开启ip伪装
  14. no # 关闭ip伪装
  15. interface: eth2 # 绑定网卡
  16. immediate # 临时生效
  1. # 允许访问http,永久生效
  2. [root@m01 ~]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled'
  3. # 允许80端口被访问,临时生效
  4. [root@m01 ~]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled'
  5. [root@m01 ~]# ansible web_group -m firewalld -a "service=http immediate=yes permanent=yes state=enabled"
  6. # 允许10.0.0.0/24网段访问22端口
  7. [root@m01 ~]# ansible web01 -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.0/24 service name=ssh accept" state=enabled'
  8. # 允许10.0.0.0/24网段访问所有服务
  9. [root@m01 ~]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=yes'

Ansible压缩解压模块

Archive压缩

  1. - name: Compress directory /path/to/foo/ into /path/to/foo.tgz
  2. archive:
  3. path: /path/to/foo #要压缩的文件或目录
  4. dest: /path/to/foo.tgz #压缩后的文件
  5. formatbz2, gz, tar, xz, zip #指定打包的类型
  1. #1.打包站点目录
  2. [root@m01 /package]# ansible web01 -m archive -a 'path=/code dest=/tmp/code.tar.gz'

unarchive解压

  1. - name: Unarchive a file that is already on the remote machine
  2. unarchive:
  3. src: /tmp/foo.zip #要解压的包
  4. dest: /usr/local/bin #解压到目标位置
  5. remote_src:
  6. yes #要解压的包在受控端
  7. no #要解压的包在控制端
  1. #1.解压控制端的包到受控端
  2. [root@m01 /package]# ansible web01 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/'
  3. #2.解压受控端的包到受控端
  4. [root@m01 /package]# ansible web02 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/ remote_src=yes'

Ansible主机信息模块

这个模块非常实用

在公司中总会有一些需求

比如:

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

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

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

4.如果安装数据库,分配内存为物理内存的80%,此时有3台不同物理内存的机器2G、4G、16G

写一个playbook的情况下,我需要获取到对应主机的内存并作出计算,写判断。


setup

  1. 获取所有主机信息
  1. [root@m01 ~]# ansible web01 -m setup
  2. web01 | SUCCESS => {
  3. "ansible_facts": {
  4. "ansible_all_ipv4_addresses": [
  5. "10.0.0.7"
  6. ],
  7. "ansible_all_ipv6_addresses": [
  8. "fe80::20c:29ff:fef8:9880"
  9. ],
  10. "ansible_apparmor": {
  11. "status": "disabled"
  12. },
  13. "ansible_architecture": "x86_64",
  14. "ansible_bios_date": "04/13/2018",
  15. "ansible_bios_version": "6.00",
  16. "ansible_cmdline": {
  17. "BOOT_IMAGE": "/vmlinuz-3.10.0-862.el7.x86_64",
  18. "LANG": "en_US.UTF-8",
  19. "biosdevname": "0",
  20. "net.ifnames": "0",
  21. "quiet": true,
  22. "rhgb": true,
  23. "ro": true,
  24. "root": "UUID=7348b9b1-f2a7-46c6-bede-4f22224dc168"
  25. },
  26. "ansible_date_time": {
  27. "date": "2019-09-10",
  28. "day": "10",
  29. "epoch": "1568115243",
  30. "hour": "19",
  31. "iso8601": "2019-09-10T11:34:03Z",
  32. "iso8601_basic": "20190910T193403218395",
  33. "iso8601_basic_short": "20190910T193403",
  34. "iso8601_micro": "2019-09-10T11:34:03.218468Z",
  35. "minute": "34",
  36. "month": "09",
  37. "second": "03",
  38. "time": "19:34:03",
  39. "tz": "CST",
  40. "tz_offset": "+0800",
  41. "weekday": "星期二",
  42. "weekday_number": "2",
  43. "weeknumber": "36",
  44. "year": "2019"
  45. },
  46. "ansible_default_ipv4": {
  47. "address": "10.0.0.7",
  48. "alias": "eth0",
  49. "broadcast": "10.0.0.255",
  50. "gateway": "10.0.0.2",
  51. "interface": "eth0",
  52. "macaddress": "00:0c:29:f8:98:80",
  53. "mtu": 1500,
  54. "netmask": "255.255.255.0",
  55. "network": "10.0.0.0",
  56. "type": "ether"
  57. },
  58. "ansible_default_ipv6": {},
  59. "ansible_device_links": {
  60. "ids": {
  61. "sr0": [
  62. "ata-VMware_Virtual_IDE_CDROM_Drive_00000000000000000001"
  63. ],
  64. "sr1": [
  65. "ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
  66. ]
  67. },
  68. "labels": {},
  69. "masters": {},
  70. "uuids": {
  71. "sda1": [
  72. "8e547355-994a-4bad-a941-da93f4f1cdfd"
  73. ],
  74. "sda2": [
  75. "9e4d046c-02cf-47bd-a4bf-1e8b5fa4bed5"
  76. ],
  77. "sda3": [
  78. "7348b9b1-f2a7-46c6-bede-4f22224dc168"
  79. ]
  80. }
  81. },
  82. "ansible_devices": {
  83. "sda": {
  84. "holders": [],
  85. "host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)",
  86. "links": {
  87. "ids": [],
  88. "labels": [],
  89. "masters": [],
  90. "uuids": []
  91. },
  92. "model": "VMware Virtual S",
  93. "partitions": {
  94. "sda1": {
  95. "holders": [],
  96. "links": {
  97. "ids": [],
  98. "labels": [],
  99. "masters": [],
  100. "uuids": [
  101. "8e547355-994a-4bad-a941-da93f4f1cdfd"
  102. ]
  103. },
  104. "sectors": "2097152",
  105. "sectorsize": 512,
  106. "size": "1.00 GB",
  107. "start": "2048",
  108. "uuid": "8e547355-994a-4bad-a941-da93f4f1cdfd"
  109. },
  110. "sda2": {
  111. "holders": [],
  112. "links": {
  113. "ids": [],
  114. "labels": [],
  115. "masters": [],
  116. "uuids": [
  117. "9e4d046c-02cf-47bd-a4bf-1e8b5fa4bed5"
  118. ]
  119. },
  120. "sectors": "2097152",
  121. "sectorsize": 512,
  122. "size": "1.00 GB",
  123. "start": "2099200",
  124. "uuid": "9e4d046c-02cf-47bd-a4bf-1e8b5fa4bed5"
  125. },
  126. "sda3": {
  127. "holders": [],
  128. "links": {
  129. "ids": [],
  130. "labels": [],
  131. "masters": [],
  132. "uuids": [
  133. "7348b9b1-f2a7-46c6-bede-4f22224dc168"
  134. ]
  135. },
  136. "sectors": "37746688",
  137. "sectorsize": 512,
  138. "size": "18.00 GB",
  139. "start": "4196352",
  140. "uuid": "7348b9b1-f2a7-46c6-bede-4f22224dc168"
  141. }
  142. },
  143. "removable": "0",
  144. "rotational": "1",
  145. "sas_address": null,
  146. "sas_device_handle": null,
  147. "scheduler_mode": "deadline",
  148. "sectors": "41943040",
  149. "sectorsize": "512",
  150. "size": "20.00 GB",
  151. "support_discard": "0",
  152. "vendor": "VMware,",
  153. "virtual": 1
  154. },
  155. "sr0": {
  156. "holders": [],
  157. "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
  158. "links": {
  159. "ids": [
  160. "ata-VMware_Virtual_IDE_CDROM_Drive_00000000000000000001"
  161. ],
  162. "labels": [],
  163. "masters": [],
  164. "uuids": []
  165. },
  166. "model": "VMware IDE CDR00",
  167. "partitions": {},
  168. "removable": "1",
  169. "rotational": "1",
  170. "sas_address": null,
  171. "sas_device_handle": null,
  172. "scheduler_mode": "deadline",
  173. "sectors": "2097151",
  174. "sectorsize": "512",
  175. "size": "1024.00 MB",
  176. "support_discard": "0",
  177. "vendor": "NECVMWar",
  178. "virtual": 1
  179. },
  180. "sr1": {
  181. "holders": [],
  182. "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
  183. "links": {
  184. "ids": [
  185. "ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
  186. ],
  187. "labels": [],
  188. "masters": [],
  189. "uuids": []
  190. },
  191. "model": "VMware IDE CDR10",
  192. "partitions": {},
  193. "removable": "1",
  194. "rotational": "1",
  195. "sas_address": null,
  196. "sas_device_handle": null,
  197. "scheduler_mode": "deadline",
  198. "sectors": "2097151",
  199. "sectorsize": "512",
  200. "size": "1024.00 MB",
  201. "support_discard": "0",
  202. "vendor": "NECVMWar",
  203. "virtual": 1
  204. }
  205. },
  206. "ansible_distribution": "CentOS",
  207. "ansible_distribution_file_parsed": true,
  208. "ansible_distribution_file_path": "/etc/redhat-release",
  209. "ansible_distribution_file_variety": "RedHat",
  210. "ansible_distribution_major_version": "7",
  211. "ansible_distribution_release": "Core",
  212. "ansible_distribution_version": "7.5",
  213. "ansible_dns": {
  214. "nameservers": [
  215. "10.0.0.2"
  216. ]
  217. },
  218. "ansible_domain": "",
  219. "ansible_effective_group_id": 0,
  220. "ansible_effective_user_id": 0,
  221. "ansible_env": {
  222. "HOME": "/root",
  223. "LANG": "zh_CN.UTF-8",
  224. "LESSOPEN": "||/usr/bin/lesspipe.sh %s",
  225. "LOGNAME": "root",
  226. "LS_COLORS": "rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:",
  227. "MAIL": "/var/mail/root",
  228. "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
  229. "PWD": "/root",
  230. "SELINUX_LEVEL_REQUESTED": "",
  231. "SELINUX_ROLE_REQUESTED": "",
  232. "SELINUX_USE_CURRENT_RANGE": "",
  233. "SHELL": "/bin/bash",
  234. "SHLVL": "2",
  235. "SSH_CLIENT": "10.0.0.51 53512 22",
  236. "SSH_CONNECTION": "10.0.0.51 53512 10.0.0.7 22",
  237. "SSH_TTY": "/dev/pts/1",
  238. "TERM": "xterm-256color",
  239. "USER": "root",
  240. "XDG_RUNTIME_DIR": "/run/user/0",
  241. "XDG_SESSION_ID": "87",
  242. "_": "/usr/bin/python"
  243. },
  244. "ansible_eth0": {
  245. "active": true,
  246. "device": "eth0",
  247. "features": {
  248. "busy_poll": "off [fixed]",
  249. "fcoe_mtu": "off [fixed]",
  250. "generic_receive_offload": "on",
  251. "generic_segmentation_offload": "on",
  252. "highdma": "off [fixed]",
  253. "hw_tc_offload": "off [fixed]",
  254. "l2_fwd_offload": "off [fixed]",
  255. "large_receive_offload": "off [fixed]",
  256. "loopback": "off [fixed]",
  257. "netns_local": "off [fixed]",
  258. "ntuple_filters": "off [fixed]",
  259. "receive_hashing": "off [fixed]",
  260. "rx_all": "off",
  261. "rx_checksumming": "off",
  262. "rx_fcs": "off",
  263. "rx_udp_tunnel_port_offload": "off [fixed]",
  264. "rx_vlan_filter": "on [fixed]",
  265. "rx_vlan_offload": "on",
  266. "rx_vlan_stag_filter": "off [fixed]",
  267. "rx_vlan_stag_hw_parse": "off [fixed]",
  268. "scatter_gather": "on",
  269. "tcp_segmentation_offload": "on",
  270. "tx_checksum_fcoe_crc": "off [fixed]",
  271. "tx_checksum_ip_generic": "on",
  272. "tx_checksum_ipv4": "off [fixed]",
  273. "tx_checksum_ipv6": "off [fixed]",
  274. "tx_checksum_sctp": "off [fixed]",
  275. "tx_checksumming": "on",
  276. "tx_fcoe_segmentation": "off [fixed]",
  277. "tx_gre_csum_segmentation": "off [fixed]",
  278. "tx_gre_segmentation": "off [fixed]",
  279. "tx_gso_partial": "off [fixed]",
  280. "tx_gso_robust": "off [fixed]",
  281. "tx_ipip_segmentation": "off [fixed]",
  282. "tx_lockless": "off [fixed]",
  283. "tx_nocache_copy": "off",
  284. "tx_scatter_gather": "on",
  285. "tx_scatter_gather_fraglist": "off [fixed]",
  286. "tx_sctp_segmentation": "off [fixed]",
  287. "tx_sit_segmentation": "off [fixed]",
  288. "tx_tcp6_segmentation": "off [fixed]",
  289. "tx_tcp_ecn_segmentation": "off [fixed]",
  290. "tx_tcp_mangleid_segmentation": "off",
  291. "tx_tcp_segmentation": "on",
  292. "tx_udp_tnl_csum_segmentation": "off [fixed]",
  293. "tx_udp_tnl_segmentation": "off [fixed]",
  294. "tx_vlan_offload": "on [fixed]",
  295. "tx_vlan_stag_hw_insert": "off [fixed]",
  296. "udp_fragmentation_offload": "off [fixed]",
  297. "vlan_challenged": "off [fixed]"
  298. },
  299. "hw_timestamp_filters": [],
  300. "ipv4": {
  301. "address": "10.0.0.7",
  302. "broadcast": "10.0.0.255",
  303. "netmask": "255.255.255.0",
  304. "network": "10.0.0.0"
  305. },
  306. "ipv6": [
  307. {
  308. "address": "fe80::20c:29ff:fef8:9880",
  309. "prefix": "64",
  310. "scope": "link"
  311. }
  312. ],
  313. "macaddress": "00:0c:29:f8:98:80",
  314. "module": "e1000",
  315. "mtu": 1500,
  316. "pciid": "0000:02:01.0",
  317. "promisc": false,
  318. "speed": 1000,
  319. "timestamping": [
  320. "tx_software",
  321. "rx_software",
  322. "software"
  323. ],
  324. "type": "ether"
  325. },
  326. "ansible_fibre_channel_wwn": [],
  327. "ansible_fips": false,
  328. "ansible_form_factor": "Other",
  329. "ansible_fqdn": "web01",
  330. "ansible_hostname": "web01",
  331. "ansible_hostnqn": "",
  332. "ansible_interfaces": [
  333. "lo",
  334. "eth0"
  335. ],
  336. "ansible_is_chroot": false,
  337. "ansible_iscsi_iqn": "",
  338. "ansible_kernel": "3.10.0-862.el7.x86_64",
  339. "ansible_lo": {
  340. "active": true,
  341. "device": "lo",
  342. "features": {
  343. "busy_poll": "off [fixed]",
  344. "fcoe_mtu": "off [fixed]",
  345. "generic_receive_offload": "on",
  346. "generic_segmentation_offload": "on",
  347. "highdma": "on [fixed]",
  348. "hw_tc_offload": "off [fixed]",
  349. "l2_fwd_offload": "off [fixed]",
  350. "large_receive_offload": "off [fixed]",
  351. "loopback": "on [fixed]",
  352. "netns_local": "on [fixed]",
  353. "ntuple_filters": "off [fixed]",
  354. "receive_hashing": "off [fixed]",
  355. "rx_all": "off [fixed]",
  356. "rx_checksumming": "on [fixed]",
  357. "rx_fcs": "off [fixed]",
  358. "rx_udp_tunnel_port_offload": "off [fixed]",
  359. "rx_vlan_filter": "off [fixed]",
  360. "rx_vlan_offload": "off [fixed]",
  361. "rx_vlan_stag_filter": "off [fixed]",
  362. "rx_vlan_stag_hw_parse": "off [fixed]",
  363. "scatter_gather": "on",
  364. "tcp_segmentation_offload": "on",
  365. "tx_checksum_fcoe_crc": "off [fixed]",
  366. "tx_checksum_ip_generic": "on [fixed]",
  367. "tx_checksum_ipv4": "off [fixed]",
  368. "tx_checksum_ipv6": "off [fixed]",
  369. "tx_checksum_sctp": "on [fixed]",
  370. "tx_checksumming": "on",
  371. "tx_fcoe_segmentation": "off [fixed]",
  372. "tx_gre_csum_segmentation": "off [fixed]",
  373. "tx_gre_segmentation": "off [fixed]",
  374. "tx_gso_partial": "off [fixed]",
  375. "tx_gso_robust": "off [fixed]",
  376. "tx_ipip_segmentation": "off [fixed]",
  377. "tx_lockless": "on [fixed]",
  378. "tx_nocache_copy": "off [fixed]",
  379. "tx_scatter_gather": "on [fixed]",
  380. "tx_scatter_gather_fraglist": "on [fixed]",
  381. "tx_sctp_segmentation": "on",
  382. "tx_sit_segmentation": "off [fixed]",
  383. "tx_tcp6_segmentation": "on",
  384. "tx_tcp_ecn_segmentation": "on",
  385. "tx_tcp_mangleid_segmentation": "on",
  386. "tx_tcp_segmentation": "on",
  387. "tx_udp_tnl_csum_segmentation": "off [fixed]",
  388. "tx_udp_tnl_segmentation": "off [fixed]",
  389. "tx_vlan_offload": "off [fixed]",
  390. "tx_vlan_stag_hw_insert": "off [fixed]",
  391. "udp_fragmentation_offload": "on",
  392. "vlan_challenged": "on [fixed]"
  393. },
  394. "hw_timestamp_filters": [],
  395. "ipv4": {
  396. "address": "127.0.0.1",
  397. "broadcast": "host",
  398. "netmask": "255.0.0.0",
  399. "network": "127.0.0.0"
  400. },
  401. "ipv6": [
  402. {
  403. "address": "::1",
  404. "prefix": "128",
  405. "scope": "host"
  406. }
  407. ],
  408. "mtu": 65536,
  409. "promisc": false,
  410. "timestamping": [
  411. "rx_software",
  412. "software"
  413. ],
  414. "type": "loopback"
  415. },
  416. "ansible_local": {},
  417. "ansible_lsb": {},
  418. "ansible_machine": "x86_64",
  419. "ansible_machine_id": "c9d400bd3c1249bd81b2d49252985ab6",
  420. "ansible_memfree_mb": 1068,
  421. "ansible_memory_mb": {
  422. "nocache": {
  423. "free": 1622,
  424. "used": 360
  425. },
  426. "real": {
  427. "free": 1068,
  428. "total": 1982,
  429. "used": 914
  430. },
  431. "swap": {
  432. "cached": 0,
  433. "free": 1023,
  434. "total": 1023,
  435. "used": 0
  436. }
  437. },
  438. "ansible_memtotal_mb": 1982,
  439. "ansible_mounts": [
  440. {
  441. "block_available": 227935,
  442. "block_size": 4096,
  443. "block_total": 259584,
  444. "block_used": 31649,
  445. "device": "/dev/sda1",
  446. "fstype": "xfs",
  447. "inode_available": 523962,
  448. "inode_total": 524288,
  449. "inode_used": 326,
  450. "mount": "/boot",
  451. "options": "rw,seclabel,relatime,attr2,inode64,noquota",
  452. "size_available": 933621760,
  453. "size_total": 1063256064,
  454. "uuid": "8e547355-994a-4bad-a941-da93f4f1cdfd"
  455. },
  456. {
  457. "block_available": 69275,
  458. "block_size": 262144,
  459. "block_total": 73684,
  460. "block_used": 4409,
  461. "device": "10.0.0.31:/data",
  462. "fstype": "nfs4",
  463. "inode_available": 9409536,
  464. "inode_total": 9436672,
  465. "inode_used": 27136,
  466. "mount": "/opt",
  467. "options": "rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.0.0.7,local_lock=none,addr=10.0.0.31",
  468. "size_available": 18160025600,
  469. "size_total": 19315818496,
  470. "uuid": "N/A"
  471. },
  472. {
  473. "block_available": 4354375,
  474. "block_size": 4096,
  475. "block_total": 4715776,
  476. "block_used": 361401,
  477. "device": "/dev/sda3",
  478. "fstype": "xfs",
  479. "inode_available": 9403419,
  480. "inode_total": 9436672,
  481. "inode_used": 33253,
  482. "mount": "/",
  483. "options": "rw,seclabel,relatime,attr2,inode64,noquota",
  484. "size_available": 17835520000,
  485. "size_total": 19315818496,
  486. "uuid": "7348b9b1-f2a7-46c6-bede-4f22224dc168"
  487. }
  488. ],
  489. "ansible_nodename": "web01",
  490. "ansible_os_family": "RedHat",
  491. "ansible_pkg_mgr": "yum",
  492. "ansible_proc_cmdline": {
  493. "BOOT_IMAGE": "/vmlinuz-3.10.0-862.el7.x86_64",
  494. "LANG": "en_US.UTF-8",
  495. "biosdevname": "0",
  496. "net.ifnames": "0",
  497. "quiet": true,
  498. "rhgb": true,
  499. "ro": true,
  500. "root": "UUID=7348b9b1-f2a7-46c6-bede-4f22224dc168"
  501. },
  502. "ansible_processor": [
  503. "0",
  504. "GenuineIntel",
  505. "Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz"
  506. ],
  507. "ansible_processor_cores": 1,
  508. "ansible_processor_count": 1,
  509. "ansible_processor_threads_per_core": 1,
  510. "ansible_processor_vcpus": 1,
  511. "ansible_product_name": "VMware Virtual Platform",
  512. "ansible_product_serial": "VMware-56 4d a5 a2 9d f3 51 25-4d 67 a8 58 f8 f8 98 80",
  513. "ansible_product_uuid": "A2A54D56-F39D-2551-4D67-A858F8F89880",
  514. "ansible_product_version": "None",
  515. "ansible_python": {
  516. "executable": "/usr/bin/python",
  517. "has_sslcontext": true,
  518. "type": "CPython",
  519. "version": {
  520. "major": 2,
  521. "micro": 5,
  522. "minor": 7,
  523. "releaselevel": "final",
  524. "serial": 0
  525. },
  526. "version_info": [
  527. 2,
  528. 7,
  529. 5,
  530. "final",
  531. 0
  532. ]
  533. },
  534. "ansible_python_version": "2.7.5",
  535. "ansible_real_group_id": 0,
  536. "ansible_real_user_id": 0,
  537. "ansible_selinux": {
  538. "config_mode": "disabled",
  539. "mode": "permissive",
  540. "policyvers": 31,
  541. "status": "enabled",
  542. "type": "targeted"
  543. },
  544. "ansible_selinux_python_present": true,
  545. "ansible_service_mgr": "systemd",
  546. "ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAiDBJtsjcCuaEVqC4e2tPeN3X7FbSfbWq4gDx65v5AX8yPzZcufMmv0yydrCvbkb3HhMGqVJ7oNMioQdyqiu8Q=",
  547. "ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIBVg0/vQDn4AFzoNyeGcB61Jr3a+Cv3hu36XOW+BAgv+",
  548. "ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQC4PhQf/9RtL4kuFejVDjQoT8ng10Wdf5SA884Nu9l5wfrBLTVpKUusox5g4lU9+cuYicZiEYmasvxQbACsI90OybLUs26eUymRMtYQiS+N9Mfz0I+CLSssIEtUd5nplNoaPLM7dvgej1YxzLoz8mF6XkwhTLCd3nnye/YxuYYecGNZRCi2q6mkMYjO0HuHtqeSyoK+gPB2so7p7QrC3kcYbgblKfztDDUJ11tmYTBQJdDm7+ICztFjiwyWsnOvbItpOyI2M6neDkN8KCqoDwDYKCbXSbs6uamWkInlz03G9LGuIf+B/rhG6pmFVxG3Ac9h1tS5b6H2DJRMxQR+Vf5/",
  549. "ansible_swapfree_mb": 1023,
  550. "ansible_swaptotal_mb": 1023,
  551. "ansible_system": "Linux",
  552. "ansible_system_capabilities": [
  553. "cap_chown",
  554. "cap_dac_override",
  555. "cap_dac_read_search",
  556. "cap_fowner",
  557. "cap_fsetid",
  558. "cap_kill",
  559. "cap_setgid",
  560. "cap_setuid",
  561. "cap_setpcap",
  562. "cap_linux_immutable",
  563. "cap_net_bind_service",
  564. "cap_net_broadcast",
  565. "cap_net_admin",
  566. "cap_net_raw",
  567. "cap_ipc_lock",
  568. "cap_ipc_owner",
  569. "cap_sys_module",
  570. "cap_sys_rawio",
  571. "cap_sys_chroot",
  572. "cap_sys_ptrace",
  573. "cap_sys_pacct",
  574. "cap_sys_admin",
  575. "cap_sys_boot",
  576. "cap_sys_nice",
  577. "cap_sys_resource",
  578. "cap_sys_time",
  579. "cap_sys_tty_config",
  580. "cap_mknod",
  581. "cap_lease",
  582. "cap_audit_write",
  583. "cap_audit_control",
  584. "cap_setfcap",
  585. "cap_mac_override",
  586. "cap_mac_admin",
  587. "cap_syslog",
  588. "35",
  589. "36+ep"
  590. ],
  591. "ansible_system_capabilities_enforced": "True",
  592. "ansible_system_vendor": "VMware, Inc.",
  593. "ansible_uptime_seconds": 96743,
  594. "ansible_user_dir": "/root",
  595. "ansible_user_gecos": "root",
  596. "ansible_user_gid": 0,
  597. "ansible_user_id": "root",
  598. "ansible_user_shell": "/bin/bash",
  599. "ansible_user_uid": 0,
  600. "ansible_userspace_architecture": "x86_64",
  601. "ansible_userspace_bits": "64",
  602. "ansible_virtualization_role": "guest",
  603. "ansible_virtualization_type": "VMware",
  604. "discovered_interpreter_python": "/usr/bin/python",
  605. "gather_subset": [
  606. "all"
  607. ],
  608. "module_setup": true
  609. },
  610. "changed": false
  611. }
  1. 获取IP地址(使用setup获取的信息,指定对应的小标题获取指定的信息)
  1. [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_default_ipv4'
  2. web01 | SUCCESS => {
  3. "ansible_facts": {
  4. "ansible_default_ipv4": {
  5. "address": "10.0.0.7",
  6. "alias": "eth0",
  7. "broadcast": "10.0.0.255",
  8. "gateway": "10.0.0.2",
  9. "interface": "eth0",
  10. "macaddress": "00:0c:29:f8:98:80",
  11. "mtu": 1500,
  12. "netmask": "255.255.255.0",
  13. "network": "10.0.0.0",
  14. "type": "ether"
  15. },
  16. "discovered_interpreter_python": "/usr/bin/python"
  17. },
  18. "changed": false
  19. }
  1. 获取主机名
  1. [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_fqdn'
  2. web01 | SUCCESS => {
  3. "ansible_facts": {
  4. "ansible_fqdn": "web01",
  5. "discovered_interpreter_python": "/usr/bin/python"
  6. },
  7. "changed": false
  8. }
  1. 获取内存信息
  1. [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_memory_mb'
  2. web01 | SUCCESS => {
  3. "ansible_facts": {
  4. "ansible_memory_mb": {
  5. "nocache": {
  6. "free": 1622,
  7. "used": 360
  8. },
  9. "real": {
  10. "free": 1068,
  11. "total": 1982,
  12. "used": 914
  13. },
  14. "swap": {
  15. "cached": 0,
  16. "free": 1023,
  17. "total": 1023,
  18. "used": 0
  19. }
  20. },
  21. "discovered_interpreter_python": "/usr/bin/python"
  22. },
  23. "changed": false
  24. }
  1. 获取磁盘信息
  1. web01 | SUCCESS => {
  2. "ansible_facts": {
  3. "ansible_memory_mb": {
  4. "nocache": {
  5. "free": 1622,
  6. "used": 360
  7. },
  8. "real": {
  9. "free": 1068,
  10. "total": 1982,
  11. "used": 914
  12. },
  13. "swap": {
  14. "cached": 0,
  15. "free": 1023,
  16. "total": 1023,
  17. "used": 0
  18. }
  19. },
  20. "discovered_interpreter_python": "/usr/bin/python"
  21. },
  22. "changed": false
  23. }
  24. [root@m01 ~]# ansible_devices^C
  25. [root@m01 ~]# ansible web01 -m setup -a 'filter=ansible_devices'
  26. web01 | SUCCESS => {
  27. "ansible_facts": {
  28. "ansible_devices": {
  29. "sda": {
  30. "holders": [],
  31. "host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)",
  32. "links": {
  33. "ids": [],
  34. "labels": [],
  35. "masters": [],
  36. "uuids": []
  37. },
  38. "model": "VMware Virtual S",
  39. "partitions": {
  40. "sda1": {
  41. "holders": [],
  42. "links": {
  43. "ids": [],
  44. "labels": [],
  45. "masters": [],
  46. "uuids": [
  47. "8e547355-994a-4bad-a941-da93f4f1cdfd"
  48. ]
  49. },
  50. "sectors": "2097152",
  51. "sectorsize": 512,
  52. "size": "1.00 GB",
  53. "start": "2048",
  54. "uuid": "8e547355-994a-4bad-a941-da93f4f1cdfd"
  55. },
  56. "sda2": {
  57. "holders": [],
  58. "links": {
  59. "ids": [],
  60. "labels": [],
  61. "masters": [],
  62. "uuids": [
  63. "9e4d046c-02cf-47bd-a4bf-1e8b5fa4bed5"
  64. ]
  65. },
  66. "sectors": "2097152",
  67. "sectorsize": 512,
  68. "size": "1.00 GB",
  69. "start": "2099200",
  70. "uuid": "9e4d046c-02cf-47bd-a4bf-1e8b5fa4bed5"
  71. },
  72. "sda3": {
  73. "holders": [],
  74. "links": {
  75. "ids": [],
  76. "labels": [],
  77. "masters": [],
  78. "uuids": [
  79. "7348b9b1-f2a7-46c6-bede-4f22224dc168"
  80. ]
  81. },
  82. "sectors": "37746688",
  83. "sectorsize": 512,
  84. "size": "18.00 GB",
  85. "start": "4196352",
  86. "uuid": "7348b9b1-f2a7-46c6-bede-4f22224dc168"
  87. }
  88. },
  89. "removable": "0",
  90. "rotational": "1",
  91. "sas_address": null,
  92. "sas_device_handle": null,
  93. "scheduler_mode": "deadline",
  94. "sectors": "41943040",
  95. "sectorsize": "512",
  96. "size": "20.00 GB",
  97. "support_discard": "0",
  98. "vendor": "VMware,",
  99. "virtual": 1
  100. },
  101. "sr0": {
  102. "holders": [],
  103. "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
  104. "links": {
  105. "ids": [
  106. "ata-VMware_Virtual_IDE_CDROM_Drive_00000000000000000001"
  107. ],
  108. "labels": [],
  109. "masters": [],
  110. "uuids": []
  111. },
  112. "model": "VMware IDE CDR00",
  113. "partitions": {},
  114. "removable": "1",
  115. "rotational": "1",
  116. "sas_address": null,
  117. "sas_device_handle": null,
  118. "scheduler_mode": "deadline",
  119. "sectors": "2097151",
  120. "sectorsize": "512",
  121. "size": "1024.00 MB",
  122. "support_discard": "0",
  123. "vendor": "NECVMWar",
  124. "virtual": 1
  125. },
  126. "sr1": {
  127. "holders": [],
  128. "host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
  129. "links": {
  130. "ids": [
  131. "ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
  132. ],
  133. "labels": [],
  134. "masters": [],
  135. "uuids": []
  136. },
  137. "model": "VMware IDE CDR10",
  138. "partitions": {},
  139. "removable": "1",
  140. "rotational": "1",
  141. "sas_address": null,
  142. "sas_device_handle": null,
  143. "scheduler_mode": "deadline",
  144. "sectors": "2097151",
  145. "sectorsize": "512",
  146. "size": "1024.00 MB",
  147. "support_discard": "0",
  148. "vendor": "NECVMWar",
  149. "virtual": 1
  150. }
  151. },
  152. "discovered_interpreter_python": "/usr/bin/python"
  153. },
  154. "changed": false
  155. }
  1. 其他信息参数
  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的时候,会针对这些内置变量参考使用。

Ansible示例

一键部署rsync,nfs,nginx,httpd,上传作业代码

  1. m01配置密钥登录
  1. [root@m01 ~]# yum install -y ansible
  2. # 创建密钥对
  3. [root@m01 ~]# ssh-keygen
  4. # 推送公钥
  5. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.4
  6. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.5
  7. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.6
  8. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7
  9. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8
  10. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.9
  11. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31
  12. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
  13. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.51
  14. [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.61
  1. 编写主机清单
  1. [web_group]
  2. web01 ansible_ssh_host=172.16.1.7
  3. web02 ansible_ssh_host=172.16.1.8
  4. [db_group]
  5. db01 ansible_ssh_host=172.16.1.51
  6. [nfs_group]
  7. nfs ansible_ssh_host=172.16.1.31
  8. [backup_group]
  9. backup ansible_ssh_host=172.16.1.41
  10. [lb_group]
  11. lb01 ansible_ssh_host=172.16.1.5
  12. lb02 ansible_ssh_host=172.16.1.6
  13. [rsync_server:children]
  14. nfs_group
  15. backup_group
  16. [nfs_server:children]
  17. web_group
  18. nfs_group
  19. [lnmp_server:children]
  20. web_group
  21. lb_group
  1. 编写脚本测试
  1. #!/bin/bash
  2. # 创建用户及组
  3. ansible 'all' -m group -a 'name=www gid=666 state=present' &&\
  4. ansible 'all' -m user -a 'name=www uid=666 group=www state=present shell=/sbin/nologin create_home=false' &&\
  5. # 部署httpd服务
  6. ansible 'web_group' -m yum -a 'name=httpd,php state=present' &&\
  7. # 替换http服务启动用户及组
  8. ansible 'web_group' -m shell -a "sed -i '/^User/c User www' /etc/httpd/conf/httpd.conf" &&\
  9. ansible 'web_group' -m shell -a "sed -i '/^Group/c Group www' /etc/httpd/conf/httpd.conf" &&\
  10. # 启动httpd服务
  11. ansible 'web_group' -m systemd -a 'name=httpd state=started enabled=yes' &&\
  12. # 上传代码,并修改图片路径
  13. ansible 'web_group' -m copy -a 'src=/root/httpd_file/ dest=/var/www/html/ owner=www group=www '
  14. # 远程推送作业代码
  15. ansible 'web_group' -m file -a 'path=/var/www/html/uploads state=directory owner=www group=www' &&\
  16. # 部署nfs服务
  17. ansible 'nfs_server' -m yum -a 'name=nfs-utils state=present' &&\
  18. # 推送nfs服务配置文件
  19. ansible 'nfs_group' -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports' &&\
  20. # 启动nfs服务并开机自启
  21. ansible 'nfs_server' -m systemd -a 'name=nfs-server state=started enabled=yes' &&\
  22. # 远程下载rsync服务
  23. ansible 'rsync_server' -m yum -a 'name=rsync state=present' &&\
  24. # 推送rsync配置文件
  25. ansible 'backup_group' -m copy -a 'src=/root/rsync_file dest=/etc/rsyncd.conf' &&\
  26. # 推送密码文件至rsync服务端
  27. ansible 'backup_group' -m copy -a 'content=rsync_backup:123 dest=/etc/rsync.passwd mode=0600' &&\
  28. # 推送密码文件至rsync客户端
  29. ansible 'nfs_group' -m copy -a 'content=123 dest=/etc/rsync.pass mode=0600' &&\
  30. # 启动rsync服务,并加入开机自启
  31. ansible 'rsync_server' -m systemd -a 'name=rsyncd state=started enabled=yes' &&\
  32. # 远程下载mariadb服务
  33. ansible 'db_group' -m yum -a 'name=mariadb-server state=present' &&\
  34. # 启动并加入开机自启
  35. ansible 'db_group' -m systemd -a 'name=mariadb state=started enabled=yes' &&\
  36. # 推送nginx官方源
  37. ansible 'lnmp_server' -m copy -a 'src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/' &&\
  38. # 远程下载nginx
  39. ansible 'lnmp_server' -m yum -a 'name=nginx state=present' &&\
  40. # 远程修改nginx配置文件
  41. ansible 'lnmp_server' -m shell -a "sed -i '/^user/c user www;' /etc/nginx/nginx.conf" &&\
  42. # 启动nginx
  43. ansible 'lnmp_server' -m systemd -a 'name=nginx state=started enabled=yes'
  1. 编写脚本所需配置文件
  1. [root@m01 ~]# vim /root/rsync_file
  2. uid = www
  3. gid = www
  4. port = 873
  5. fake super = yes
  6. use chroot = no
  7. max connections = 200
  8. timeout = 600
  9. ignore errors
  10. read only = false
  11. list = false
  12. auth users = rsync_backup
  13. secrets file = /etc/rsync.passwd
  14. log file = /var/log/rsyncd.log
  15. #####################################
  16. [backup]
  17. comment = welcome to oldboyedu backup!
  18. path = /backup
  19. [data]
  20. comment = welcome to oldboyedu nfs!
  21. path = /data
  22. # 传作业压缩包到目录里,并解压修改上传图片路径
  23. [root@m01 ~]# cd httpd_file
  24. # 创建nginx官方源
  25. [root@m01 ~]# vim /etc/yum.repos.d/nginx.repo
  26. [nginx]
  27. name=nginx repo
  28. baseurl=http://nginx.org/packages/centos/7/$basearch/
  29. gpgcheck=0
  30. enabled=1
  1. 执行脚本

    测试:web上http服务,上传作业代码

测试:lb上nginx服务

测试:测试db上mysql服务

测试:nfs服务

测试:rsync服务

自动化运维工具-Ansible之2-ad-hoc的更多相关文章

  1. CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机

    使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...

  2. 自动化运维工具Ansible详细部署 (转载)

    自动化运维工具Ansible详细部署 标签:ansible 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://sofar.blog. ...

  3. 自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客

    自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客 自动化运维工具Ansible详细部署

  4. 自动化运维工具Ansible介绍

    一个由 Python 编写的强大的配置管理解决方案.尽管市面上已经有很多可供选择的配置管理解决方案,但他们各有优劣,而 ansible 的特点就在于它的简洁. 让 ansible 在主流的配置管理系统 ...

  5. 在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操

    前言 Ansible是一款优秀的自动化IT运维工具,具有远程安装.远程部署应用.远程管理能力,支持Windows.Linux.Unix.macOS和大型机等多种操作系统. 下面就以CentOS 7.6 ...

  6. 自动化运维工具-Ansible基础

    目录 自动化运维工具-Ansible基础 什么是Ansible 同类型软件对比 Ansible的功能及优点 Ansible的架构 Ansible的执行流程 安装Ansible ansible配置文件 ...

  7. 自动化运维工具-Ansible之7-roles

    自动化运维工具-Ansible之7-roles 目录 自动化运维工具-Ansible之7-roles Ansible Roles基本概述 Ansible Roles目录结构 Ansible Roles ...

  8. 自动化运维工具-Ansible之6-Jinja2模板

    自动化运维工具-Ansible之6-Jinja2模板 目录 自动化运维工具-Ansible之6-Jinja2模板 Ansible Jinja2模板概述 Ansible Jinja2模板使用 Ansib ...

  9. 自动化运维工具-Ansible之5-流程控制

    自动化运维工具-Ansible之5-流程控制 目录 自动化运维工具-Ansible之5-流程控制 playbook条件语句 单条件 多条件 多条件运算 示例 playbook循环语句 with_ite ...

  10. 自动化运维工具-Ansible之3-playbook

    自动化运维工具-Ansible之3-playbook 目录 自动化运维工具-Ansible之3-playbook PlayBook初识 YAML语法 PlayBook部署httpd PlayBook实 ...

随机推荐

  1. 老猿学5G扫盲贴:与用户和终端相关的名词UE、SUPI、GPSI、PEI

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 和4 ...

  2. Python字符串学习相关问题

    Python中format_map与format字符串格式化的区别 Python中使用f字符串进行字符串格式化的方法 Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同? ...

  3. PyQt学习随笔:Qt事件类QEvent详解

    QEvent类是PyQt5.QtCore中定义的事件处理的基类,事件对象包含了事件对应的参数. <Python & PyQt学习随笔:PyQt主程序的基本框架>介绍了PyQt程序通 ...

  4. MongoDB 复合索引结构

  5. go学习49天

    写文件操作 func OpenFile(name string,flag int,perm FileMode) (file *File,err error)

  6. 题解-CF802C Heidi and Library (hard)

    题面 CF802C Heidi and Library (hard) 有一个大小为 \(k\) 的空书架.有 \(n\) 天和 \(n\) 种书,每天要求书架中有书 \(a_i\).每天可以多次买书, ...

  7. protobuf 协议浅析

    目录 Protobuf 协议浅析 1. Protobuf 介绍 1.1 Protobuf 基本概念 1.2 Protobuf 的优点 1.3 Protobuf, JSON, XML 的区别 2. Pr ...

  8. Android之Activity启动流程详解(基于api28)

    前言 Activity作为Android四大组件之一,他的启动绝对没有那么简单.这里涉及到了系统服务进程,启动过程细节很多,这里我只展示主体流程.activity的启动流程随着版本的更替,代码细节一直 ...

  9. sql注入之双查询注入

    双查询注入前需要了解什么是子查询 子查询可以理解在一个select语句中再插入一个select 里面的select语句就是子查询 例子:select concat((select database() ...

  10. 新手入门 : Windows Phone 8.1 开发 视频学习地址

    本视频资源来自Microsoft Virtual Academy http://www.microsoftvirtualacademy.com/ 下面为视频下载地址! 新手入门 : Windows P ...