ansible批量部署

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指的是被控节点上真正的主机名称

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值