在playbook中有严格的缩进要求,不能空格和tab同时使用,不同的缩进代表不同的级别,所以相同级别时必须要有相同的缩进。
且每个playbook习惯上以"---"开头。
playbook默认以root账户对远程主机执行操作。
编写一个简单的playbook:
[root@localhost ansible]# cat test.yml
---
- hosts: wyh-test
remote_user: root
tasks:
- name: create new directory
file: name=/usr/local/wyh state=directory
- name: create new file
file: name=/usr/local/wyh/test0905.txt state=touch
- 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
编写好playbook之后可以先使用-C(check)参数来对yml文件进行检查。
[root@localhost ansible]# ansible-playbook -C test.yml
执行:
[root@localhost ansible]# ansible-playbook test.yml
执行成功之后我们可以使用命令来验证一下上述的任务是否真的执行了:
#查看80端口是否启动
[root@localhost ansible]# ansible wyh-test -m shell -a 'ss -tln|grep :80'
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
192.168.126.129 | CHANGED | rc=0 >>
LISTEN 0 128 [::]:80 [::]:*
#查看index.html是否可访问
[root@localhost ansible]# curl 192.168.126.129
Hello World!
playbook中变量的使用:
变量必须是由数字、字母、下划线组成,并且只能以字母开头。
[root@localhost ansible]# cat test-app.yml
---
- hosts: wyh-test
remote_user: root
tasks:
- name: install package
yum: name={{ pkname }}
- name: start service
service: name={{ pkname }} state=started enabled=yes
#这里我们要测试的是vsftp,它的yum包和service服务中的名成是一样的,所以我们使用了同一个变量,但是可能有些包名和服务名是不一样的,需要不同的变量名。
传参执行:
[root@localhost ansible]# ansible-playbook -e 'pkname=vsftpd' test-app.yml
可以看到21端口已经启动:?
[root@localhost ansible]# ansible wyh-test -m shell -a 'ss -tln|grep :21'
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
192.168.126.129 | CHANGED | rc=0 >>
LISTEN 0 32 [::]:21 [::]:*
也可以传多个参数:
[root@localhost ansible]# cat test-app.yml
---
- hosts: wyh-test
remote_user: root
tasks:
- name: install package
yum: name={{ pkname1 }},{{ pkname2 }}
执行:
[root@localhost ansible]# ansible-playbook -e 'pkname1=httpd pkname2=vsftpd' test-app.yml
查看rpm包中是否安装成功:
[root@localhost ansible]# ansible wyh-test -m shell -a 'rpm -q httpd vsftpd'
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use
command because yum, dnf or zypper is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.126.129 | CHANGED | rc=0 >>
httpd-2.4.6-97.el7.centos.x86_64
vsftpd-3.0.2-29.el7_9.x86_64
也可以直接在playbook里面定义变量:
#可以先把刚才安装好的httpd/vsftpd包删掉,然后再做测试
[root@localhost ansible]# ansible wyh-test -m shell -a 'yum -y remove httpd vsftpd'
在playbook中添加vars来指定参数:?
[root@localhost ansible]# cat test-app.yml
---
- hosts: wyh-test
remote_user: root
vars:
- pkname1: httpd
- pkname2: vsftpd
tasks:
- name: install package
yum: name={{ pkname1 }},{{ pkname2 }}
?执行时就不需要指定参数了:
[root@localhost ansible]# ansible-playbook test-app.yml
执行完之后也可以再去看一下rpm中有没有安装好这两个包。
|