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
检查并执行
[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指的是被控节点上真正的主机名称
执行
[root@ansible ansible]# ansible-playbook when.yml
[root@ansible-web1 ~]# cat /tmp/when.txt
123
[root@ansible-web2 ~]# cat /tmp/when.txt
3.使用变量并不显示搜集主机相关信息
gather_facts参数:指定了在任务部分执行前,是否先执行setup模块获取主机相关信息,默认值为true,改成false之后在执行过程中不会搜集主机相关信息。
[root@ansible ansible]# vim create_user.yml
- hosts: ansible-web1
user: root
gather_facts: false #是否执行setup模块,搜集对方机器的信息
vars: #自定义变量
- user: "jack" #user是自定义变量名称,“jack”是变量值
- src_path: "/root/a.txt" #同上
- dest_path: "/mnt/"
tasks:
- name: create user
user: name={{ user }}
- name: copy file
copy: src={{ src_path }} dest={{ dest_path }}
[root@ansible ansible]# vim /root/a.txt #创建测试文件
123
执行:
[root@ansible ansible]# ansible-playbook create_user.yml
项目实战:通过ansible 批量部署Jdk+Tomcat
[root@ansible-server src]# cat tomcat.yml
- hosts: webservers
user: root
tasks:
- name: configure Jdk1.8
copy: src=/usr/src/jdk-8u211-linux-x64.tar.gz dest=/usr/src
- name: unzip
shell: tar -xvzf /usr/src/jdk-8u211-linux-x64.tar.gz -C /usr/local
- name: rename to java
shell: mv /usr/local/jdk1.8.0_211 /usr/local/java
- name: configure envirement1
shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile
- name: configure envirement2
shell: echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
- name: copy tomcat
copy: src=/usr/src/apache-tomcat-8.5.45.tar.gz dest=/usr/src
- name: unzip tomcat
shell: tar -xvzf /usr/src/apache-tomcat-8.5.45.tar.gz -C /usr/local
- name: rename to tomcat
shell: mv /usr/local/apache-tomcat-8.5.45 /usr/local/tomcat
- name: copy startup file
copy: src=/usr/src/startup.sh dest=/usr/local/tomcat/bin
notify: start tomcat
handlers:
- name: start tomcat
shell: nohup /usr/local/tomcat/bin/startup.sh &
[root@java-server src]# ls
apache-tomcat-8.5.45 debug kernels tomcat.retry
apache-tomcat-8.5.45.tar.gz jdk-8u211-linux-x64.tar.gz startup.sh tomcat.yml
[root@java-server src]# head -2 startup.sh
#!/bin/sh
source /etc/profile