ansible playbook的使用
一、playbook的简单使用1、创建文件实例
1)编辑配置文件
1
2
3
4
5
6
7
8
9
10
# cd /etc/ansible/
# ls
ansible.cfghostsroles
# vi test.yml //固定后缀为yml,一定要注意空格
---
- hosts: web10.gz.com
user: root
tasks:
- name: playbook_test
shell: touch /tmp/playbook.txt
注意:hosts参数指定了对哪些主机进行参作;user参数指定了使用什么用户登录远程主机操作;tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。2)执行配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ansible-playbook test.yml
PLAY ***************************************************************************
TASK *******************************************************************
ok:
TASK ***********************************************************
changed:
: Consider using file module with state=touch rather than running touch
PLAY RECAP *********************************************************************
web10.gz.com : ok=2 changed=1 unreachable=0 failed=0
查看有没有创建
1
2
3
4
5
6
7
8
9
10
11
12
# ls -la /tmp/
总用量 32
drwxrwxrwt.5 root root 4096 4月22 04:12 .
dr-xr-xr-x. 23 root root 4096 4月21 23:11 ..
-rw-r--r--.1 root root 1273 4月22 01:17 1.txt
drwxr-xr-x.3 root root 4096 4月22 01:23 ansibletest
drwxr-xr-x.3 root root 4096 4月22 01:07 ansibletestowner=root
-rw-r--r--.1 root root 43 4月22 01:53 an_test.txt
drwxrwxrwt.2 root root 4096 4月21 23:11 .ICE-unix
-rw-r--r--.1 root root 0 4月22 04:09 playbook.txt
-rwxr-xr-x.1 root root 49 4月22 01:49 test.sh
-rw-------.1 root root 0 4月20 07:24 yum.log
2、创建用户实例
1)编辑配置文件(创建用户)
1
2
3
4
5
6
7
8
9
10
11
# vi user.yml
---
- name: create_user
hosts: web10.gz.com
user: root
gather_facts: false
vars:
- user: "test"
tasks:
- name: create user
user: name="{{ user }}"
注意:name参数对该playbook实现的功能做一个概述,后面执行过程中,会打印 name变量的值 ,可以省略;
gather_facts参数指定了在以下任务部分执行前,是否先执行setup模块获取主机相关信息,这在后面的task会使用到setup获取的信息时用到;
vars参数指定了变量,这里指字一个user变量,其值为test ,需要注意的是,变量值一定要用引号引住;
user提定了调用user模块,name是user模块里的一个参数,而增加的用户名字调用了上面user变量的值。2)执行配置文件
1
2
3
4
5
6
7
8
9
# ansible-playbook user.yml
PLAY *************************************************************
TASK *************************************************************
changed:
PLAY RECAP *********************************************************************
web10.gz.com : ok=1 changed=1 unreachable=0 failed=0
查看
1
2
# grep test /etc/passwd
test:x:500:500::/home/test:/bin/bash
页:
[1]