ansibleplaybook的使用

1、简单格式要求

[root@ansibleserver ansible]# cat nagios.yml

---

- hosts: nagiosserver

 tasks:

        - name: ensure nagios service stop

         service: name=nagios state=stopped

        - name: ensure nagios service start

         service: name=nagios state=started

a、整体格式用---开始

b、在冒号之后,必须存在一个空格

c、name和service必须对齐

d、hosts和tasks必须对齐

e、在书写key和value的时候,不能存在空格

f、 在roles中的main.yml不能使用tasks关键词,主要是因为在目录结构中已经包含了此关键词,main.yml是存在于tasks目录中。

如果违反以上规定,那么就会出错,出错内容如下:

ERROR: Syntax Error while loading YAML script, nagios.yml

Note: The error may actually appear before this position: line 3, column 1

- hosts: nagiosserver

tasks:

^

2、运行playbook

对上面格式中的nagios.yml进行运行,运行命令如下:

[root@ansibleserver ansible]# ansible-playbook nagios.yml

在上面的playbook中,存在两个任务,一个是停止nagios服务,一个是启动nagios服务,运行结果如下所示:

PLAY [nagiosserver] ***********************************************************

GATHERING FACTS ***************************************************************

ok: [192.168.1.20]

TASK: [ensure nagios service stop] ********************************************

changed: [192.168.1.20]

TASK: [ensure nagios service start] *******************************************

changed: [192.168.1.20]

PLAY RECAP ********************************************************************

192.168.1.20               : ok=3    changed=2    unreachable=0    failed=0

可以看到,定义为name的地方会在输出中进行显示,然后显示运行结果,也就是changed,从而在第一个任务中,表示关闭服务成功,第二个任务中,表示为启动服务成功,最后会显示结果,也就是OK为3,表示连接成功,关闭服务成功,启动服务成功,chenged为2,表示为进行了两次修改。


 3、最佳实践

创建roles的组织目录结构可以用如下语句(一次性创建目录组织架构,修改common表示相关的roles):

mkdir -p roles/common/{tasks,handlers,templates,files,vars,defaults,meta}

总体目录结构如下所示:

[root@ansibleserver kel]# ls -l

total 28

drwxr-xr-x 2 root root 4096 Jan 27 06:23 group_vars

-rw-r--r-- 1 root root  108 Jan 28 11:35 hosts

drwxr-xr-x 2 root root 4096 Jan 28 11:34 host_vars

-rw-r--r-- 1 root root   97 Jan 27 06:21 production.yml

drwxr-xr-x 4 root root 4096 Jan 27 06:17 roles

-rw-r--r-- 1 root root   53 Jan 28 11:27 site.yml

-rw-r--r-- 1 root root   77 Jan 27 06:21 staging.yml

         在定级目录中,roles和site.yml和staging.yml和production.yml,group_vars和host_vars在同一级目录中,在变量的优先级中,host_vars的变量优先级比group_vars的优先级高,使用的变量的时候,如果名字相同,那么host_vars中变量的值会覆盖group_vars中变量的值,在roles中设置变量的时候,roles中变量的优先级又比host_vars高

         在这里site.yml包含staging和production的playbook,从而在这里使用的是include,在staging和production中使用的是roles,从而需要注意的是roles里的目录结构,roles的目录结构如下所示:

[root@ansibleserver kel]# cd roles

[root@ansibleserver roles]# ls -l

total 8

drwxr-xr-x 3 root root 4096 Jan 27 06:18 adduser

drwxr-xr-x 4 root root 4096 Jan 24 14:13 changepassword

[root@ansibleserver roles]# cd changepassword/

[root@ansibleserver changepassword]# ls -l

total 8

drwxr-xr-x 2 root root 4096 Jan 27 06:22 tasks

drwxr-xr-x 2 root root 4096 Jan 28 11:43 vars

[root@ansibleserver changepassword]# cd tasks/

[root@ansibleserver tasks]# ls -l

total 4

-rw-r--r-- 1 root root 140 Jan 27 06:22 main.yml

在roles中,包含的文件夹也就是staging中包含的roles,也就是一个文件夹,表示一个roles,在上面的例子中分为adduser和changepassword两个roles,在子目录中,包含tasks和vars,表示所进行的任务和变量,还有其他的文件夹没有进行创建,从而在最后是一个main.yml

分别的内容如下:

[root@ansibleserver tasks]# cat main.yml

---

#- hosts: ansibleservers

#  use the variables kel to change the user root's passwrod

- name: change the user root password

  shell: 'echo "{{kel}}"|passwd --stdin root > /dev/null 2>&1'

此内容主要是用kel变量的值作为root用户的密码

[root@ansibleserver kel]# cat site.yml

---

- include: staging.yml

- include: production.yml

此中的内容,主要就是使用include语句来包含另外两个playbook

[root@ansibleserver kel]# cat staging.yml

---

- hosts: ansibleserver1

  roles:

      - changepassword

#      - adduser

此种的内容,主要是用来表示修改密码,使用的是roles,在使用roles的时候,注意目录结构,使用roles的方式也是推荐的一种方式。

执行的时候如下:

[root@ansibleserver kel]# ansible-playbook -i hosts site.yml

SSH password:

PLAY [ansibleserver1] *********************************************************

GATHERING FACTS ***************************************************************

ok: [192.168.1.163]

TASK: [changepassword | change the user root password] ************************

changed: [192.168.1.163]

PLAY [ansibleserver2] *********************************************************

GATHERING FACTS ***************************************************************

ok: [192.168.1.164]

TASK: [changepassword | change the user root password] ************************

changed: [192.168.1.164]

PLAY RECAP ********************************************************************

192.168.1.163              : ok=2    changed=1    unreachable=0    failed=0  

192.168.1.164              : ok=2    changed=1    unreachable=0    failed=0

发现执行成功,再最佳实践里推荐的就是用roles来执行相关的任务,然后将生产环境和测试环境进行分开。


更新一下github的地址,在里面写了相关模块的例子,从而可以进行查看,以后会根据相关的东西,从而写出相关的playbook


for linux and python
原文地址:https://www.cnblogs.com/kellyseeme/p/5525078.html