|
一 在远程创建文件
1 进入/etc/ansible 建立test.yml这个文件,然后加入如下内容:
l相当于把模块写入到配置文件里面,例:
1
2
3
4
5
6
7
| lcat /etc/ansible/test.yml
---
- hosts: testhost
remote_user: root
tasks:
- name: test_playbook
shell: touch /tmp/huningei.txt
|
##注意格式,前面都有两个空格,冒号之后也都有空格
l说明: hosts参数指定了对哪些主机进行参作;
luser参数指定了使用什么用户登录远程主机操作;
ltasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来。
l执行:ansible-playbook test.yml
二 playbook循环
建立三个文件
1
2
3
4
5
6
| cat file.yml
- hosts: testhost
user: root
tasks:
- name: touch files
shell: touch /tmp/{1.txt,2.txt,3,txt}
|
更改三个文件的权限
1
2
3
4
5
6
7
8
9
10
| ---
- hosts: testhost
user: root
tasks:
- name: change mod for file
file: path=/tmp/{{item}} mode=600
with_items:
- 1.txt
- 2.txt
- 3.txt
|
三 playbook条件判断
1
2
3
4
5
6
7
8
9
| [iyunv@slave ansible]# cat a.yml
---
- hosts: testhost
user: root
gather_facts: True
tasks:
- name: use when
shell: touch /tmp/when.txt
when: ansible_eth0.ipv4.address == "192.168.216.132"
|
最后执行 ansible-playbook a.yml
这个文件的意思是:当ansible_eth0.ipv4.address == "172.7.15.106" 时,在这台机器上touch /tmp/when.tx
注意:如果在虚拟机上面做操作,可能会出现错误,先用这个 ansible 192.168.216.131 -m setup 来查看一下条件
|
|
|
|
|
|
|