Ansible剧本编写说明

一. 缩进

    yaml 的缩进要求比较严格。一定不能使用tab键

    注意:编写yaml文件,就忘掉shell的tab吧。

二. 冒号

每个冒号后面一定要有一个空格

注意:1. 以冒号结尾不需要空格

           2.表示文件路径的模版可以不需要空格

三. 短横线  -

    想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分

总之:

1. 严格控制空格编写剧本的时候

2.剧本编写不支持tab

Ansible书写规范

一. 注意点

1、脚本名:***.yml   【不是.yml也行】

2、注释:  “#”

3、tasks: (后不能加任何字符-可以加注释#)

4、- name: 一个 - name: 下不能有两条功能(行)

5.每个-和:即冒号之后要有一个空格,task除外。

二.  剧本格式

---                     ### 剧本的开头,可以不写

- hosts: all         <- 处理所有服务器,找到所有服务器;  -(空格)hosts:(空格)all

  tasks:            <- 剧本所要干的事情;  (空格)(空格)task:

   - command:     (空格)(空格)空格)(空格)-(空格)模块名称:(空格)模块中对应的功能

测试剧本命令后面可以跟多个-v进行调试检查

Ansible剧本常用命令

1 .对剧本语法检测

ansible-playbook --syntax-check  /root/ansible/httpd.yaml

2.-C模拟执行剧本

ansible-playbook  -C /root/ansible/httpd.yaml

3.执行剧本

ansible-playbook   /root/ansible/httpd.yaml

Ansible剧本实例

 

实例一:安装httpd并启动

第一步: 编写一个httpdin.yaml剧本,剧本内容如下

[root@ken ~]# vim httpdin.yaml
- hosts: all
  tasks:
   - name: install httpd
     yum: name=httpd state=present
   - name: start httpd
     service: name=httpd state=started

第二步:语法检测

检测没有问题

[root@ken ~]# ansible-playbook --syntax-check httpdin.yaml

playbook: httpdin.yaml

第三步:模拟执行剧本

[root@ken ~]# ansible-playbook -C httpdin.yaml

PLAY [all] *********************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [10.220.5.139]
ok: [10.220.5.138]

TASK [install httpd] ***********************************************************************************************************
ok: [10.220.5.138]
changed: [10.220.5.139]

TASK [start httpd] *************************************************************************************************************
changed: [10.220.5.139]
ok: [10.220.5.138]

PLAY RECAP *********************************************************************************************************************
10.220.5.138               : ok=3    changed=0    unreachable=0    failed=0
10.220.5.139               : ok=3    changed=2    unreachable=0    failed=0   

第四步:执行剧本

经过上面的模拟执行,并不会真的执行脚本,下面我们来进行真正的执行

[root@ken ~]# ansible-playbook  httpdin.yaml

PLAY [all] *********************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [10.220.5.138]
ok: [10.220.5.139]

TASK [install httpd] ***********************************************************************************************************
ok: [10.220.5.138]
changed: [10.220.5.139]

TASK [start httpd] *************************************************************************************************************
ok: [10.220.5.138]
changed: [10.220.5.139]

PLAY RECAP *********************************************************************************************************************
10.220.5.138               : ok=3    changed=0    unreachable=0    failed=0
10.220.5.139               : ok=3    changed=2    unreachable=0    failed=0   

第五步:检查是否已经安装并启动成功

执行表名httpd安装并启动成功

[root@ken ~]# ansible all -m shell -a "ss -tnl | grep 80"
10.220.5.139 | SUCCESS | rc=0 >>
LISTEN     0      128         :::80                      :::*

10.220.5.138 | SUCCESS | rc=0 >>
LISTEN     0      128         :::80                      :::*                  

经过上面的五步,就可以安装任意软件及启动了,前提是你需要配置好你的yum源。你来试一下安装nginx吧!

实例二:使用处理器

在上面的实例中,我们已经可以安装软件了。现在有这样一个需求,如果我们更改了节点的配置文件需要重启操作,其余动作照常执行怎么解决?

第一步:本地准备一个httpd配置文件,更改端口号为8081

[root@ken ~]# cp /etc/httpd/conf/httpd.conf ./
[root@ken ~]# sed -i "s/Listen 80/Listen 8081/" httpd.conf 

第二步:编写剧本

剧本说明:

- hosts: all 指定主机组,可以理解为这个最大,顶个写

  tasks:       指定下面一系列的动作,这个是第二,需要有两个空格

    - name: 指定名称,排行第三,需要有三到四个空格

      yum: 模块名 排行第四,需要有四到五个空格

  headlers:指定处理器(触发器),排行第二,需要与tasks对齐

- hosts: all
  tasks:
   - name: install httpd
      yum: name=httpd state=present
   - name: copy file
     copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
     notify:
       - restart httpd
   - name: start httpd
     service: name=httpd state=present
  handlers:
   - name: restart httpd
     service: name=httpd state=restarted

第三步:剧本语法检测

剧本加测报一下错误,说明排版有问题

[root@ken ~]# ansible-playbook --syntax-check httpdhe.yaml
ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to have been in '/root/httpdhe.yaml': line 4, column 11, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  - name: install httpd
       yum: name=httpd state=present
          ^ here

 第四步:排查错误

可以发现第四行yum多缩进了一个空格,一定要严格控制缩进。

tasks和hosts首字母对齐

模块和name对齐首字母

- hosts: all
  tasks:
   - name: install httpd
     yum: name=httpd state=present
   - name: copy file
     copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
     notify:
       - restart httpd
   - name: start httpd
     service: name=httpd state=present
  handlers:
   - name: restart httpd
     service: name=httpd state=restarted

第五步:执行剧本

注意看执行过程,中间有一步RUNNING HANDLER,说明已经出发了处理器进行了重启操作

[root@ken ~]# ansible-playbook httpdhe.yaml

PLAY [all] *********************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [10.220.5.139]
ok: [10.220.5.138]

TASK [install httpd] ***********************************************************************************************************
ok: [10.220.5.139]
ok: [10.220.5.138]

TASK [copy file] ***************************************************************************************************************
changed: [10.220.5.139]
changed: [10.220.5.138]

TASK [start httpd] *************************************************************************************************************
ok: [10.220.5.138]
ok: [10.220.5.139]

RUNNING HANDLER [restart httpd] ************************************************************************************************
changed: [10.220.5.139]
changed: [10.220.5.138]

PLAY RECAP *********************************************************************************************************************
10.220.5.138               : ok=5    changed=2    unreachable=0    failed=0
10.220.5.139               : ok=5    changed=2    unreachable=0    failed=0   

第六步:检查端口

上面我们已经把配置文件的端口改成8081了,检查下节点启动的是否是8081

可以发现节点中的8081已经启动

[root@ken ~]# ansible all -m shell -a "ss -tnl | grep 8081"
10.220.5.139 | SUCCESS | rc=0 >>
LISTEN     0      128         :::8081                    :::*

10.220.5.138 | SUCCESS | rc=0 >>
LISTEN     0      128         :::8081                    :::*                  

实例三:剧本中使用判断

剧本中可以使用when来进行判断

现在我们再主机名为ken1的节点之上创建一个用户tang

第一步:域名解析

确保本机以及节点之上可以解析IP和域名

root@ken ~]# echo "10.220.5.138 ken1" >>/etc/hosts

第二步:编写剧本

ansible_fqdn是一个变量,可以用如下命令看到,表示的是主机名

[root@ken ~]# ansible 10.220.5.138 -m setup | grep ansible_fqdn
        "ansible_fqdn": "ken1", 

在编写剧本的时候ken1即主机名需要加上双引号或者单引号,否则会报错

- hosts: all
  tasks:
   - name: useradd tang
     user: name=tang uid=566 system=yes
     when: ansible_fqdn ==  "ken1"

第三步:语法检测

[root@ken ~]# ansible-playbook --syntax-check useradd.yaml

playbook: useradd.yaml

第四步:执行剧本

可以看到10.220.5.139跳过了

[root@ken ~]# ansible-playbook useradd.yaml

PLAY [all] *********************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [10.220.5.139]
ok: [10.220.5.138]

TASK [useradd tang] ************************************************************************************************************
skipping: [10.220.5.139]
changed: [10.220.5.138]

PLAY RECAP *********************************************************************************************************************
10.220.5.138               : ok=2    changed=1    unreachable=0    failed=0
10.220.5.139               : ok=1    changed=0    unreachable=0    failed=0

[root@ken ~]# vim useradd.yaml

第五步:检测是否执行成功

可以发现在主机10.220.5.138上面已经有了用户tang

[root@ken ~]# ansible all -m shell -a "id tang"
10.220.5.139 | FAILED | rc=1 >>
id: tang: no such usernon-zero return code

10.220.5.138 | SUCCESS | rc=0 >>
uid=566(tang) gid=566(tang) groups=566(tang)

实例四:剧本中使用循环

剧本中的循环使用with_items,使用item引用变量

变量的引用格式是 {{ 变量 }}

第一步:创建剧本

- hosts: all
  tasks:
    - name: useradd ding
      user: name="ding{{item}}"
      with_items:
        - a1
        - a2
        - a3
        - a4

第二步:语法检测

[root@ken ~]# ansible-playbook --syntax-check user.yaml

playbook: user.yaml

第三步:执行剧本

[root@ken ~]# ansible-playbook user.yaml

PLAY [all] *********************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [10.220.5.139]
ok: [10.220.5.138]

TASK [useradd ding] ************************************************************************************************************
changed: [10.220.5.138] => (item=a1)
changed: [10.220.5.139] => (item=a1)
changed: [10.220.5.138] => (item=a2)
changed: [10.220.5.139] => (item=a2)
changed: [10.220.5.138] => (item=a3)
changed: [10.220.5.139] => (item=a3)
changed: [10.220.5.138] => (item=a4)
changed: [10.220.5.139] => (item=a4)

PLAY RECAP *********************************************************************************************************************
10.220.5.138               : ok=2    changed=1    unreachable=0    failed=0
10.220.5.139               : ok=2    changed=1    unreachable=0    failed=0   

第四步:查看执行结果

可以发现用户已经创建完毕

[root@ken ~]# ansible all -m shell -a "tail -5 /etc/passwd"
10.220.5.139 | SUCCESS | rc=0 >>
wukong:x:1002:234::/home/wukong:/bin/bash
dinga1:x:1003:1003::/home/dinga1:/bin/bash
dinga2:x:1004:1004::/home/dinga2:/bin/bash
dinga3:x:1005:1005::/home/dinga3:/bin/bash
dinga4:x:1006:1006::/home/dinga4:/bin/bash

10.220.5.138 | SUCCESS | rc=0 >>
tang:x:566:566::/home/tang:/bin/bash
dinga1:x:1002:1002::/home/dinga1:/bin/bash
dinga2:x:1003:1003::/home/dinga2:/bin/bash
dinga3:x:1004:1004::/home/dinga3:/bin/bash
dinga4:x:1005:1005::/home/dinga4:/bin/bash

自我感觉这个循环很low,如果创建1000个用户难道需要这样写- a1 -a2 -a3...-a1000个吗?

实例五:剧本中使用变量

自定义变量的实现方式

1. 直接写在yaml文件中

2. 在Inventory file中定义

3. 变量的声明:

vars:

 - 变量名: 变量值

4. 变量的引用:{{ var }}

5. vars和tasks同级,并要写在tasks前面

下面的剧本定义了一个变名为pkgname 变量值为mariadb-server, 变量名为sername,变量值为mariadb两个变量

第一步:创建剧本

- hosts: all
  vars:
    - pkgname: mariadb-server
    - sername: mariadb
  tasks:
    - name: install mariadb
      yum: name={{ pkgname }} state=present
    - name: start mariadb
      service: name={{ sername }} state=started

第二步:语法检测

[root@ken ~]# ansible-playbook --syntax-check mariadb.yaml

playbook: mariadb.yaml

第三步:执行剧本

[root@ken ~]# ansible-playbook mariadb.yaml

PLAY [all] *********************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************
ok: [10.220.5.139]
ok: [10.220.5.138]

TASK [install mariadb] *********************************************************************************************************
ok: [10.220.5.138]
ok: [10.220.5.139]

TASK [start mariadb] ***********************************************************************************************************
changed: [10.220.5.138]
changed: [10.220.5.139]

PLAY RECAP *********************************************************************************************************************
10.220.5.138               : ok=3    changed=1    unreachable=0    failed=0
10.220.5.139               : ok=3    changed=1    unreachable=0    failed=0   

第四步:检测剧本是否执行成功

[root@ken ~]# ansible all -m shell -a "ss -tnl| grep 3306"
10.220.5.139 | SUCCESS | rc=0 >>
LISTEN     0      50           *:3306                     *:*

10.220.5.138 | SUCCESS | rc=0 >>
LISTEN     0      50           *:3306                     *:*                  
11-20 16:15