ansible运维自动化之三---ansible-playbook入门及进阶

ansible-playbook入门及进阶
ansible帮助:ansible官方手册
Ansible-console

cd进入相应设置的组,在里面执行任何命令都是针对所有主机,非常便捷
在这里插入图片描述

ansible-console -h
root@web (2)[f:5]$ copy src=/tmp/console.txt dest=/tmp

ansible-galaxy安装删除模块用
在官网找到我们需要的模块直接下载安装即可,默认这些都是在github上的,我们可以写一些遵守ansible的约定通过即可显示。

https://galaxy.ansible.com/explore#/ 
ansible-galaxy install DavidWittman.redis
ansible Yaml语法简介

yaml: yaml ain’t a markup language
yaml的意思其实是:yet another markup language,但为了强调这种语言以数据作为中心,而不是以标记语言为中断,而用反向缩略语重命名
1.首先以“—”(3个减号)开始,且需顶行首写
2.次行开始正常playbook的内容,但笔者一般建议写明该playbook的功能
3.使用#号注释代码
4.缩进必须是统一的,不能空格和tab混用
5.缩进的级别也必须是一致的,同样的缩进代表同样的级别,程序判别配置的级别是通过缩 进结合换行来实现的
6.yaml文件内容和linux系统大小写判断方式保持一致,是区别大小写的,k/v的值均需大小 写敏感
7.k/v的值可同行写也可换行写,同行使用:分隔,换行写需要以-分隔
8.一个完整的代码块功能需最少元素需包括 name:task
9.一个name只能包括一个task

cd /root/files
cp /etc/nginx/nginx.conf  /root/files
cd /etc/ansible/roles
vim nginx.yml
---

- hosts: web  #针对哪些组的主机操作
  remote_user: root
  tasks:
  - name: install Nginx
    yum: name=nginx state=latest
  - name: configure Nginx
    copy: src=/root/files/nginx.conf dest=/etc/nginx/nginx.conf backup=yes
  - name: start nginx
    service: name=nginx state=started
    
ansible-playbook -C nginx.yml #测试语法是否执行通过
ansible-playbook nginx.yml

[root@longma-node13-808 roles]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.

root@all (3)[f:5]$ cd web
root@web (3)[f:5]$ ps -aux |grep nginx

handlers:当我们某个任务集触发到某个代码库,那么这个代码块才会去执行,比如重启
某个服务,不是每次都重启服务,而是当配置文件更改的时候才会去重启服务

---

- hosts: web
  remote_user: root
  tasks:
  - name: install Nginx
    yum: name=nginx state=latest
  - name: configure Nginx
    copy: src=/root/files/nginx.conf dest=/etc/nginx/nginx.conf backup=yes
    notify: restart nginx
#- name: start nginx
#    service: name=nginx state=started

  handlers:
  - name: restart nginx
    service: name=nginx state=restarted


[root@longma-node13-808 roles]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.

root@all (3)[f:5]$ cd web
root@web (3)[f:5]$ ss -ntl |grep 808


Playbook核心元素
  • Hosts: 运行指定任务的目标主机
  • Tasks: 任务列表
  • Varniables: 变量
  • Templates: 模板
  • Handlers: 由特定条件触发的任务,监控资源改变时才会触发改变
  • Roles: Playbook的按固定目录结构组成
示例2:创建用户
---

- hosts: web
  remote_user: root
  tasks:
  - name: added user1
    user: name=user1 system=yes uid=307
  - name: added user2
    user: name=user2 system=yes uid=308

ansible-playbook user.yml

示例3:使用变量
---

- hosts: web
  remote_user: root
  tasks:
  - name: added user1
    user: name=user1 system=yes uid=307
  - name: added user2
    user: name=user2 system=yes uid=308
  - name: added {{ userx }}
    user: name={{ userx }} uid={{ userid }}

ansible-playbook -e userx=user3 -e userid=10080 user.yml

示例4:使用tags
---

- hosts: web
  remote_user: root
  tasks:
  - name: added user1
    tags: autouser
    user: name=user1 system=yes uid=307
  - name: added user2
    tags: autouser
    user: name=user2 system=yes uid=308
  - name: added {{ userx }}
    tags: manueuser
    user: name={{ userx }} uid={{ userid }}



[root@longma-node13-808 roles]# ansible-playbook -t autouser user.yml 
[root@longma-node13-808 roles]# ansible-playbook -t manueuser -e userx=user4 -e userid=10099 user.yml
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值