ansible_模块和剧本ansible_bookplay

ansible_second_day

软件相关模块

yum

  • rpm 和 yum 的区别

    rpm:redhat package manager yum可以解决依赖关系

  • yum 源配置

    ansible web -m copy -a 'src=/etc/yum.repos.d/epel.repo dest=/etc/yum.repos.d/epel'
    [epel]
    name=Extra Packages for Enterprise Linux 7 -
    $basearch # 名字
    baseurl=http://mirrors.aliyun.com/epel/7/$basearch
    # rpm源的地址,可以写http,https,ftp,Samba,file;
    failocermethod=priority
    enabled=1 # 是否开启,1代表开启, 0代表关闭
    gpgcheck=0 # 是否校验签名,1,代表校验码,0表示不校验
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  • yum 安装包组

    yum grouplist # 查看包组信息
    yum groupinstall # 安装包组
    
    disablerepo # 禁用源
    enablerepo # 启动源
    name # 包名
    state install (`present' or `installed',`latest'),
    or remove(`absent` or 'removed')

    实战代码:

    ansible web -m yum -a 'name=wget' # 安装wget
    
    ansible web -m yum -a 'name=python2-zip' 
    # a安装python2 -zip 
    pip 有多个版本,不能用yum直接下 应该像这样
    下载以后就有pip可以使用了
    
    
    ansible web -m yum -a 'name=wget state=abesent'
    # 卸载安装的软件包
    
    ansible web -m yum -a 'name="@Development Tools"'
    # 安装包组pip

pip

pip install 安装包

pip freeze > a.txt 将python的环境打包到文件中

pip install -r a.txt 安装文件中的包

pip list 查看所有已经安装成功的包
ansible web -m pip -a 'name=flask' # 安装flask模块

service

ps -ef |grep nginx # 查看进程
ss -tunpl # 查看端口信息
systemctl start nginx  # centos7
service nginx start # centos6
systemctl enabled nginx # centos7 开机启动
chkconfig nginx on # centos6 开机启动
ansible web -m service -a 'namme=nginx state=started'
# 启动nginx

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

corn

计划任务

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

参数

day  天
disabled 禁言 落实到代码上就是在任务前面加# 注释掉
hour 小时 
job 任务
minute 分钟
mouth 月
name 任务名称
weekday 周
ansible db - m cron -a 'minute=39 job="touch /tmp/zhu.txt" name=fouchfile' # 新建一个计划任务

ansible db - m cron -a 'name=touchfile state=abesnt'
# 删除一个计划任务

ansible db -m cron -a 'minute=26 job="mkdir zzz" 
name = touchfile disabled=yes'
# 禁用计划任务,以#表示禁用

user

用户 :
    管理员 root 0
    普通用户 
        系统用户 不能登录 1-999 centos7 1-499 centos6
        登录用户 可以登录 1000-65535 centos7
                        500-65535 centos6

用户组:
    系统管理员组 root 0
    系统用户组 1-999 centos7 1-499 centos6
    登录用户组 1000-65535 centos7 500-65535 centos6
    
-d  指定用户的家目录
-g  指定用户组
-G  执行用户的附加组
-s  指定登陆后使用的shell
-r  穿件一个系统组

useradd -r max 创建系统用户,从999倒叙计数

useradd -s /sbin/nologin max 
# 创建的是普通用户,计数从 1000开始升序

useradd -d /opt/max1 max2 
# 创建用户时并指定用户的家目录

useradd -u 8888 max 
# 创建用户并指定用户的uid

userdel max 
# 删除用户max 只在系统中删除了用户 用户家目录还在

userdel -r max 
# 删除用户并且删除用户家目录


groupadd earl 
# 创建用户组

groupdel earl
# 删除用户组

group # 组
groups # 附加组
home # 家目录
name # 用户名
password # 密码
remove  # 删除
shell # 登录后使用的shell
system # 创建一个系统用户
uid # 用来指定用户的id
state # 状态


ansible db -m user -a'name=max uid=3333 home=/opt/max groups=root shell=/sbin/nologin'
# 创建一个用户,指定用户的id,用户的家目录,用户的附加组,用户的shell

ansible db -m user -a 'name=max state=absent'
# 删除用户但是留下用户的家目录

ansible db -m user -a 'name=maex state=absent remove=yes'
# 删除用户同时并且删除用户的家目录

group

gid # 组id
name # 组名
system # 系统组
state # 状态

ansible db -m group -a 'name=max system=yes'
# 创建系统组

ansible db -m group -a 'name=max state=absent'
# 删除组
web
ansible web -m group -a 'name=max8'
# 创建一个用户组 max8

ansible web -m user -a 'name=eral10'
# 创建一个用户 eral8

ansible web -m copy -a 'src=/etc/ftp dest=/tmp/f'
# 把/etc/ftp 文件复制到远程主机上 /tmp/f 下

ansible web -m yum -a 'name=nginx'
# 安装nginx

ansible web -m service -a 'name=nginx enabled=yes'
# 设置nginx 开机启动

ansible剧本

yaml

是一个编程语言
xml 是用来写配置文件的一个语言
ini
yaml
字典 : key:value
列表 : [] -
后缀名 : yaml, yml

ansible-playbook 命令格式

执行顺序,从上往下

特性: 幂等性,执行多少次,结果都一样

ansible-playbook [options] playbook.yml [playbook2...]

-C, --check 
# 检查,演示一遍,但不是真的运行,白干活

-f FORKS, --forks=FORKS 
# 用来做并发

--list-hosts 
# 列出主机列表

--syntax-check
# 语法检测

简单使用

- hosts: web
  tasks:
  - name: creategroup
    group: name=max9
  - name: createuser
    user: name=eral9
    

传参

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

first

ansible-play -e 'user=max888' p1.yml

second

[db]
192.168.111.129 user=max777
195.168.107.133 user=max888

third

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

fourth

- host: db
 vars:
 - user: max666
 tasks:
 - name: create{{ user }}
   user: name= {{ user }}

fifth

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

传递参数的优先级

-e >>> playbook vars > hosts文件

猜你喜欢

转载自www.cnblogs.com/zzy7372/p/10408159.html