五、准备工做css
centos7.5 ansible-managenode
centos7.5 masterpython
centos7.5 node1linux
centos7.5 node2web
关闭防火墙 :systemctl stop firewalld && systemctl disable firewalldshell
禁用selinux :setenforce 0 临时关闭vim
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinuxcentos
下载epel扩展源:yum install -y epel-releaseruby
六、安装ansible:服务器
yum install ansible -y
这里默认安装的是ansible2.7
修改配置文件 vim /etc/ansible/hosts 添加主机组
七、ssh免秘钥登陆配置
生成密钥文件:ssh-keygen -t dsa -f ~/.ssh/id_dsa -P
""
与各主机免密登录:ssh-copy-id -i ~/.ssh/id_dsa.pub root@
192.168
.
175.144等
八、测试ansible
ping各主机状态:ansible test -m ping
ansible 经常使用模块介绍
一、Command 模块
做用:在远程主机运行命令
示例:ansible web -a "ls /root" #省略模块不写默认为command模块
注意:该模块不支持一些特殊符号如`"<"', `">"', `"|"',`";"' and `"&"等如需利用这些符号需使用shell模块。
二、shell模块
做用:在远程主机在shell进程下运行命令,支持shell特性,如管道等。
示例:ansible web -m shell -a "echo 123456|passwd --stdin wang"
三、copy模块
做用:在远程主机执行赋值操做文件
示例:
(1) src= dest=
(2) content= dest= #content生成字符到目标文件
其余 owner,group, mode (mode#修改权限)
[root@centos7_1~]# ansible web -m copy -a "src=/etc/fstab dest=/apps/test"
[root@centos7_1~]# ansible web -m copy -a"content='hello world1' dest=/apps/test"
[root@centos7_1 ~]# ansible web -m copy-a "content='hello world1' mode=777 dest=/apps/test1"
四、cron模块
做用:主要是用来对定时任务进行调度
参数:
name= 任务的描述
minute= 分
day= 天
weekday= 周
hour= 时
Month 月
job= 须要执行的命令,必须状态为present
state= 状态
present:建立
absent:删除
五、fetch模块
做用:fetches a file from remotenodes (获取远程节点文件)
六、file模块
做用:sets attributes of files( 设置文件的属性)
用法:
(1) 建立连接文件:*path= src= state=link
(2) 修改属性 : path= owner= mode= grup=
(3) 建立目录:path= state=directory
七、filesystem模块
做用:Makes file system on block device 可以在块设备上建立文件系统(慎用)
八、hostname模块
做用:管理主机名称
九、pip模块
做用:Manages Python librarydependencies. #管理Python库依赖项。
十、yum模块
做用:Manages packages with the`yum' package manager #使用`yum'软件包管理器管理软件包
参数:
name= 程序包名称,能够带版本号
state= 状态
present,latest 目前/最新版本
installed 安装
conf_file:指定yum配置文件
示例:[root@centos7_1 ~]# ansibleweb -m yum -a "name=httpd state=latest"
十一、service 模块
做用:管理服务
参数
name= 服务名称
state= 状态
started 启动
stopped 中止
restarted 重启
enabled= [yes|no] 是否随系统启动
runlevel= 运行级别
示例:ansible web -m service -a"name=httpd state=started enabled=yes runlevel=5" #记得针对Centos7就
不要使用这个模块了。
十二、user 模块
做用:管理用户账号
参数:
name= 用户名
state= 状态
system= [yes|no] 是否为系统用户
shell= 默认shell类型 指定shell
uid= 指定UID
home= 指定家目录
group= 指定属组
groups= 指定附加组
comment= 描述信息
1三、script模块
执行脚本 (执行的是本地的脚本将其复制到远程在执行)
-a “/PATH/TO/SCRIPT_FILE” 会在远程自动给予权限并运行
1四、Template模块
基于模板方式生成一个文件复制到远程主机(template使用Jinjia2格式做为文件模版,进行文档内变量的替换的模块。它的每次使用都会被ansible标记为”changed”状态。)下一章节由详细应用介绍。
backup= 备份
src= 源文件
dest= 目标路径
owner= 属主
group= 主组
mode= 权限
下一章节准备 介绍ansible-playbook的使用 敬请期待!!!