vmvare15网络环境
nat
三台机器的系统信息
[root@ansible1 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@ansible1 ~]# uname –a
#32位操作系统,下述ansible的软件一定也要用32位,不然不能执行
Linux ansible1 2.6.32-696.el6.i686 #1 SMP Tue Mar 21 18:53:30 UTC 2017 i686 i686 i386 GNU/Linux
ansible各机器关系
ansible1为ansible控制机器
ansible2和ansible3为ansible被控制机器
ansible1的IP信息
[root@ansible1 ~]# ifconfig eth0|awk -F “[: ]+” ‘NR==2{print $4}’
10.0.0.11
[root@ansible1 ~]# ifconfig eth1|awk -F “[: ]+” ‘NR==2{print $4}’
172.16.1.11
ansible2的IP信息
[root@ansible2 ansible]# ifconfig eth0|awk -F “[: ]+” ‘NR==2{print $4}’
10.0.0.12
[root@ansible2 ansible]# ifconfig eth1|awk -F “[: ]+” ‘NR==2{print $4}’
172.16.1.12
ansible3 的IP信息
[root@ansible3 appli]# ifconfig eth0|awk -F “[: ]+” ‘NR==2{print $4}’
10.0.0.13
[root@ansible3 appli]# ifconfig eth1|awk -F “[: ]+” ‘NR==2{print $4}’
172.16.1.13
ansible1处理ansible
#安装ansible
[root@ ansible1~]# yum install -y ansible
[root@ansible1 ansible]# ansible –version
ansible 2.6.17
config file = /etc/ansible/ansible.cfg
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.6.6 (r266:84292, Aug 18 2016, 14:53:48) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
#生成密钥
[root@ ansible1~]# ssh-keygen
[root@ ansible1~]# cd .ssh/
[root@ ansible1.ssh]# ll
total 12
-rw——- 1 root root 1675 Nov 19 10:02 id_rsa
-rw-r–r– 1 root root 402 Nov 19 10:02 id_rsa.pub
-rw-r–r– 1 root root 782 Nov 18 16:46 known_hosts
#发送密钥
[root@ ansible1.ssh]# yum install -y openssl openssh-server
[root@ ansible1.ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.12
[root@ ansible1.ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.0.0.13
#测试是否能直接登录
[root@ ansible1.ssh]# ssh root@10.0.0.12
[root@ ansible1.ssh]# ssh root@10.0.0.13
ansible测试被控机器
#这个目录下的文件都不要删除
#这个是ansible安装目录,在执行ansible –version可以看到。
[root@ ansible1.ssh]# cd /etc/ansible/
[root@ ansible1 ansible]# vim hosts
#最下方加入
[jdk]
10.0.0.12
10.0.0.13
[root@ansible1 ~]# ansible jdk -m ping
10.0.0.13 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
10.0.0.12 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
ansible帮助信息
#帮助命令
[root@ ansible1~]# ansible-doc -h
#ansible系统中支持的模块
[root@ ansible1~]# ansible-doc -l
#每个模块支持的动作
[root@ ansible1~]# ansible-doc -s
#ansible-doc直接加模块名称,显示该模块的描述和使用示例
[root@ ansible1~]# ansible-doc ping
PING (/usr/lib/python2.6/site-packages/ansible/modules/system/ping.py)
#可以发现ansible模块安装到了python的目录中
[root@ ansible1~]# cd /usr/lib/python2.6/site-packages
#可以观察下各个目录下都有啥
[root@ ansible1ansible]# cd modules/
[root@ ansible1modules]# cd system/
#每个模块的一系列动作,ansible-doc -s + 模块名称列出
[root@ ansible1~]# ansible-doc -s yum
设置ansible连接的机器信息
[root@ansible1 ansible]# pwd
/etc/ansible
[root@ansible1 ansible]# cat hosts
#最下方加入
[jdk]
10.0.0.12
10.0.0.13
[tomcat]
10.0.0.12
10.0.0.13
建立目录结构
[root@ansible1 ansible]# pwd
/etc/ansible
#当目录架构建完后,是下述的基本结构
创建目录结构
#yml
[root@ansible1 ansible]# mkdir yml
#roles
[root@ ansible1 ansible]#
mkdir -pv roles/{jdkrole,tomcatrole,mainrole}/{defaults,files,handlers,meta,tasks,templates,vars}
jdk文件
[root@ansible1 files]# pwd
/etc/ansible/roles/jdkrole/files
[root@ansible1 files]# mv jdk-7u80-linux-i586.tar.gz jdk.tar.gz
[root@ansible1 files]# ll
total 151224
-rw-r–r– 1 root root 154850445 Nov 24 01:01 jdk.tar.gz
[root@ansible1 ansible]# cat yml/jdk.yml (对应下述图中的格式)
– hosts: jdk
remote_user: root
roles:
– jdkrole
– mainrole
tasks:
– name: mkdir jdk directory
file: path={{BigdataDir}} state=directory mode=0755
– name: copy and unzip jdk
unarchive: src=/etc/ansible/roles/jdkrole/files/jdk.tar.gz dest={{BigdataDir}}
– name: software link
shell: ln -s /application/jdk1.7.0_80 /application/jdk
– name: set env
lineinfile: dest=/etc/profile insertafter=”{{item.position}}” line=”{{item.value}}” state=present
with_items:
– {position: EOF, value: “export JAVA_HOME={{BigdataDir}}/jdk”}
– {position: EOF, value: “export PATH=$JAVA_HOME/bin:$PATH”}
– name: enforce env
shell: source /etc/profile
tomcat文件
[root@ansible1 files]# pwd
/etc/ansible/roles/tomcatrole/files
[root@ansible1 files]#mv apache-tomcat-7.0.69.tar.gz tomcat.tar.gz
[root@ansible1 files]# ll
total 8704
-rw-r–r– 1 root root 8910579 Nov 24 01:15 tomcat.tar.gz
[root@ansible1 ansible]# cat yml/tomcat.yml (对应下述图中的格式)
– hosts: tomcat
remote_user: root
roles:
– tomcatrole
– mainrole
tasks:
– name: mkdir tomcat directory
file: path={{BigdataDir}} state=directory mode=0755
– name: copy and unzip tomcat
unarchive: src=/etc/ansible/roles/tomcatrole/files/tomcat.tar.gz dest={{BigdataDir}}
– name: software link
shell: ln -s /application/apache-tomcat-7.0.69 /application/tomcat
– name: set env
lineinfile: dest=/etc/profile insertafter=”{{item.position}}” line=”{{item.value}}” state=present
with_items:
– {position: EOF, value: “export TOMCAT_HOME={{BigdataDir}}/tomcat”}
#- {position: EOF, value: “export PATH=$TOMCAT_HOME/bin:$PATH”}
– name: enforce env
shell: source /etc/profile
#报错 . no JAVA_HOME
#- name: start tomcat
#shell: /bin/sh /application/tomcat/bin/startup.sh
main.yml文件
[root@ansible1 ansible]# cat roles/mainrole/vars/main.yml
BigdataDir: /application
传输测试
注意:如果上述yml文件中的remote_user和ssh互信的用户是普通用户的话,那么在执行下述命令的时候也要用普通用户来执行,这三者的名称最好保持一致。yml文件中如果要copy某个目录下的文件的话,不要使用cp /XX/XX/file.*的形式,要执行多个copy命令,一个个拷贝,不然会出现拷贝错误的情况。
执行传输并验证jdk
[root@ansible1 ansible]# ansible-playbook -i hosts yml/jdk.yml
#在10.0.0.12/13验证jdk(退出当前会话再执行)
[root@ansible2 ~]# java -version
java version “1.7.0_80”
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) Client VM (build 24.80-b11, mixed mode)
[root@ansible2 ~]# cd /application/
[root@ansible2 application]# ll
lrwxrwxrwx 1 root root 24 Nov 24 01:14 jdk -> /application/jdk1.7.0_80
drwxr-xr-x 8 uucp 143 4096 Apr 11 2015 jdk1.7.0_80
[root@ansible2 application]# cat /etc/profile
export JAVA_HOME=/application/jdk
export PATH=$JAVA_HOME/bin:$PATH
[root@ansible3 ~]# java -version
java version “1.7.0_80”
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) Client VM (build 24.80-b11, mixed mode)
[root@ansible3 ~]# cd /application/
[root@ansible3 application]# ll
lrwxrwxrwx 1 root root 24 Nov 24 01:14 jdk -> /application/jdk1.7.0_80
drwxr-xr-x 8 uucp 143 4096 Apr 11 2015 jdk1.7.0_80
[root@ansible3 application]# cat /etc/profile
export JAVA_HOME=/application/jdk
export PATH=$JAVA_HOME/bin:$PATH
执行传输并验证tomcat
[root@ansible1 ansible]# ansible-playbook -i hosts yml/tomcat.yml
[root@ansible2 ~]# cd /application/
[root@ansible2 application]# ll
drwxr-xr-x 9 root root 4096 Nov 24 01:26 apache-tomcat-7.0.69
lrwxrwxrwx 1 root root 33 Nov 24 01:26 tomcat -> /application/apache-tomcat-7.0.69
[root@ansible3 application]# cat /etc/profile
export TOMCAT_HOME=/application/tomcat
[root@ansible2 application]# /bin/sh /application/tomcat/bin/startup.sh &
[root@ansible3 ~]# cd /application/
[root@ansible3 application]# ll
drwxr-xr-x 9 root root 4096 Nov 24 01:26 apache-tomcat-7.0.69
lrwxrwxrwx 1 root root 33 Nov 24 01:26 tomcat -> /application/apache-tomcat-7.0.69
[root@ansible3 application]# cat /etc/profile
export TOMCAT_HOME=/application/tomcat
[root@ansible3 application]# /bin/sh /application/tomcat/bin/startup.sh &
标题:ansible2.6以playbook组件形式安装jdk7和tomcat7
作者:yazong
地址:https://blog.llyweb.com/articles/2019/09/24/1578148496700.html