代码发布系统node
准备一个虚拟机后克隆出另外三个,注意,克隆前要将虚拟机关机python
puppet ansible slatstackweb
第一步:下载wget
yum install wget
第二部:下载epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
第三部:安装ansible
yum install -y ansible
注:ssh 的认证方式shell
密码链接json
秘钥链接bash
ansible使用秘钥登陆并发
ssh-keygen # 用来生成ssh的密钥对
ssh-copy-id 192.168.177.132 # 复制秘钥到远程主机
ssh-copy-id 192.168.177.133 # 复制秘钥到远程主机
ssh-copy-id 192.168.177.134 # 复制秘钥到远程主机
注:135做为主节点,后三个做为被控者节点ssh
结果:在192.168.177.135主机中,想要链接另外三个被控节点,使用命令:ssh 192.168.177.132(ssh ip)便可高并发
ansible -h
-a MODULE_ARGS, --args=MODULE_ARGS #模块的参数
-C, --check # 检查
-f FORKS, --forks=FORKS #用来作高并发的
--list-hosts #列出主机列表
-m MODULE_NAME #模块名称
--syntax-check # 语法检查
-k 输入密码
rpm -ql ansible
/etc/ansible
/etc/ansible/ansible.cfg #配置文件
/etc/ansible/hosts
/etc/ansible/roles
ansible 192.168.177.132 -m ping
若是ping不通,在hosts文件中添加相应IP工具
vi /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character #以警号开头的表明注释
# - Blank lines are ignored #空白行能够被忽略
# - Groups of hosts are delimited by [header] elements #主机组须要下载[组名]下
# - You can enter hostnames or ip addresses #能够写主机名或者IP地址
# - A hostname/ip can be a member of multiple groups #一台主机能够在多个组中
192.168.177.132
192.168.177.133
192.168.177.134
根据注释:You can enter hostnames or ip addresses,能够在hosts文件中加入主机命
或IP地址,以此来ping通相应IP
ansible all -m ping
ansible 192.168.177.132,192.168.177.132 -m ping
在配置文件中
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
[web]
192.168.177.[132:133]
[db]
192.168.177.133
192.168.177.134
[cache]
192.168.177.134
ansible web,db -m ping #查看web,db两个组的机器是否在线
ansible 'web:&db' -m ping
ansible 'web:db' -m ping #方式一
ansible web,db -m ping #方式二
ansible 'web:!db' -m ping
注:系统自带的ping走的是ICMP协议
ansible-doc -h
-j, --json **For internal testing only** Dump json metadata for #以json的格式返回全部的ansible模块 ansible-doc -j
-l, --list List available plugins #以列表的格式返回全部的ansible模块 ansible-doc -l
-s, --snippet Show playbook snippet for specified plugin(s) #以片断的形式显示ansible的帮助信息 ansible-doc -s 模块
查看详细信息
ansible-doc command
ansible-doc shell
ansible web -m command -a 'ls /' #(默认就是command,command能够省略)
ansible web -a 'ls /' #可简写为这
ansible web -a 'pwd' #
ansible web -a 'chdir=/tmp pwd' #切换目录执行命令,经常使用来编译安装,
ansible web -a 'creates=/tmp pwd' #若是目录已存在,就不执行命令,用来判断tmp目录是否存在
ansible web -a 'removes=/data pwd' #目录已存在,就执行命令
ansible web -a 'useradd shy'
ansible web -a 'tail -l /etc/passwd' #方式一
ansible web -a 'tail -l /etc/shadow' #方式二
id shy #方式三
passwd shy
echo '123' | passwd --stdin shy #用这种方式设置密码,远程登陆时,不用重复确认密码
ansible web -a 'echo '123' | passwd --stdin shy ' #管道符不支持
ansible不支持 < > ; &|这些符号,这时候就永远到了shell模块,shell模块支持这些符号
ansible web -m shell -a 'echo '123' | passwd --stdin shy' #此时可使用各类特殊符号
ansible 192.168.177.132 -m shell -a 'bash /opt/a.sh' #方式一
ansible 192.168.177.132 -m shell -a '/opt/a.sh' #方式二
注:方式二文件可能没有可执行权限,须要:chmod +x a.sh
ansible 192.168.177.132 -m shell -a '/opt/b.py'
ansible web -m script -a '/opt/m.sh'
ansible web -m script -a 'removes=/opt/m.sh /opt/m.sh'
ansible web -m script -a 'creates=/opt/m.sh /opt/m.sh'
backup 备份,以时间戳结尾 dest 目的地址 group 文件的属组 mode 文件的权限 r 4 w 2 x 1 owner 文件的属主 src 源文件(加/是保存目录中的文件,不加/是保存目录) content 直接把文字输入到远程的文件中 # 经过md5码来判断是否须要复制
ansible 192.168.177.132 -m copy -a 'src=/opt/a.sh dest=/root/a.sh' #复制获得的权限信息跟原来比少了一个可执行的权限
ansible db -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755' #修改文件的权限
ansible web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755 owner=shy' 修改文件的属主
ansible web -m copy -a 'src=/etc/init.d/ dest=/tmp/ mode=755 owner=hsy' # 复制本地目录内的全部文件到远程主机,若是改变文件夹的属性,则文件夹内文件的属性也会被改变
ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语\n' dest=/tmp/b.txt" # 直接将文本内容注入到远程主机的文件中
inode 硬盘的地址 id 获取到的是内存的地址 ln -s a.py b.py 建立软链接 #相似于快捷方式 ln a.py c.py 建立硬连接 #两个文件的inode是同样的 cp a.py d.py #复制 #当源文件变化时,软链接和硬连接文件都会跟着变化,复制的文件不会变化
ansible db -m file -a 'path=/lzmly2 state=directory' #在远程机器上建立文件夹
ansible db -m file -a 'path=/root/q.txt state=touch' #用来在远程机器上建立文件
ansible db -m file -a 'path=/tmp/f src=/etc/fstab state=link' #建立软链接src是源地址,path是目标地址,state=hard是硬连接
ansible db -m file -a 'path=/tmp/f state=absent' #用来删除文件或者文件夹
下载被控节点的文件
dest 目的地址
src 源地址 被管控机上的地址
ansible web -m fetch -a 'src=/var/log/cron dest=/tmp' # 下载被控节点的文件,每台机器建立一个文件夹(以IP命名),并保留原来的目录结构