ansible代码发布系统

ansible - 代码发布系统

ssh 秘钥登录

ssh-keygen # 用来生成ssh的密钥对
ssh-copy-id 192.168.107.131 # 复制秘钥到远程主机

 

ansible 命令格式

ansible <host-pattern> [options]
-a MODULE_ARGS, --args=MODULE_ARGS #模块的参数
-C, --check # 检查
-f FORKS, --forks=FORKS #用来做高并发的
--list-hosts #列出主机列表
 -m MODULE_NAME #模块名称
 --syntax-check # 语法检查
 -k 输入密码

 

ansible hosts文件

vim /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 # 一台主机可以在多个组里面

[web]
111.111.111.11[1:5]

 

- 交集  ‘web:&db’
- 并集
  - web,db
  - ‘web:db’
- 差集 ‘web:!db’

 

ansible-doc 查看模块的帮助信息

-j #以json的方式返回ansible的所有模块
-l, --list#列出所有的ansible的模块
-s#以片段式显示ansible的帮助信息

 

命令相关模块

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 'creates=/data pwd' # 因为data不存在,所有才会执行pwd命令
 ansible web -a 'removes=/tmp pwd' #用来判断tmp目录是否存在,存在就执行操作
 ansible web -a 'removes=/data pwd' #因为data不存在,所有才不会执行

 

shell 

由于直接-a不能用 > < | 等符号

 ansible web -m shell -a 'echo "123" | passwd --stdin qwe' # 批量创建密码
 ansible 192.168.107.131 -m shell -a 'bash a.sh' # 执行远程文件方式一
 ansible 192.168.107.131 -m shell -a '/root/a.sh' #执行远程文件方式二,文件必须有执行权限
 ansible 192.168.107.131 -m shell -a '/root/a.py' # 执行远端的Python脚本

 

script

ansible web -m script -a '/root/m.sh' # 执行本地的文件,执行管控机上的文件
ansible web -m script -a 'removes=/root/m.sh /root/m.sh' # 用来判断被管控机上是不是存在文件,如果存在,存在就执行,不存在就不执行
ansible web -m script -a 'creates=/root/a.sh /root/m.sh' #用来判断被管控机上是不是存在文件,如果存在,就不执行

 

copy

backup 备份,以时间戳结尾
dest 目的地址
group 文件的属组
mode 文件的权限 r 4 w 2 x 1
owner 文件的属主
src 源文件
# 通过md5码来判断是否需要复制
ansible db -m copy -a 'src=/root/m.sh dest=/tmp/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=alex' 修改文件的属主
 ansible web -m copy -a 'src=/etc/init.d dest=/tmp/ mode=755 owner=alex' # 复制本地目录到远程主机,如果改变文件的属性,则文件夹内的文件也会被改变
 ansible web -m copy -a 'src=/etc/init.d/ dest=/tmp/ mode=755 owner=alex' # 复制本地目录内的所有文件到远程主机
 ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语\n' dest=/tmp/b.txt" # 直接将文本内容注入到远程主机的文件中

 

file

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是目标地址
ansible db -m file -a 'path=/tmp/f state=absent' #用来删除文件或者文件夹

 

inode 硬盘的地址
id 获取到的是内存的地址
ln -s a.py b.py 创建软连接
ln  a.py c.py 创建硬链接
当 源文件变化时,软连接和硬链接文件都会跟着变化

 

fetch

dest 目的地址
src 源地址
ansible web -m fetch -a 'src=/var/log/cron dest=/tmp' # 下载被控节点的文件,每台机器创建一个文件夹,并保留原来的目录结构

 

软件相关模块

yum

ansible web -m yum -a 'name=wget' # 安装wget
ansible web -m yum -a 'name=python2-pip' # 安装python2-pip
ansible web -m yum -a 'name=wget state=absent' # 卸载软件包
 ansible web -m yum -a 'name="@Development Tools"' # 安装包组

 

rpm 和yum 的区别
rpm:redhat package manager yum可以解决依赖关系
yum grouplist # 查看包组信息
yum groupinstall # 安装包组

 

pip

ansible web -m pip -a 'name=flask' # 安装flask模块

 

service

ansible web -m service -a 'name=nginx state=started' # 启动nginx
ansible web -m service -a 'name=nginx state=stopped' # 关闭nginx

 

ps -ef|grep nginx #查看进程
ss -tnlp # 查看端口信息
systemctl start nginx # centos7
service nginx start  # centos6
systemctl enabled nginx # centos7 开机自启动
chkconfig nginx on # centos6开机自启动

 

计划任务

cron

day  天
disabled 禁用
hour 小时
job 任务
minute 分钟
month 月
name 任务名字
weekday 周

ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile' # 新建一个计划任务
ansible db -m cron -a 'name=touchfile state=absent' # 删除一个计划任务
ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile disabled=yes'  # 禁用计划任务,以#表示禁用

* * * * * job 
分 时 日 月 周 任务
0 */2 *  * *  job  每隔两个小时
0 12,13 * * * job 12点和13点
0 12-17 * * * job 12点到17点
0 12-17/2 * * 1,3,6,0 周1,周3,周6,周7 12点到17点每隔两个小时 
crontab -e # 编辑计划任务
crontab -l # 查看计划任务
crontab -r # 删除计划任务

 

用户相关

user

group 组
groups 附加组
home 家目录
name 用户名
password 密码
remove ?
shell 用户登录后使用的shell
system 创建一个系统用户
uid 用来指定用户的id
state 状态
ansible db -m user -a 'name=qwe uid=4000 home=/opt/qwe groups=root shell=/sbin/nologin' #创建一个用户,并指定用户的id,用户的家目录,用户的附加组,用户的shell
ansible db -m user -a 'name=qwe state=absent' #删除用户但是不删除用户的家目录
ansible db -m user -a 'name=qwe3 state=absent remove=yes' # 删除用户并删除用户的家目录

 

group

gid 组的id
name 组名
system 系统组
state
ansible db -m group -a 'name=asd system=yes' #创建系统组
ansible db -m group -a 'name=asd state=absent' # 删除组

 

ansible 剧本

xxx.yml xxx.yaml

ansible-playbook命令格式

执行顺序: 从上往下
特性:幂等性 不管执行多少遍,结果都是一样的
ansible-playbook [options] playbook.yml [playbook2 ...] 
-C, --check   # 检查,白跑,干跑
-f FORKS, --forks=FORKS #用来做并发
--list-hosts # 列出主机列表
--syntax-check # 语法检查 

 

aaa.yml

简单
- hosts: web tasks: - name: creategroup group: name=qwe10 - name: cretaeuser user: name=asd10

传参

- hosts: web
tasks:
- name: create{{ user }}
user: name={{ user}}

5种传参方式
  1. ansible-playbook -e 'user=zxc10' p2.yml
  2. hosts中添加

[db]

192.168.107.132 user=alexsb11
192.168.107.133 user=alexsb12

 

    3.hosts中添加

[db:vars] #表示组的参数
user=alexsb13

    4. 1.yml中写入

- hosts: web
vars:
- user: asd14
tasks:
- name: create{{ user }}
user: name={{ user}}

    5. 先yum install bc, 再在1.yml中写入

- hosts: db
tasks:
- name: sum
shell: echo 7+8|bc
register: user
- name: createuser
user: name={{user.stdout}}

传参方式的优先级

-e > playbook vars > hosts文件

 

setup

ansible web -m setup

ansible cache -m setup

ansible_all_ipv4_addresses # ipv4的所有地址
ansible_all_ipv6_addresses # ipv6的所有地址
ansible_date_time # 获取到控制节点时间
ansible_default_ipv4 # 默认的ipv4地址
ansible_distribution # 系统
ansible_distribution_major_version # 系统的大版本
ansible_distribution_version # 系统的版本号
ansible_domain #系统所在的域
ansible_env #系统的环境变量
ansible_hostname #系统的主机名
ansible_fqdn #系统的全名
ansible_machine #系统的架构
ansible_memory_mb #系统的内存信息
ansible_os_family # 系统的家族
ansible_pkg_mgr # 系统的包管理工具
ansible_processor_cores #系统的cpu的核数(每颗)
ansible_processor_count #系统cpu的颗数
ansible_processor_vcpus #系统cpu的总个数=cpu的颗数*CPU的核数
ansible_python # 系统上的python
ansible cache -m setup -a 'filter=*processor*' # 用来搜索

 

条件判断

使用情况: 
- 不同的系统 - 不同的版本 - 不同的环境 - 不同的用户 - hosts: db remote_user: root tasks: - name: createfile copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt when: a=="3" - name: cratefile copy: content="小弦切切如私语" dest=/tmp/a.txt when: a=="4"

 

tags

tags和when不能一起使用

- hosts: web
  tasks:
  - name: installnginx
    yum: name=nginx
  - name: copyfile
    copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
    tags: copyfile
  - name: start
    service: name=nginx state=started

 

循环 with_item

- hosts: web
  tasks:
  - name: crateuser
    user: name={{item}}
    with_items:
    - qwe1
    - asd1
    - zxc1

 

嵌套循环

createuser的用户组必须以存在
- hosts: web tasks: - name: crategroup group: name={{item}} with_items: - asd1 - asd2 - zxc3 - name: createuser user: name={{item.name}} group={{item.group}} with_items: - {'name':qwe1,'group':asd1} - {'name':qwe2,'group':asd2} - {'name':qwe3,'group':asd3}

 

template

copy和tamplate的区别

  • copy模块不替代参数

  • template模块替代参数

配置文件内 /etc/redis.conf 写入 
bind {{ ansible_default_ipv4.address }} 

xx.yml 内写入
- hosts: web
  tasks:
  - name: installredis
    yum: name=redis
  - name: copyfile
    template: src=/etc/redis.conf dest=/etc/redis.conf
  - name: start
    service: name=redis state=started

 

handlers

handlers可以用于修改配置文件

notify触发时触发handler, 一个name下无法写两个命令, 所以在一个命令下写tag和notify
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: src=redis.conf dest=/etc/redis.conf tags: copyfile notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted

 

转载于:https://www.cnblogs.com/NachoLau/p/10409802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值