jsp网站开发分享网站,做非洲国际贸易网站,wordpress 安全部署,济源做网站前言运维自动化
云计算核心职能 搭建平台架构 日常运营保障 性能效率优化 相关工具 代码管理#xff08;SCM#xff09;#xff1a;GitHub、GitLab、BitBucket、SubVersion 构建工具#xff1a;maven、Ant、Gradle 自动部署#xff1a;Capistrano、CodeDeploy 持续…前言运维自动化
云计算核心职能 搭建平台架构 日常运营保障 性能效率优化 相关工具 代码管理SCMGitHub、GitLab、BitBucket、SubVersion 构建工具maven、Ant、Gradle 自动部署Capistrano、CodeDeploy 持续集成CIJenkins、Travis 配置管理Ansible、SaltStack、Chef、Puppet 容器Docker、Podman、LXC、第三方厂商如AWS 编排Kubernetes、Core、Apache Mesos 服务注册与发现Zookeeper、etcd、Consul 脚本语言python、ruby、shell、go 日志管理ELK、Logentries 系统监控Prometheus、Zabbix、Datadog、Graphite、Ganglia、Nagios 性能监控AppDynamics、New Relic、Splunk 压力测试JMeter、Blaze Meter、loader.io 应用服务器Tomcat、JBoss、IIS Web服务器Apache、Nginx 数据库MySQL、Oracle、PostgreSQL等关系型数据库mongoDB、redis等NoSQL数据库 项目管理PMJira、Asana、Taiga、Trello、Basecamp、Pivotal Tracker 1. Ansible 发展史及功能
作者Michael DeHaan Cobbler pxe kikstar 与 Func 作者ansible 的名称来自科幻小说《安德的游戏》中跨越时空的即时通信工具使用它可以在相距数光年的距离远程实时控制前线的舰队战斗2012-03-09发布0.0.1版2015-10-17Red Hat宣布1.5亿美元收购。
官网https://www.ansible.com/ 官方文档https://docs.ansible.com/
1.1Ansible 功能
批量执行远程命令,可以对远程的多台主机同时进行命令的执行批量安装和配置软件服务可以对远程的多台主机进行自动化的方式配置和管理各种服务编排高级的企业级复杂的IT架构任务, Ansible的Playbook和role可以轻松实现大型的IT复杂架构提供自动化运维工具的开发API, 有很多运维工具,如jumpserver堡垒机就是基于 ansible 实现自动化管理功能
1.2 Ansible 特性
- 模块化调用特定的模块完成特定任务支持自定义模块可使用任何编程语言写模块账号软件等
- Paramikopython对ssh的实现PyYAMLJinja2模板语言三个关键模块
- 基于Python语言实现
- 部署简单基于python和SSH(默认已安装)agentless无需代理不依赖PKI无需ssl
- 安全基于OpenSSH
- 幂等性一个任务执行1遍和执行n遍效果一样不因重复执行带来意外情况,此特性非绝对
- 支持playbook编排任务YAML格式编排任务支持丰富的数据结构 剧本演员要按照 系统按照你规定的方式去执行命令
- 较强大的多层解决方案 role
1.3 Ansible 架构
1.3.1 Ansible组成
- INVENTORYAnsible管理主机的清单 /etc/anaible/hosts 需要管理的服务清单
- MODULESAnsible执行命令的功能模块多数为内置核心模块也可自定义
- PLUGINS模块功能的补充如连接类型插件、循环插件、变量插件、过滤插件等该功能不常用
- API供第三方程序调用的应用程序编程接口
1.3.2 Ansible 命令执行来源
- USER 普通用户即SYSTEM ADMINISTRATOR- PLAYBOOKS任务剧本任务集编排定义Ansible任务集的配置文件由Ansible顺序依次执行通常是JSON格式的YML文件- CMDB配置管理数据库 API 调用- PUBLIC/PRIVATE CLOUD API调用
2.Ansible 安装和入门
2.1安装
开启SELinux来限制进程的权限防止恶意程序通过提权等方式对系统进行攻击
########yum源安装###############
[rootnode1 yum.repos.d]#vim CentOS-Base.repo
#加入 epel源
[epel]
namegn
baseurlhttps://mirrors.aliyun.com/epel/$releasever/x86_64https://mirrors.cloud.tencent.com/epel/$releasever/x86_64https://mirrors.huaweicloud.com/epel/$releasever/x86_64https://mirrors.tuna.tsinghua.edu.cn/epel/$releasever/x86_64
gpgcheck0[rootnode1 yum.repos.d]#yum info ansible
已加载插件fastestmirror, langpacks
base | 3.6 kB 00:00:00
epeel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/3): epeel/7/group_gz | 96 kB 00:00:00
(2/3): epeel/7/updateinfo | 1.0 MB 00:00:00
(3/3): epeel/7/primary_db | 7.0 MB 00:00:04
Loading mirror speeds from cached hostfile* base: mirrors.163.com* epeel: mirrors.aliyun.com* extras: mirrors.163.com* updates: mirrors.163.com
可安装的软件包
名称 ansible
架构 noarch
版本 2.9.25
发布 1.el7
大小 17 M
源 epeel/7
简介 SSH-based configuration management, deployment, and task execution system
网址 http://ansible.com
协议 GPLv3
描述 Ansible is a radically simple model-driven configuration management,: multi-node deployment, and remote task execution system. Ansible works: over SSH and does not require any software or daemons to be installed: on remote nodes. Extension modules can be written in any language and: are transferred to managed machines automatically.[rootnode1 yum.repos.d]#yum install ansible -y
......................省略..................................
已安装:ansible.noarch 0:2.9.25-1.el7 作为依赖被安装:PyYAML.x86_64 0:3.10-11.el7 libyaml.x86_64 0:0.1.4-11.el7_0 python-babel.noarch 0:0.9.6-8.el7 python-jinja2.noarch 0:2.7.2-4.el7 python-markupsafe.x86_64 0:0.11-10.el7 python-paramiko.noarch 0:2.1.1-9.el7 python2-httplib2.noarch 0:0.18.1-3.el7 python2-jmespath.noarch 0:0.9.4-2.el7 完毕yum install epel-release.noarch -y
yum install ansible -y###############Git方式#######################
yum install git
git clone git://github.com/ansible/ansible.git --recursive
cd ./ansible
source ./hacking/env-setup
操作安装ansible
[roottest1 ~]# systemctl stop firewalld
[roottest1 ~]# systemctl disable firewalld
[roottest1 ~]# setenforce 0
[roottest1 ~]# yum install -y epel-releaseInstalled:epel-release.noarch 0:7-11 Complete!
[roottest1 ~]# yum install ansible -yDependency Installed:PyYAML.x86_64 0:3.10-11.el7 libyaml.x86_64 0:0.1.4-11.el7_0 python-babel.noarch 0:0.9.6-8.el7 python-jinja2.noarch 0:2.7.2-4.el7 python-markupsafe.x86_64 0:0.11-10.el7 python-paramiko.noarch 0:2.1.1-9.el7 python2-httplib2.noarch 0:0.18.1-3.el7 python2-jmespath.noarch 0:0.9.4-2.el7 sshpass.x86_64 0:1.06-2.el7 Complete!
[roottest1 ~]# ansible --version
ansible 2.9.27config file /etc/ansible/ansible.cfgconfigured module search path [u/root/.ansible/plugins/modules, u/usr/share/ansible/plugins/modules]ansible python module location /usr/lib/python2.7/site-packages/ansibleexecutable location /usr/bin/ansiblepython version 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
[roottest1 ~]#
[roottest1 ~]# yum info ansible
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* epel: mirrors.bfsu.edu.cn* extras: mirrors.nju.edu.cn* updates: mirrors.nju.edu.cn
Installed Packages
Name : ansible
Arch : noarch
Version : 2.9.27
Release : 1.el7
Size : 103 M
Repo : installed
From repo : epel
Summary : SSH-based configuration management, deployment, and task execution: system
URL : http://ansible.com
License : GPLv3
Description : Ansible is a radically simple model-driven configuration management,: multi-node deployment, and remote task execution system. Ansible works: over SSH and does not require any software or daemons to be installed: on remote nodes. Extension modules can be written in any language and: are transferred to managed machines automatically.2.2查看基本信息
[roottest1 ~]# ansible --version
ansible 2.9.27config file /etc/ansible/ansible.cfgconfigured module search path [u/root/.ansible/plugins/modules, u/usr/share/ansible/plugins/modules]ansible python module location /usr/lib/python2.7/site-packages/ansibleexecutable location /usr/bin/ansiblepython version 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]2.3相关文件
- /etc/ansible/ansible.cfg 主配置文件配置ansible工作特性,也可以在项目的目录中创建此文件,当前目录下如果也有ansible.cfg,则此文件优先生效,建议每个项目目录下,创建独有的ansible.cfg文 件- /etc/ansible/hosts 主机清单;存放下属客机
- /etc/ansible/roles/ 存放角色的目录
2.4 ansible 主配置文件
Ansible 的配置文件可以放在多个不同地方,优先级从高到低顺序如下
ANSIBLE_CONFIG #环境变量,注意此项用 ansible --version 看不到,但可以生效
./ansible.cfg #当前目录下的ansible.cfg
~/.ansible.cfg #当前用户家目录下的.ansible.cfg
/etc/ansible/ansible.cfg #系统默认配置文件
可以使用 来查看
[rootnode1 yum.repos.d]#ansible --version
Ansible 的默认配置文件 /etc/ansible/ansible.cfg ,其中大部分的配置内容无需进行修改
[defaults]
#inventory /etc/ansible/hosts #主机列表配置文件
#library /usr/share/my_modules/ #库文件存放目录
#remote_tmp $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp $HOME/.ansible/tmp #本机的临时命令执行目录
#forks 5 #默认并发数
#sudo_user root #默认sudo 用户
#ask_sudo_pass True #每次执行ansible命令是否询问ssh密码
#ask_pass True
#remote_port 22
#host_key_checking False #检查对应服务器的host_key建议取消此行注释,实现第一次连接自动信任目标主机
#log_path/var/log/ansible.log #日志文件建议启用
#module_name command 、mod #默认模块可以修改为shell模块
[privilege_escalation] #普通用户提权配置
#becomeTrue
#become_methodsudo
#become_userroot
#become_ask_passFalse (操作)实现免密登录
[roottest1 ~]# vim /etc/ansible/hosts 43 ## db-[99:101]-node.example.com44 45 [web]#可以指定ssh端口非默认的端口46 192.168.67.12:66647 192.168.67.134849 [local]#指定本地连接,无需ssh配置50 192.168.67.11 ansible_connectionlocal[roottest1 ~]# ansible 192.168.67.11 -m ping
192.168.67.11 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong
}#使用密码连接因为只能输入一次所以需要被管理的主机密码一致
[roottest1 ~]# ansible web -m ping -k
SSH password:
192.168.67.13 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong
}
192.168.67.12 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong
}#ansible 默认使用 ssh 连接
#所以管理前要免密登录ask——no 要在安全的内网环境下才用no
[roottest1 ~]# vim /etc/ssh/ssh_config 34 # ConnectTimeout 035 StrictHostKeyChecking no[roottest1 ~]# vim /etc/ansible/ansible.cfg
#开启71行的不验证70 # uncomment this to disable SSH key host checking71 host_key_checking False#添加免密登录的脚本文件
[roottest1 ~]# cd /opt/
[roottest1 opt]# ls
fdisk.sh rh
[roottest1 opt]# rz -E
rz waiting to receive.
[roottest1 opt]# ls
fdisk.sh rh ssh_key.sh
#修改一下登录密码
[roottest1 opt]# vim ssh_key.sh 1 #!/bin/bash2 3 PASS123#bash跑一下
[roottest1 opt]# bash ssh_key.sh
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Y4JojyvznzV66aQotwDtcb8QLweNrATP/cbTqIUjnk roottest1
The keys randomart image is:
---[RSA 2048]----
| |
| |
|. |
| |
|. O O o S |
|. * O . |
|. * #.. |
|.. ^o |
| *Eoo |
----[SHA256]-----
sshpass-1.06-2.el7.x86_64
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: /root/.ssh/id_rsa.pub
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysNumber of key(s) added: 1Now try logging into the machine, with: ssh -o StrictHostKeyCheckingno 192.168.67.11
and check to make sure that only the key(s) you wanted were added.known_hosts 100% 525 1.3MB/s 00:00
known_hosts 100% 525 824.0KB/s 00:00
known_hosts 100% 525 1.1MB/s 00:00 #跑完后即可实现免密登录
[roottest1 opt]# ansible web -m ping
192.168.67.12 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong
}
192.168.67.13 | SUCCESS {ansible_facts: {discovered_interpreter_python: /usr/bin/python}, changed: false, ping: pong
}脚本免密登录脚本文件的代码
#!/bin/bashPASS123
#设置网段最后的地址4-255之间越小扫描越快
END254IPip a s ens33 | awk -F[ /] NR3{print $3}
NET${IP%.*}.rm -f /root/.ssh/id_rsa
[ -e ./SCANIP.log ] rm -f SCANIP.log
for((i3;i$END;i));do
ping -c 1 -w 1 ${NET}$i /dev/null echo ${NET}$i SCANIP.log
done
waitssh-keygen -P -f /root/.ssh/id_rsa
rpm -q sshpass || yum -y install sshpass
sshpass -p $PASS ssh-copy-id -o StrictHostKeyCheckingno $IP AliveIP(cat SCANIP.log)
for n in ${AliveIP[*]};do
sshpass -p $PASS scp -o StrictHostKeyCheckingno -r /root/.ssh root${n}:
done#把.ssh/known_hosts拷贝到所有主机使它们第一次互相访问时不需要输入回车
for n in ${AliveIP[*]};do
scp /root/.ssh/known_hosts ${n}:.ssh/
done 2.5 inventory 主机清单文件
ansible的主要功用在于批量主机操作为了便捷地使用其中的部分主机可以在inventory 主机清单文件中将其分组组织
官方文档How to build your inventory — Ansible Documentation
默认的inventory file 位置在/etc/ansible/hosts
参数详细说明
ansible_ssh_host
#将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
ansible_ssh_port
#ssh端口号.如果不是默认的端口号,通过此变量设置.这种可以使用 ip:端口
192.168.1.100:2222ansible_ssh_user
#默认的 ssh 用户名ansible_ssh_pass
#ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)ansible_sudo_pass
#sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)ansible_sudo_exe (new in version 1.8)
#sudo 命令路径(适用于1.8及以上版本)ansible_connection
#与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 smart,smart 方式会根据是否支持 ControlPersist, 来判断ssh 方式是否可行.ansible_ssh_private_key_file
#ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.ansible_shell_type
#目标系统的shell类型.默认情况下,命令的执行使用 sh 语法,可设置为csh 或 fish.ansible_python_interpreter
#目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是/usr/bin/python,比如 \*BSD, 或者 /usr/bin/python 不是 2.X 版本的Python.之所以不使用 /usr/bin/env 机制,因为这要求远程用户的路径设置正确,且要求 python 可执行程序名不可为 python以外的名字(实际有可能名为python26).与ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....
例子
[web]
192.168.67.12:666
#可以指定ssh端口非默认的端口
192.168.67.13[server]
192.168.67.[11:15]
#指定连续的主机[ky15]
node[1:5]
#指定连续的主机[server:ky15]
server
ky15
#可以嵌套组[local]
192.168.67.11 ansible_connectionlocal
#指定本地连接,无需ssh配置#ansible_connectionssh 需要StrictHostKeyChecking no
192.168.67.14 ansible_connectionssh ansible_ssh_port2222 ansible_ssh_userroot ansible_ssh_password123
10.0.0.6 ansible_connectionssh ansible_ssh_userroot ansible_ssh_password123
还可以指定用户身份 端口号 和密码#执行ansible命令时显示别名,如node1
[websrvs]
node2 ansible_ssh_host192.168.67.12
node3 ansible_ssh_host192.168.67.13[websrvs]
ansible_ssh_password123
例子
[rootnode1 ansible]#vim hosts
[local]
192.168.91.100 ansible_connectionlocal[web]
192.168.91.101
192.168.91.102[accp]
192.168.91.103
192.168.91.105[webserver]
192.168.91.[101:103]
192.168.91.105[web:children]
web
accp[rootnode1 ~]#ansible 192.168.91.105 -m ping
#可以指定 m代表使用模块 ping代表ping模块 命令探测下需要输入 yes 或no
2.6Ansible相关工具
- /usr/bin/ansible 主程序临时命令执行工具
- /usr/bin/ansible-doc 查看配置文档模块功能查看工具,相当于man ansible-doc -l |grep 关键字 具体模块名字
- /usr/bin/ansible-playbook 定制自动化任务编排剧本工具,相当于脚本
- /usr/bin/ansible-pull 远程执行命令的工具
- /usr/bin/ansible-vault 文件加密工具
- /usr/bin/ansible-console 基于Console界面与用户交互的执行工具
- /usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块的官网平台 2.6.1ansible
执行临时任务一次性任务
用法
ansible host-pattern [-m module_name] [-a args]
命令 主机或者清单中的组 -m 指定模块 -a 执行的任务选项
--version #显示版本
-m module #指定模块默认为command使用时可以不写
-v #详细过程 -vv -vvv更详细
--list-hosts #显示主机列表可简写 --list
-C, --check #检查脚本文件play book并不执行nginx-t 检查配置文件是不一样的
-T, --timeoutTIMEOUT #执行命令的超时时间默认10s
-k, --ask-pass #提示输入ssh连接密码默认密钥Key验证密码容易被破解
-u, --userREMOTE_USER #执行远程执行的用户,默认root
-b, --become #代替旧版的sudo 切换
--become-userUSERNAME #指定sudo的runas用户默认为root vim /etc/sudoers 用户权限
-K, --ask-become-pass #提示输入sudo时的口令
-f FORKS, --forks FORKS #指定并发同时执行ansible任务的主机数
(操作)
[roottest1 opt]# ansible localhost -m ping
localhost | SUCCESS {changed: false, ping: pong
}######## 提权操作 ######
[rootcentos7 ~]#grep lisi /etc/sudoers
#先在被控制端目标主机sudo授权
zhangsan ALL(ALL) NOPASSWD: ALL#以zhangsan的用户连接用户,并利用sudo代表mage执行whoami命令
[rootansible ~]#ansible 192.168.91.101 -m shell -a whoami -u zhangsan -k -b --become-userlisi
SSH password: #输入远程主机lisi用户ssh连接密码
10.0.0.8 | CHANGED | rc0 [rootnode1 ~]#ansible 192.168.91.105 -m shell -a hostname /opt/host.txt -u zhangsan -k -b
SSH password:
192.168.91.105 | CHANGED | rc0 命令模块 不行
shell模块 可以使用重定向、特殊命令 ######################## 主机列表 ###########################
##支持通配符
ansible all -m ping
ansible * -m ping
ansible 192.168.67.* -m ping
#ansible srvs -m ping
ansible 192.168.67.12 192.168.67.13 -m ping[roottest1 ~]# vim /etc/ansible/hosts 45 [web]46 192.168.67.1247 192.168.67.1348 49 [kgc]50 192.168.67.1251 192.168.67.1452 53 [local]54 192.168.67.11 ansible_connectionlocal#或关系双方和
[roottest1 ~]# ansible web:kgc --list-hostshosts (3):192.168.67.12192.168.67.13192.168.67.14#并且关系双方都有
[roottest1 ~]# ansible web:kgc --list-hostshosts (1):192.168.67.12#逻辑非前者有后者无在web里但不在kgc里的
[roottest1 ~]# ansible web:!kgc --list-hostshosts (1):192.168.67.13#正则表达式
ansible websrvs:dbsrvs -m ping
ansible ~(k|a).* -m ping
[rootnode1 ~]#ansible ~(k|a).* -m ping################例子###
#除了本机外都重启
ansible kube*:etcd:!10.0.0.101 -a reboot reboot## -f 并行执行-f1,一台一台执行睡眠3秒
[roottest1 opt]# ansible all -a sleep 3 -f1
## -f4 四台被管理机同时执行
[roottest1 opt]# ansible all -a sleep 30 -f4192.168.67.11 | CHANGED | rc0 192.168.67.12 | CHANGED | rc0 192.168.67.13 | CHANGED | rc0 192.168.67.14 | CHANGED | rc0 #睡眠时被管理机会在用户主目录生成一个临时文件可以装一个tree来看也可以直接cd过去
[roottest2 ~]# yum -y install tree
[roottest2 ~]# tree .ansible/
.ansible/
└── tmp└── ansible-tmp-1710239948.82-21869-160369154402830└── AnsiballZ_command.py
#一般ctrlc强行停止睡眠临时文件会被保留多了会卡
##ansible命令执行过程
1. 加载自己的配置文件,默认/etc/ansible/ansible.cfg
#[roottest1 opt]# ansible all -a sleep 3 -f4
#cd /root/.ansible/tmp
2. 加载自己对应的模块文件如command
3. 通过ansible将模块或命令生成对应的临时py文件并将该文件传输至远程服务器的对应执行用户$HOME/.ansible/tmp/ansible-tmp-数字/XXX.PY文件
4. 给文件x执行
5. 执行并返回结果
6. 删除临时py文件退出$HOME 为当前用户的家目录
执行返回结果 绿色执行成功并且不需要做改变的操作 黄色执行成功并且对目标主机做变更 红色执行失败
配置文件中定义了颜色可改但一般不改
vim /etc/ansible/ansible.cfg
[colors]
#highlight white
#verbose blue
#warn bright purple
#error red
#debug dark gray
#deprecate purple
#skip cyan
#unreachable red
#ok green
#changed yellow
#diff_add green
2.6.2 ansible-doc #使用ansible-doc 模块名 可以查看该模块的用法和实例
[roottest1 opt]# ansible-doc pingPING (/usr/lib/python2.7/site-packages/ansible/modules/system/ping.py)
....省略了#加上-s可以简略查看
[roottest1 opt]# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return pong on successping:data: # Data to return for the ping return value. If thisparameter is set tocrash, the modulewill cause anexception.#查看所有支持的模块
[roottest1 opt]# ansible-doc -l
q#统计模块数量
[roottest1 opt]# ansible-doc -l | wc -l
3387[roottest1 opt]# ansible-doc -l | grep ^ping
ping Try to connect to ...
pingdom Pause/unpause Ping...[roottest1 opt]# ansible-doc file
q退出#在vim中使用 :set ai 可以在回车时使光标下移到与当前行相同的位置 2.6.3 ansible-console
#此工具可交互执行命令支持tabansible 2.0新增
提示符格式
执行用户当前操作的主机组 (当前组的主机数量)[f:并发数]$常用子命令
设置并发数 forks n 例如 forks 10
切换组 cd 主机组 例如 cd web
列出当前组主机列表 list
列出所有的内置命令 ?或help[roottest1 opt]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.rootall (4)[f:5]$ cd web
rootweb (2)[f:5]$ forks 10
rootweb (2)[f:10]$ list
192.168.67.12
192.168.67.13#还可以直接使用模块
rootweb (2)[f:10]$ ping
192.168.67.13 | SUCCESS {changed: false, ping: pong
}
192.168.67.12 | SUCCESS {changed: false, ping: pong
}rootweb (2)[f:10]$ ?Documented commands (type help topic):EOF
...省略rootweb (2)[f:10]$
# ctrlc 退出
[roottest1 opt]# #2.6.4 playbook
此工具用于执行编写好的 playbook 任务
范例[roottest1 opt]# vim hello.yml
---
#hello world yml file
- hosts: webremote_user: rootgather_facts: notasks:- name: hello worldcommand: /usr/bin/wall hello worldansible-playbook hello.yml
cat hello.yml#添加超级管理员张三并设置成免密登录慎用
[roottest1 opt]# vim /etc/sudoers91 ## Allow root to run any commands anywhere 92 root ALL(ALL) ALL93 zhangsan ALL(ALL) NOPASSWD: ALLansible-doc user