本章主要介绍如何在ansible中写脚本
1 ‐ name: play的名称
2 hosts:主机组1,主机组2,... #‐‐列出主机组
3 tasks:
4 ‐ name: 提示信息1
5 模块1: argx1=vx1 argz2=vx2 #这种写法,“=”两边不要有空格
6 ‐ name: 提示信息x
7 模块x: rgxl=vxl argx2=vx2
1 ‐ hosts:主机组1,主机组2,... #‐‐列出主机组
2 tasks:
3 ‐ name:描述语句1
4 模块1:
5 argxl: vxl #这里指定模块的参数,注意冒号后面的空格
6 argx2: vx2
7 ‐ name:描述语句2
8 模块x:
9 argxl:vx1
10 argx2:vx2
1 ‐‐‐
2 ‐ name: 第一个play的名称
3 hosts: 主机组1,主机组2,...#‐‐列出主机组
4 tasks:
5 ‐ name:提示信息1
6 模块1:argxl‐vxl argx2=vx2
7 ‐ name:提示信息×
8 模块x:rgxl=vxl argx2=vx2
9 ‐ name: 第二个play的名称
10 hosts: 主机组3,主机组4,...#‐‐列出主机组
11 gather facts: false
12 tasks:
13 ‐ name: 提示信息1
14 模块1: argxl=vxl argx2=vx2
15 ‐ name: 提示信息×
16 模块x: rgxl=vxl argx2=Vx2
[blab@node01 ~]$ mkdir demo1 //创建文件夹
[blab@node01 ~]$ cp ansible.cfg hosts demo1/ //拷贝文件
[blab@node01 ~]$ cd demo1/ //切换工作目录
[blab@node01 demo1]$ ls //列出当前工作目录文件
ansible.cfg hosts
[blab@node01 demo1]$
[blab@node01 demo1]$ cat test1.yml
---
- hosts: node02,node03
tasks:
- name: 打印主机名
debug: msg={{ansible_fqdn}}
- name: 打印IP
debug: msg={{ansible_default_ipv4.address}}
[blab@node01 demo1]$
[blab@node01 demo1]$ ansible-playbook test1.yml
PLAY [node02,node03] ***********************************************************
TASK [Gathering Facts] *********************************************************
ok: [node03]
ok: [node02]
TASK [打印主机名] *******************************************************************
ok: [node02] => {
"msg": "node02"
}
ok: [node03] => {
"msg": "node03"
}
TASK [打印IP] ********************************************************************
ok: [node02] => {
"msg": "192.168.182.193"
}
ok: [node03] => {
"msg": "192.168.182.210"
}
PLAY RECAP *********************************************************************
node02 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node03 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo1]$
[blab@node01 demo1]$ cat test2.yml
---
- name: 在node02上的操作
hosts: node02
tasks:
- name: 这是第一个操作
debug: msg={{ansible_fqdn}}
- name: 在node03上的操作
hosts: node03
tasks:
- name: 打印IP
debug: msg={{ansible_default_ipv4.address}}
[blab@node01 demo1]$
[blab@node01 demo1]$ ansible-playbook test2.yml
PLAY [在node02上的操作] *************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [这是第一个操作] *****************************************************************
ok: [node02] => {
"msg": "node02"
}
PLAY [在node03上的操作] *************************************************************
TASK [Gathering Facts] *********************************************************
ok: [node03]
TASK [打印IP] ********************************************************************
ok: [node03] => {
"msg": "192.168.182.210"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node03 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo1]$
[blab@node01 demo1]$ cat test3_1.yml
---
- name: 在node02上要做的操作----安装vsftpd,启动服务,开启防火墙
hosts: node02
tasks:
- name: 第一个操作安装vsftpd
yum: name=vsftpd state=installed
- name: 第二个操作启动服务
service: name=vsftpd state=started enabled=yes
- name: 第三个操作开启防火墙
firewalld: service=ftp state=enabled immediate=yes permanent=yes
- name: 在node03上要做的操作----安装httpd,启动服务,开启防火墙
hosts: node03
tasks:
- name: 第一个操作安装httpd
yum: name=httpd state=installed
- name: 第二个操作启动服务
service: name=httpd state=started enabled=yes
- name: 第三个操作开启防火墙
firewalld: service=http state=enabled immediate=yes permanent=yes
[blab@node01 demo1]$
[blab@node01 demo1]$ ansible-playbook test3_1.yml
PLAY [在rhel802上要做的操作----安装vsftpd,启动服务,开启防火墙] ***********************************
TASK [Gathering Facts] *********************************************************
ok: [node02]
TASK [第一个操作安装vsftpd] ***********************************************************
ok: [node02]
TASK [第二个操作启动服务] ***************************************************************
ok: [node02]
TASK [第三个操作开启防火墙] **************************************************************
changed: [node02]
PLAY [在rhel803上要做的操作----安装httpd,启动服务,开启防火墙] ************************************
TASK [Gathering Facts] *********************************************************
ok: [node03]
TASK [第一个操作安装httpd] ************************************************************
changed: [node03]
TASK [第二个操作启动服务] ***************************************************************
changed: [node03]
TASK [第三个操作开启防火墙] **************************************************************
changed: [node03]
PLAY RECAP *********************************************************************
node02 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node03 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[blab@node01 demo1]$
[blab@node01 demo1]$ cat test4.yml
---
- hosts: node02
gather_facts: false
tasks:
- name: aa
debug: msg={{default_xxx}}
- name: bb
debug: msg="2222"
[blab@node01 demo1]$
[blab@node01 demo1]$ ansible-playbook test4.yml
PLAY [node02] ******************************************************************
TASK [aa] **********************************************************************
fatal: [node02]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'default_xxx' is undefined\n\nThe error appears to be in '/home/blab/demo1/test4.yml': line 5, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: aa\n ^ here\n"}
PLAY RECAP *********************************************************************
node02 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[blab@node01 demo1]$
[blab@node01 demo1]$ cat test4.yml
---
- hosts: node02
gather_facts: false
tasks:
- name: aa
debug: msg={{default_xxx}}
ignore_errors: true //增加内容
- name: bb
debug: msg="2222"
[blab@node01 demo1]$
[blab@node01 demo1]$ ansible-playbook test4.yml
PLAY [node02] ******************************************************************
TASK [aa] **********************************************************************
fatal: [node02]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'default_xxx' is undefined\n\nThe error appears to be in '/home/blab/demo1/test4.yml': line 5, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: aa\n ^ here\n"}
...ignoring
TASK [bb] **********************************************************************
ok: [node02] => {
"msg": "2222"
}
PLAY RECAP *********************************************************************
node02 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
[blab@node01 demo1]$
[blab@node01 demo1]$ cat test5.yml
---
- hosts: node02
gather_facts: false
tasks:
- name: aa
debug: msg="111"
- name: bb
fail: mag="222"
- name: ccc
debug: msg=333
[blab@node01 demo1]$
[blab@node01 demo1]$ ansible-playbook test5.yml
PLAY [node02] ******************************************************************
TASK [aa] **********************************************************************
ok: [node02] => {
"msg": "111"
}
TASK [bb] **********************************************************************
fatal: [node02]: FAILED! => {"changed": false, "msg": "Invalid options for fail: mag"}
PLAY RECAP *********************************************************************
node02 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
[blab@node01 demo1]$