ansible-playbook(2)
group模块参数:
name参数:必须参数,用于指定组名称。
state参数:用于指定组的状态,两个值可选,present,absent,默认为 present,设置为absent 表示删除组。
gid参数:用于指定组的gid。如果不指定为随机
system参数:如果是yes为系统组。--可选
=========================================================================================
1.创建多个play
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim play.yml
- hosts: webservers1
user: root
tasks:
- name: create a group
group: name=mygrp gid=2003 system=true
- name: create a user
user: name=tom group=mygrp system=true
- hosts: webservers2
user: root
tasks:
- name: install apache
yum: name=httpd state=latest
- name: start httpd service
service: name=httpd state=started
=========================================================================================
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QtVoiQDh-1689292067242)(https://youngfitfei.oss-cn-beijing.aliyuncs.com/img/1569479193759.png)]
检查并执行
[root@ansible ansible]# ansible-playbook --syntax-check play.yml
[root@ansible ansible]# ansible-playbook play.yml
2.条件执行when模块
先判断when条件是否成立
[root@ansible ansible]# cat /etc/ansible/hosts
[webservers1]
ansible-web1
ansible-web2
[root@ansible ansible]# vim when.yml
- hosts: webservers1
user: root
tasks:
- name: use when
file: state=touch path=/tmp/when.txt
- name: insert data
shell: echo 123 >> /tmp/when.txt #2在执行这个模块命令
when: ansible_hostname == "ansible-web1" #1.先条件执行,先判断when是否成立,如果成立则执行上面命令,ansible-web1指的是被控节点上真正的主机名称