5.实现ansible企业级用法playbook

1.ansible系列命令

1.1 ansible-galaxy

连接https://galaxy.ansible.com下载相应的roles(角色)

  • 列出所有已安装的galaxy
ansible-galaxy list
  • 安装galaxy
ansible-galaxy install geerlingguy.redis
  • 删除galaxy
ansible-galaxy remove geerlingguy.redis
1.1.2安装Nginx角色
[root@ansible ~]# ansible-galaxy install geerlingguy.nginx

#下载到隐藏目录.asnible目录

[root@ansible ~]# tree .ansible/roles/
.ansible/roles/
└── geerlingguy.nginx
    ├── defaults
    │   └── main.yml
    ├── handlers
    │   └── main.yml
    ├── LICENSE
    ├── meta
    │   └── main.yml
    ├── molecule
    │   └── default
    │       ├── converge.yml
    │       └── molecule.yml
    ├── README.md
    ├── tasks
    │   ├── main.yml
    │   ├── setup-Archlinux.yml
    │   ├── setup-Debian.yml
    │   ├── setup-FreeBSD.yml
    │   ├── setup-OpenBSD.yml
    │   ├── setup-RedHat.yml
    │   ├── setup-Ubuntu.yml
    │   └── vhosts.yml
    ├── templates
    │   ├── nginx.conf.j2
    │   ├── nginx.repo.j2
    │   └── vhost.j2
    └── vars
        ├── Archlinux.yml
        ├── Debian.yml
        ├── FreeBSD.yml
        ├── OpenBSD.yml
        └── RedHat.yml

角色就是多个playbook

[root@ansible tasks]# pwd
/root/.ansible/roles/geerlingguy.nginx/tasks
[root@ansible tasks]# cat main.yml
---
# Variable setup.
- name: Include OS-specific variables.
  include_vars: "{{ ansible_os_family }}.yml"

- name: Define nginx_user.
  set_fact:
    nginx_user: "{{ __nginx_user }}"
  when: nginx_user is not defined

# Setup/install tasks.
- include_tasks: setup-RedHat.yml
  when: ansible_os_family == 'RedHat'

- include_tasks: setup-Ubuntu.yml
  when: ansible_distribution == 'Ubuntu'

- include_tasks: setup-Debian.yml
  when: ansible_os_family == 'Debian'

- include_tasks: setup-FreeBSD.yml
  when: ansible_os_family == 'FreeBSD'

- include_tasks: setup-OpenBSD.yml
  when: ansible_os_family == 'OpenBSD'

- include_tasks: setup-Archlinux.yml
  when: ansible_os_family == 'Archlinux'

# Vhost configuration.
- import_tasks: vhosts.yml

# Nginx setup.
- name: Copy nginx configuration in place.
  template:
    src: "{{ nginx_conf_template }}"
    dest: "{{ nginx_conf_file_path }}"
    owner: root
    group: "{{ root_group }}"
    mode: 0644
  notify:
    - reload nginx

- name: Ensure nginx service is running as configured.
  service:
    name: nginx
    state: "{{ nginx_service_state }}"
    enabled: "{{ nginx_service_enabled }}"
1.1.3查看角色
[root@ansible tasks]# ansible-galaxy list geerlingguy.nginx
# /root/.ansible/roles
- geerlingguy.nginx, 2.8.0
1.1.4复制角色
[root@ansible roles]# cp geerlingguy.nginx/ wang.nginx -rp
[root@ansible roles]# ansible-galaxy list
# /root/.ansible/roles
- geerlingguy.nginx, 2.8.0
- wang.nginx, 2.8.0
# /usr/share/ansible/roles
# /etc/ansible/roles
1.1.5删除角色
[root@ansible ~]# ansible-galaxy remove geerlingguy.nginx
#验证 
#说白了 删除目录就可以
[root@ansible ~]# cd .ansible/roles/
[root@ansible roles]# ls
wang.nginx

1.2ansible-pull

推送至远程,效率无线提升,对运维要求较高

1.3ansible-playbook

1.用法
useage: ansible-playbook [optinon] playbook.yml [playbook2...]
#可以跟很多playbook
.yaml  后缀
2.格式
[root@ansible ansible]# cat hello.yaml
---   #表示开头
- hosts: webserver   #操作的哪些主机
  remote_user: root   #默认使用什么用户

  tasks:       #任务
    - name: hello   #任务名称
      command: hostname   #执行操作

1.3ansible-vault

== 功能︰管理加密解密yml文件==


ansible-vault[create|decrypt|edit|encrypt|rekey|view]
ansible-vault encrypt hello.yml加密
ansible-vault decrypt hello.yml解密
ansible-vault view hello.yml查看
ansible-vault edit hello.yml编辑加密文件
ansible-vault rekey hello.yml修改口令
ansible-vault create new.yml创建新文件
1.encrypt :加密
[root@ansible ansible]# ansible-vault encrypt hello.yaml  #加密
New Vault password:
Confirm New Vault password:
Encryption successful
[root@ansible ansible]# cat hello.yaml
$ANSIBLE_VAULT;1.1;AES256
62303934313466613534656430353834353434636532643439633634636434346462366537626365
3037313238336431643961323631333334663035653762650a663738316336333263383465343734
36323363636166316635396433646563383162653831323037646661313336303439303235313766
3433613735313237340a616264666464616334393033343138323539373764303734366538316439
31623730366338636438366363396538313966643539363737386264366639336238396635633130
39643264623730303435666431303232633062643439356238663333323333303766336665363661
38303535306565373131626163626466333439386265343837643034373461666534656364363730
36616665343862326537666333623266313538366262323430333832656465636531343862643738
6632

#加密后playbook不能直接使用
[root@ansible ansible]# ansible-playbook hello.yaml
ERROR! Attempting to decrypt but no vault secrets found
2.decrypt:解密
[root@ansible ansible]# ansible-vault decrypt hello.yaml
Vault password:
Decryption successful
[root@ansible ansible]# cat hello.yaml
---
- hosts: webserver
  remote_user: root

  tasks:
    - name: hello
      command: hostname
3.view :查看

使用口令进行查看加密的yml

[root@ansible ansible]# ansible-vault view hello.yaml
Vault password:
---
- hosts: webserver
  remote_user: root

  tasks:
    - name: hello
      command: hostname
4.edit :编辑
[root@ansible ansible]# ansible-vault edit hello.yaml
Vault password:
5.rekey: 修改口令
[root@ansible ansible]# ansible-vault rekey hello.yaml
Vault password:
New Vault password:
Confirm New Vault password:
Rekey successful
6.create :创建
[root@ansible ansible]# ansible-vault create hello2.yml
New Vault password:
Confirm New Vault password:

1.4 ansible-console

Ansible-console : 2.0+新增,可交互执行命令,支持tab
root@test(2)[f:10]$
执行用户@当前操作的主机组(当前组的主机数量)[f:并发数]$

设置并发数:forks n例如: forks 10
切换组:cd主机组例如: cd web
列出当前组主机列表:list
列出所有的内置命令∶?或help

用法示例︰
root@all(2)[f:5]$ list    #查看主机
root@all (2)[f:5]$ cd appsrvs    #进入主机清单
root@appsrvs (2)[f:5]$ list     #查看主机
root@appsrvs (2)[f:5]$ yum name=httpd state=present   #执行命令
root@appsrvs (2)[f:5]$ service name=httpd state=started
实践
[root@ansible ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.

root@all (3)[f:5]$
#root  用户
# all  所有人
(3)表示几个主机
[f:5]  并发5执行5个
1.进入主机目录
[root@ansible ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.

root@all (3)[f:5]$ cd webserver
2.更改并发数
root@webserver (1)[f:5]$ forks 10
3.查看模板
root@webserver (1)[f:10]$ ?
4.执行命令:查看主机名
root@webserver (1)[f:10]$ command hostname
10.0.0.48 | CHANGED | rc=0 >>
node1.ylm.com
5.更改主机名
root@webserver (1)[f:10]$ cd 10.0.0.48
root@10.0.0.48 (1)[f:10]$ hostname name=48
10.0.0.48 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "",
        "ansible_fqdn": "48",
        "ansible_hostname": "48",
        "ansible_nodename": "48"
    },
    "changed": true,
    "name": "48"
}
root@10.0.0.48 (1)[f:1

playbook

1.简介
  • playbook是由一个或多个“play*组成的列表
  • play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task
    定义好的角色。从根本上来讲,所谓task无非是调用ansible的一个module。
    将多个play组织在一个playbook中,即可以让它们联同起来按事先编排的机制
    同唱一台夭戏
  • Playbook采用YAML语言编写
2.工作流程

在这里插入图片描述
在playbook里面写好多 play 用户用ansible命令去调用执行的play,每个play里面有自己的任务,任务和任务之间有先后顺序,本质上调底层模块(开发 ,核心,插件),也得看主机清单 ,也通过ssh连接到远程主机上。

3.YAML介绍
  • YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言,包括:XML.
    C语言、Python、Perl以及电子邮件格式RFC2822等。ClarkEvans在2001年在首次发表了这种
    语言,另外Ingy dot Net与Oren Ben-Kikit也是这语言的共同设计者

  • YAML Ain’t Markup Language(不是标记语言),即YAML不是XML。不过,在开发的这种语言时,YAML的意
    思其实是:“Yet Another Markup Language”(仍是一种标记语言)

  • 特性
    1. YAML的可读性好
    2. YAML和脚本语言的交互性好
    3. YAML使用实现语言的数据类型
    4. YAML有一个一致的信息模型
    5. YAML易于实现
    6. YAML可以基于流来处理
    7. YAML表达能力强,扩展性好

  • 内容及规范参见http://www.yaml.org

  • 在单一挡案中,可用连续三个连字号(——)区分多个档案。另外,还有选择性的连续三个点号
    (…)用来表示档案结尾

  • 次行开始正常写Playbook的内容,一般建议写明该Playbook的功能

  • 使用#号注释代码

  • 缩进必须是统一的,不能空格和tab混用

  • 缩进的级别也必须是一致的,同样的缩进代表同样的级别,程序判别配置的级别是通过缩进结
    合换行来实现的

  • YAML文件内容和Linux系统大小写判断方式保持一致,是区别大小写的,k/v的值均需大小写敏

  • k/v的值可同行写也可换行写。同行使用:分隔

  • v可是个字符串,也可是另一个列表

  • 一个完整的代码块功能需最少元素需包括name: task

  • 一个name只能包括一个task

  • YAML文件扩展名通常为yml或yaml

3.1YAML语法简介
  • List:列表,其所有元素均使用“”打头
  • 示例∶
  #A list of tasty fruits
   -Apple
   -orange
   -strawberry
   -Mango
  • Dictionary :字典,通常由多个key与value构成(键值对)
  • 示例:
#An employee record
name: Example Developer
job: Developer
skill:Elite

也可以将key:value放置于{f}中进行表示,用,分隔多个key:value

  • 示例:
--
# An employee record
{name:Example Developer,job: Developer,skill:Elite}
3.2YAML语法范例
  • YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构
    (Structure )通过空格来展示,序列(Sequence)里的项用-来代表,Map里的键值对用"∵"分隔
  • 示例
name: John Smith
age;41
gender: Male
spouse:
  name: lane Smith  
  age: 37
  gender: Female
children:
  - name: Jimmy Smith
    age: 17
    gender:Malc
  - name: Jenny Smith
    age 13
    gender: Female
4.playbook核心元素

ansible中文权威指南

  • Hosts执行的远程主机列表
  • Tasks任务集
  • Varniables内置变量或自定义变量在playbook中调用
  • Templates模板,可替换模板文件中的变量并实现一些简单逻辑的文件
  • Handlers和notity结合使用,由特定条件触发的操作,满足条件方才执行,否
    则不执行
  • tags标签指定某条任务执行,用于选择运行playbook中的部分代码。ansible
    具有幂等性,因此会自动跳过没有变化的部分,即便如此,有些代码为测试其
    确实没有发生变化的时间依然会非常地长。此时,如果确信其没有变化,就可
    以通过tags跳过此些代码片断
ansible-playbook -t tagsname useradd.yml
5.playbook基础组件
  • Hosts;:

    • playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户
      身份执行任务。hosts用于指定要执行指定住务的主机,须事宪定义在主机清
      单中
    • 可以是如下形式∶
      one.example.com
      one.example.com:two.example.com
      192.168.1.50
      192.168.1.*
  • Websrvs:dbsrvs两个组的并集

  • Websrvs:&dbsrvs两个组的交集

  • webservers:!phoenix在websrvs组,但不在dbsrvs组
    示例: - hosts: websrvs : dbsrvs

  • remote_user:可用于Host和task中。也可以通过指定其通过sudo的方式在远
    程主机上执行任务,其可用于play全局或某任务;此外,甚至可以在sudo时使
    用sudo_user指定sudo时切换的用户

- hosts: websrvs
  remote_user: root
  tasks:
   - name: test connection
     ping:
     remote_user: magedu
     sudo: yes     #默认sudo为root
     sudo_user:wang       #sudo为wang
  • task列表和actioh

  • play的主体部分是tasklist。task list中的各任务按次序逐个在hosts中指定的所
    有主机上执行,即在所有主机上完成第一个任务后再开始第二个。在运行自
    下而下某playbook时,如果中途发生错误,所有已执行任务都将回滚,因此,
    在更正playbook后重新执行一次即可

  • task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模
    块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致

  • 每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可
    能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出

  • tasks:任务列表
    格式∶(1)action: module arguments
    (2) module: arguments # 建议使用
    - 注意: shell和command模块后面跟命令,而非key=value

  • 某任务的状态在运行后为changed时,可通过“notify”通知给相应的
    handlers

  • 任务可以通过"tags“打标签,而后可在ansible-playbook命令上使用-t指定进
    行调用
    示例:

  tasks:
        - name: disable selinux
          command: /sbin/setenforce o
  • 如果命令或脚本的退出码不为零,可以使用如下方式替代
tasks:
- name: run this command and ignore the result
  shell: /usr/bin/somecommand || /bin/true
  • 或者使用ignore_errors来忽略错误信息∶
tasks:
 - name: run this command and ignore the result
   shell: /usr/bin/somecommand
   ignore_errors: True
5.1新建
[root@ansible ansible]# cat file.yml
---
- hosts: webserver
  remote_user: root

  tasks:
    - name: create new file
      file: name=/data/newfile state=touch
    - name: create new user
      user: name=test2 system=yes shell=/sbin/nologin
    - name: install package
      yum: name=httpd
    - name: copy html
      copy: src=/var/www/html/index.html dest=/var/www/html
    - name: start service
      service: name=httpd state=started enabled=yes
[root@ansible ansible]#

ps:新建用户使用user命令之前 一定要查看 用户是否存在

[root@ansible ~]# ansible webserver -a 'getent passwd'
5.2 -C 检查语法
[root@ansible ansible]# ansible-playbook -C file.yml
5.3运行playbook
1.运行playbook的方式
ansible-playbook <filename.yml> ... [options]
2.常见选项
     --check只检测可能会发生的改变,但不真正执行操作
     --list-hosts列出运行任务的主机
     --limit主机列表只针对主机列表中的主机执行
     -v 显示过程 -vv -v 更详细
  • 示例
     ansible-playbook file.yml --check只检测
     ansible-playbook file.yml
     ansible-playbook file.yml --limit websrvs
[root@ansible ansible]# ansible all -a 'getent passwd test2' --limit 10.0.0.48   只看48的
10.0.0.48 | CHANGED | rc=0 >>
test2:x:807:807::/home/test2:/sbin/nologin

3.查看文件针对那个主机执行的
[root@ansible ansible]# ansible-playbook file.yml --list-hosts

playbook: file.yml

  play #1 (webserver): webserver        TAGS: []
    pattern: [u'webserver']
    hosts (1):
      10.0.0.48
4.查看几个任务
[root@ansible ansible]# ansible-playbook file.yml --list-tasks

playbook: file.yml

  play #1 (webserver): webserver        TAGS: []
    tasks:
      create new file   TAGS: []
      create new user   TAGS: []
      install package   TAGS: []
      copy html TAGS: []
      start service     TAGS: []
5.playbook VS shellscript
  • SHELL脚本

#!/bin/bash
#安装Apache
yum install --quiet -y httpd
#复制配置文件
cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
cp/tmp/vhosts.conf /etc/httpd/conf.d/
#启动Apache,并设置开机启动
service httpd start
chkconfig httpd on
  • Playbook定义
hosts: all
tasks:
  - name:"安装Apache"
    yum: name=httpd
  - name:"复制配置文件”
    copy: src=ftmp/httpd.conf dest=/etc/httpd/conf/
    copy: src=/tmp/vhosts.conf dest=fetc/httpd/conf/    #不可用 一个name 使用一个copy 
  - name:"启动Apache ,并设置开机启动”
    service: name=httpd state=started enabled=yes

相对路径☞的是playbook 的目录下

[root@ansible ansible]# cat file.yml
---
- hosts: webserver
  remote_user: root

  tasks:
    - name: create new file
      file: name=/data/newfile state=touch
    - name: create new user
      user: name=test2 system=yes shell=/sbin/nologin
    - name: install package
      yum: name=httpd
    - name: copy html
      copy: src=file/index.html dest=/var/www/html/
    - name: copy test.html
      copy: src=file/test.html dest=/var/www/html/
    - name: start service
      service: name=httpd state=started enabled=yes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云原生解决方案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值