(1) 编写一个playbook,hosts为node1和node2,复制/etc/passwd文件,属主为upwen,属组为upwen,权限为0600;
首先创建upwen:ansible all -m user -a "name=upwen state=present"
创建代码:vim test.yml
执行代码:ansible-playbook test.yml
并验证结果:ansible all -a ' ls -1 /home/upwen/passwd '
---
- name: copy
hosts: all
tasks:
- name: copy passwd
copy:
src: /etc/passwd
dest: /home/upwen/passwd
owner: upwen
group: upwen
mode: "0600"
(2) 编写一个playbook,在node1和node2上执行play1,创建用户lisi,并指定其uid为1999;在node1上执行play2,删除用户lisi;(playbook执行后的结果输出,以及通过临时命令查看/etc/passwd文件当中的用户信息)
检查新建用户:cut -d : -f 1 /etc/passwd
---
- name: play1
hosts: all
tasks:
- name: creat user
user:
name: lisi
uid: 1999
shell: /bin/lisi
- name: play2
hosts: node1
tasks:
- name: delete user
user:
user: lisi
state: absent
remove: yes
(3) 编写一个playbook,利用循环结构,在node1和node2上分别创建三个用户,tom,jerry,cat;(执行完成之后查看并验证结果)
---
- name: creat user and loop
hosts: all
tasks:
- name: creat user
user:
name: "{{item}}"
state: present
loop:
- tom
- jerry
- cat