一、playbook 的简单使用

1、创建文件实例

(1)编辑配置文件

[root@tiejiangSRC1 ~]# cd /etc/ansible/
[root@tiejiangSRC1 ansible]# vim test.yml //固定后缀为yml,一定要注意空格
---
- hosts: testhost
user: root
tasks:
- name: playbook_test
shell: touch /tmp/playbook.txt

注意:

  • hosts参数指定了对哪些主机进行参作;

  • user参数指定了使用什么用户登录远程主机操作;

  • tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。

(2)执行创建playbook文件文件

[root@tiejiangSRC1 ansible]# ansible-playbook test.yml
PLAY [testhost] **************************************************************** TASK [setup] *******************************************************************
ok: [192.168.2.71]
ok: [192.168.2.73]
ok: [192.168.2.72] TASK [playbook_test] ***********************************************************
changed: [192.168.2.71]
changed: [192.168.2.73]
changed: [192.168.2.72] PLAY RECAP *********************************************************************
192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0
192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0
192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0

(3)现在来查看是否批量创建成功playbook.txt文件。

[root@tiejiangSRC1 ~]# ansible testhost -m command -a 'ls -l /tmp/playbook.txt'
192.168.2.73 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 33 4月 19 13:41 /tmp/playbook.txt 192.168.2.71 | SUCCESS | rc=0 >>
-rw-r--r--. 1 root root 33 4月 19 13:41 /tmp/playbook.txt 192.168.2.72 | SUCCESS | rc=0 >>
-rw-r--r--. 1 root root 33 4月 19 13:41 /tmp/playbook.txt

(4)给创建的playbook批量导入内容,并查看导入的结果

[root@tiejiangSRC1 ansible]# vim test.yml
---
- hosts: testhost
user: root
tasks:
- name: 铁匠运维网博客
shell: echo "www.tiejiang.org" >> /tmp/playbook.txt
[root@tiejiangSRC1 ansible]# ansible-playbook test.yml
PLAY [testhost] **************************************************************** TASK [setup] *******************************************************************
ok: [192.168.2.71]
ok: [192.168.2.72]
ok: [192.168.2.73] TASK [铁匠运维网博客] *************************************************************
changed: [192.168.2.73]
changed: [192.168.2.71]
changed: [192.168.2.72] PLAY RECAP *********************************************************************
192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0
192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0
192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0 [root@tiejiangSRC1 ansible]# ansible testhost -m command -a 'cat /tmp/playbook.txt'
192.168.2.73 | SUCCESS | rc=0 >>
www.tiejiang.org 192.168.2.71 | SUCCESS | rc=0 >>
www.tiejiang.org 192.168.2.72 | SUCCESS | rc=0 >>
www.tiejiang.org

2、创建用户实例

(1)编辑配置文件

[root@tiejiangSRC1 yml]# vim create_user.yml
---
- name: create_user
hosts: testhost
user: root
gather_facts: false
vars:
- user: "tiejiang"
tasks:
- name: create user
user: name="{{ user }}"
  • name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;

  • gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;

  • vars参数指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;

  • user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。

(2)执行配置文件

[root@tiejiangSRC1 ansible]# ansible-playbook create_user.yml 

PLAY [create_user] *************************************************************

TASK [create user] *************************************************************
changed: [192.168.2.73]
changed: [192.168.2.71]
changed: [192.168.2.72] PLAY RECAP *********************************************************************
192.168.2.71 : ok=1 changed=1 unreachable=0 failed=0
192.168.2.72 : ok=1 changed=1 unreachable=0 failed=0
192.168.2.73 : ok=1 changed=1 unreachable=0 failed=0

(3)查看远程机器的passwd文件,是否创建出来了用户

[root@tiejiangSRC1 ansible]# ansible testhost -m command -a 'grep tiejiang /etc/passwd'
192.168.2.73 | SUCCESS | rc=0 >>
tiejiang:x:502:502::/home/tiejiang:/bin/bash 192.168.2.72 | SUCCESS | rc=0 >>
tiejiang:x:501:501::/home/tiejiang:/bin/bash 192.168.2.71 | SUCCESS | rc=0 >>
tiejiang:x:502:502::/home/tiejiang:/bin/bash

二、playbook循环

实例:修改/tmp目录下的1.txthe 2.txt文件属性

(1)去新建实验文件

[root@tiejiangSRC1 yml]# cat touch1and2.yml
---
- hosts: testhost
user: root
tasks:
- name: 创建实验文件
shell: touch /tmp/{1.txt,2.txt} [root@tiejiangSRC1 yml]# ansible-playbook touch1and2.yml PLAY [testhost] **************************************************************** TASK [setup] *******************************************************************
ok: [192.168.2.73]
ok: [192.168.2.71]
ok: [192.168.2.72] TASK [创建实验文件] ******************************************************************
changed: [192.168.2.73]
[WARNING]: Consider using file module with state=touch rather than running touch changed: [192.168.2.71]
changed: [192.168.2.72] PLAY RECAP *********************************************************************
192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0
192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0
192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0

(2)编辑配置文件

[root@tiejiangSRC1 yml]# cat loop.yml
---
- hosts: testhost
user: root
tasks:
- name: change mode for files
file: path=/tmp/{{ item }} mode=600 owner=root group=root
with_items:
- 1.txt
- 2.txt

(3)执行配置文件

[root@tiejiangSRC1 yml]# ansible-playbook loop.yml
PLAY [testhost] **************************************************************** TASK [setup] *******************************************************************
ok: [192.168.2.71]
ok: [192.168.2.72]
ok: [192.168.2.73] TASK [change mode for files] ***************************************************
changed: [192.168.2.73] => (item=1.txt)
changed: [192.168.2.71] => (item=1.txt)
changed: [192.168.2.72] => (item=1.txt)
changed: [192.168.2.73] => (item=2.txt)
changed: [192.168.2.71] => (item=2.txt)
changed: [192.168.2.72] => (item=2.txt) PLAY RECAP *********************************************************************
192.168.2.71 : ok=2 changed=1 unreachable=0 failed=0
192.168.2.72 : ok=2 changed=1 unreachable=0 failed=0
192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0

三、playbook条件判断

条件判断一般用于针对不同版本的系统,比如对centos、ubuntu 等系统进行不同的操作命令。

(1)编辑配置文件

[root@tiejiangSRC1 yml]# vim when.yml
---
- hosts: testhost
user: root
gather_facts: True
tasks:
- name: use when
shell: touch /tmp/when.txt
when: ansible_default_ipv4.address == "192.168.2.73"

(2)执行配置文件

[root@tiejiangSRC1 yml]# ansible-playbook when.yml
PLAY [testhost] **************************************************************** TASK [setup] *******************************************************************
ok: [192.168.2.71]
ok: [192.168.2.73]
ok: [192.168.2.72] TASK [use when] ****************************************************************
skipping: [192.168.2.71]
skipping: [192.168.2.72]
changed: [192.168.2.73]
[WARNING]: Consider using file module with state=touch rather than running touch PLAY RECAP *********************************************************************
192.168.2.71 : ok=1 changed=0 unreachable=0 failed=0
192.168.2.72 : ok=1 changed=0 unreachable=0 failed=0
192.168.2.73 : ok=2 changed=1 unreachable=0 failed=0

四、playbook handlers

当我们执行 tasks 后,服务器发生变化之后我们要执行一些操作。比如我们修改了某个服务的配置文件,需要重启下服务。实例如下:

(1)编辑配置文件

[root@tiejiangSRC1 yml]# vim handlers.yml
---
- name: handlers test
hosts: testhost
user: root
tasks:
- name: test copy
copy: src=/etc/passwd dest=/tmp/handlers.txt
notify: test handlers
handlers:
- name: test handlers
shell: echo "www.tiejiang.org" >> /tmp/handlers.txt

说明:只有 copy 模块真正执行后,才会去调用下面的 handlers 相关的操作,追加内容。也就是说如果 src 和 dest 内容是一样的,并不会去执行 handlers 里面的 shell 相关命令。所以这种比较适合配置文件发生更改后,需要重启服务的操作。

(2)执行配置文件

[root@tiejiangSRC1 yml]# ansible-playbook handlers.yml
PLAY [handlers test] *********************************************************** TASK [setup] *******************************************************************
ok: [192.168.2.73]
ok: [192.168.2.71]
ok: [192.168.2.72] TASK [test copy] ***************************************************************
changed: [192.168.2.71]
changed: [192.168.2.73]
changed: [192.168.2.72] RUNNING HANDLER [test handlers] ************************************************
changed: [192.168.2.71]
changed: [192.168.2.73]
changed: [192.168.2.72] PLAY RECAP *********************************************************************
192.168.2.71 : ok=3 changed=2 unreachable=0 failed=0
192.168.2.72 : ok=3 changed=2 unreachable=0 failed=0
192.168.2.73 : ok=3 changed=2 unreachable=0 failed=0

(3)查看执行结果

[root@tiejiangSRC1 yml]# ansible testhost -m command -a 'tail -n 1 /tmp/handlers.txt '     //这里我直接用-n 1显示handlers.txt的最后一行内容
192.168.2.71 | SUCCESS | rc=0 >>
www.tiejiang.org 192.168.2.73 | SUCCESS | rc=0 >>
www.tiejiang.org 192.168.2.72 | SUCCESS | rc=0 >>
www.tiejiang.org

可查看到 copy 文件成功,同时也执行了 handlers 的相关命令,追加了新的信息。

Ansible Playbook 详解的更多相关文章

  1. ansible playbook详解

    ansible playbook是由yml语法书写,结构清晰,可读性强,所以必须掌握yml基础语法 语法 描述 缩进 YAML使用固定的缩进风格表示层级结构,每个缩进由两个空格组成,不能使用tabs键 ...

  2. Ansible之Playbook详解

    1.Playbook详解 playbook是一个非常简单的配置管理和多主机部署系统,可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式. 核心元素 Hosts:主机 Tasks:任务,由 ...

  3. Ansible配置详解

    目录 Ansible配置详解 参考 配置优先级 配置参数说明 Ansible配置详解

  4. ansible自动化运维详细教程及playbook详解

    前言 当下有许多的运维自动化工具( 配置管理 ),例如:Ansible.SaltStack.Puppet.Fabric 等. Ansible 一种集成 IT 系统的配置管理.应用部署.执行特定任务的开 ...

  5. Ansible配置文件ansible.cfg详解

    Ansible是一个系列文章,我会尽量以通俗易懂.诙谐幽默的总结方式给大家呈现这些枯燥的知识点,让学习变的有趣一些. Ansible系列博文直达链接:Ansible入门系列 前言 此时外面小雨淅淅沥沥 ...

  6. 2、Ansible配置文件详解

    0.配置文件 两个核心文件:ansible.cfg和hosts文件,默认都存放在/etc/ansible目录下. ansible.cfg:主要设置一些ansible初始化的信息,比如日志存放路径.模块 ...

  7. Ansible 配置文件详解

    # config file for ansible -- http://ansible.com/ # ============================================== #  ...

  8. Ansible之Playbook详解、案例

    什么是playbook playbooks是一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活.简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的 ...

  9. ansible中的playbook详解

    首先简单说明一下playbook,playbook是什么呢?根本上说playbook和shell脚本没有任何的区别,playbook就像shell一样,也是把一堆的命令组合起来,然后加入对应条件判断等 ...

随机推荐

  1. groovy语法

    1.注释1.1. 单行注释1.2. 多行注释1.3. GroovyDoc注释1.4. Shebang线2.关键词3.标识符3.1. 普通标识符3.2. 带引号的标识符4.字符串4.1. 单引号字符串4 ...

  2. oracle vm突然黑屏了

    装完mongodb-compass后,我重启了下虚拟机,发现突然进不到ubuntu系统了,一开起来就黑屏.网上查了下有的说是显卡问题的,有说是内核问题的,但我啥都没干突然间就黑屏了.折腾了一下午没搞定 ...

  3. 【369】列表/字典的分拆, unpacking

    参考: python--参数列表的分拆 参考: List Comprehensions 当你要传递的参数已经是一个列表,调用的函数却接受分开一个个的参数,这个时候可以考虑参数列表拆分: 可以使用* 操 ...

  4. ORACLE常用操作命令

    1.ORACLE实例启动.停止 SQL>startup;  #启动ORACLE实例 SQL>shutdown immediate; #关闭ORACLE实例,常用.阻止新用户连接且阻止已连接 ...

  5. arcgis_SDE安装步骤

    弄了将近一个星期的Oracle和ArcSDE终于让我给弄好了!下面把过程跟大家分享一下: 首先是Oracle10gR2的安装,在Oracle的官方网站上可以下到Oracle10gR2的安装程序,安装过 ...

  6. CMake Error at cmake/OpenCVUtils.cmake

    CMake Error at cmake/OpenCVUtils.cmake:1047 (message): Failed to download . Status= Call Stack (most ...

  7. 深入理解hello world

    阅读目录 为什么所有东西都是从类开始的 为什么总是需要有一个“main”方法 HelloWorld的字节码 HelloWorld在JVM中是如何运行的 对于每个Java程序员来说,HelloWorld ...

  8. 'basetsd.h': No such file or directory

    By Select the Windows 8.1 SDK During install. Download visualcppbuildtools_full.exe from below websi ...

  9. 第三章,DNA序列的进化演变

    31.前言 3.1.两个序列间的核苷酸差异 来自同一祖先序列的两条后裔序列,之间的核苷酸的差异随着时间的增加而变大.简单的计量方法,p距离 3.2.核苷酸代替数的估计 3.3.Jukes和Cantor ...

  10. Real Time Render 4

    [Real Time Render 4] 1.Radiometry(辐射测试) deals with the measurement of electromagnetic(电磁) radiation( ...