centos 6.4 上安装ansible
来源:互联网 发布:崩坏3矩阵重置 编辑:程序博客网 时间:2024/05/17 07:47
1.控制服务器与被管理服务器要求
Master:Python 2.6+
Slave:Python 2.4+,最好也是2.6+(CentOS 6/7自带)
2.使用yum安装ansible
yum install ansible
如果提示没有此安装包,可以加入EPEL扩展包:
yum install epel-release
这是再执行yum install ansible会有如下报错:
-----报错1:
[root@ansible ~]# yum install ansible
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
-----解决方案:
然后修改编辑/etc/yum.repos.d/epel.repo
将baseurl的注释取消, mirrorlist注释掉。即可
然后 yum clean all
最后还是不行的话,重启下network
----报错2:
[Errno 14] problem making ssl connection Trying other mirror.
-----解决方案:
参考:http://www.178linux.com/11795
因为curl版本太低,升级curl版本 yum update curl
3. ansible --version 安装完成后查看版本
4. 配置无密码访问
在服务器端执行:
mkdir ~/.ssh
ssh-keygen -t rsa
在.ssh目录上会生成两个文件:
私钥id_rsa 公钥id_rsa.pub
将公钥拷到客户端端/root/.ssh下并
scp id_rsa.pub root@10.0.2.1:/root/.ssh
登录到客户端执行:cat id_rsa.pub >> authorized_keys
chmod 600 authorized_keys
chmod 700 /home/weblogic/.ssh
5.客户端python升级
tar zxvf Python-2.7.11.tgz
cd Python-2.7.11
mkdir /usr/local/python27
./configure --prefix=/usr/local/python27
make
make install
mv /usr/bin/python /usr/bin/python_2.4
ln -s /usr/local/python27/bin/python /usr/bin/python
vi /usr/bin/yum
将第一行#!/usr/bin/python 改成#!/usr/bin/python_2.4
6.ansible的日常使用
ansible-doc -l 命令查看到当前 ansible 都支持哪些模块
ansible-doc -s 模块名 又可以查看该模块有哪些参数可以使用
常用的模块:
---copy模块
把主控端/root目录下的simplejson-3.5.2.tar.gz文件拷贝到到指定节点上
ansible 10.0.224.1 -m copy -a 'src=/root/simplejson-3.5.2.tar.gz dest=/home/weblogic'
---file模块
更改指定节点上/home/weblogic/simplejson-3.5.2.tar.gz的权限为755,属主和属组为weblogic
ansible 10.0.224.1 -m file -a 'dest=/home/weblogic/simplejson-3.5.2.tar.gz mode=755 owner=weblogic group=weblogic'
---cron模块
在指定节点上新建crontab的任务,其中name的意义是会在crontab -l中添加一行注释,对任务的描述。
user表明对哪个的用户的crontab任务进行添加。
ansible 10.0.224.1 -m cron -a 'name="ntpetest" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 10.0.224.1" user=weblogic'
---group模块
在指定节点上创建一个组名为nolinux,gid为2014的组
ansible 10.0.224.1 -m goup -a 'gid=2014 name=nolinux'
---user模块
在指定节点上创建一个用户名为nolinux,组为nolinux的用户
ansible 10.0.224.1 -m user -a 'name=nolinux groups=nolinux state=present'
---yum模块
在指定节点上安装 lrzsz 服务
ansible 10.0.224.1 -m yum -a "state=present name=httpd"
---service模块
启动指定节点上的 vsftpd 服务,并让其开机自启动
ansible 10.0.224.1 -m service -a 'name=vsftpd state=restarted '
---script模块
在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的,ansible服务本地的)
ansible 10.0.224.1 -m script -a '/root/a.sh'
---ping模块
检查指定节点机器是否还能连通
ansible 10.0.224.1 -m ping
---command模块
comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 "<", ">", "|", and "&"操作都不可以,当然,也就不支持管道
在使用ansible中的时候,默认的模块是-m command,从而模块的参数不需要填写,直接使用即可。
ansible 10.0.224.1 -m command -a 'hostname'
ansible 10.0.224.1 -m command -a 'ls -l'
ansible 10.0.224.1 -m command -a 'pwd'
---shell模块
运行指定节点上的shell脚本
ansible 10.0.224.1 -m shell -a './a.sh chdir=/root'
ansible 10.1.216.217 -m shell -a './Start.sh chdir=/app/mid/weblogic_home/user_projects/domains/dwluatdomain/bin'
ansible 10.0.224.1 -m shell -a './Restart_all.sh chdir=/app/mid/weblogic_home/user_projects/domains/ttnj_domain/bin'
synchronize模块
7. handlers
Handlers: 在发生改变时执行的操作
当一个文件的内容被改动时,重启两个 services:
- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
‘notify’ 下列出的即是 handlers.
Handlers 也是一些 task 的列表,通过名字来引用,它们和一般的 task 并没有什么区别.Handlers 是由通知者进行 notify, 如果没有被 notify, handlers 不会执行.
不管有多少个通知者进行了 notify,等到 play 中的所有 task 执行完成之后,handlers 也只会被执行一次.
Handlers 最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了.
handlers:
- name: restart memcached
service: name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted
Master:Python 2.6+
Slave:Python 2.4+,最好也是2.6+(CentOS 6/7自带)
2.使用yum安装ansible
yum install ansible
如果提示没有此安装包,可以加入EPEL扩展包:
yum install epel-release
这是再执行yum install ansible会有如下报错:
-----报错1:
[root@ansible ~]# yum install ansible
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
-----解决方案:
然后修改编辑/etc/yum.repos.d/epel.repo
将baseurl的注释取消, mirrorlist注释掉。即可
然后 yum clean all
最后还是不行的话,重启下network
----报错2:
[Errno 14] problem making ssl connection Trying other mirror.
-----解决方案:
参考:http://www.178linux.com/11795
因为curl版本太低,升级curl版本 yum update curl
3. ansible --version 安装完成后查看版本
4. 配置无密码访问
在服务器端执行:
mkdir ~/.ssh
ssh-keygen -t rsa
在.ssh目录上会生成两个文件:
私钥id_rsa 公钥id_rsa.pub
将公钥拷到客户端端/root/.ssh下并
scp id_rsa.pub root@10.0.2.1:/root/.ssh
登录到客户端执行:cat id_rsa.pub >> authorized_keys
chmod 600 authorized_keys
chmod 700 /home/weblogic/.ssh
5.客户端python升级
tar zxvf Python-2.7.11.tgz
cd Python-2.7.11
mkdir /usr/local/python27
./configure --prefix=/usr/local/python27
make
make install
mv /usr/bin/python /usr/bin/python_2.4
ln -s /usr/local/python27/bin/python /usr/bin/python
vi /usr/bin/yum
将第一行#!/usr/bin/python 改成#!/usr/bin/python_2.4
6.ansible的日常使用
ansible-doc -l 命令查看到当前 ansible 都支持哪些模块
ansible-doc -s 模块名 又可以查看该模块有哪些参数可以使用
常用的模块:
---copy模块
把主控端/root目录下的simplejson-3.5.2.tar.gz文件拷贝到到指定节点上
ansible 10.0.224.1 -m copy -a 'src=/root/simplejson-3.5.2.tar.gz dest=/home/weblogic'
---file模块
更改指定节点上/home/weblogic/simplejson-3.5.2.tar.gz的权限为755,属主和属组为weblogic
ansible 10.0.224.1 -m file -a 'dest=/home/weblogic/simplejson-3.5.2.tar.gz mode=755 owner=weblogic group=weblogic'
---cron模块
在指定节点上新建crontab的任务,其中name的意义是会在crontab -l中添加一行注释,对任务的描述。
user表明对哪个的用户的crontab任务进行添加。
ansible 10.0.224.1 -m cron -a 'name="ntpetest" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 10.0.224.1" user=weblogic'
---group模块
在指定节点上创建一个组名为nolinux,gid为2014的组
ansible 10.0.224.1 -m goup -a 'gid=2014 name=nolinux'
---user模块
在指定节点上创建一个用户名为nolinux,组为nolinux的用户
ansible 10.0.224.1 -m user -a 'name=nolinux groups=nolinux state=present'
---yum模块
在指定节点上安装 lrzsz 服务
ansible 10.0.224.1 -m yum -a "state=present name=httpd"
---service模块
启动指定节点上的 vsftpd 服务,并让其开机自启动
ansible 10.0.224.1 -m service -a 'name=vsftpd state=restarted '
---script模块
在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的,ansible服务本地的)
ansible 10.0.224.1 -m script -a '/root/a.sh'
---ping模块
检查指定节点机器是否还能连通
ansible 10.0.224.1 -m ping
---command模块
comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 "<", ">", "|", and "&"操作都不可以,当然,也就不支持管道
在使用ansible中的时候,默认的模块是-m command,从而模块的参数不需要填写,直接使用即可。
ansible 10.0.224.1 -m command -a 'hostname'
ansible 10.0.224.1 -m command -a 'ls -l'
ansible 10.0.224.1 -m command -a 'pwd'
---shell模块
运行指定节点上的shell脚本
ansible 10.0.224.1 -m shell -a './a.sh chdir=/root'
ansible 10.1.216.217 -m shell -a './Start.sh chdir=/app/mid/weblogic_home/user_projects/domains/dwluatdomain/bin'
ansible 10.0.224.1 -m shell -a './Restart_all.sh chdir=/app/mid/weblogic_home/user_projects/domains/ttnj_domain/bin'
synchronize模块
7. handlers
Handlers: 在发生改变时执行的操作
当一个文件的内容被改动时,重启两个 services:
- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
‘notify’ 下列出的即是 handlers.
Handlers 也是一些 task 的列表,通过名字来引用,它们和一般的 task 并没有什么区别.Handlers 是由通知者进行 notify, 如果没有被 notify, handlers 不会执行.
不管有多少个通知者进行了 notify,等到 play 中的所有 task 执行完成之后,handlers 也只会被执行一次.
Handlers 最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了.
handlers:
- name: restart memcached
service: name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted
阅读全文
0 0
- centos 6.4 上安装ansible
- 在CentOS 7.3 上安装Ansible
- 如何在CentOS 6上安装Python 2.7 + Ansible
- Centons7上安装Ansible
- centos 7 ansible安装篇
- centos安装ansible及其依赖包
- CentOS release 6.5 (Final) 安装ansible
- 在 CentOS 7 中安装并使用自动化工具 Ansible
- ansible 安装
- ansible安装
- ansible 安装
- Ansible安装
- ansible安装
- Ansible安装
- ansible安装
- ansible安装
- ansible安装
- Ansible安装
- Codeforces Round #428 (Div. 2) A. Arya and Bran
- 我的第一篇博客
- 258. Add Digits
- string.find()
- 【持续更新】微电子专业术语常用缩写英汉对照
- centos 6.4 上安装ansible
- 2017"百度之星"程序设计大赛
- Sensor板改板要求
- Angularjs系列之基本概念
- Java四类八种数据类型
- hadoop分布式集群hive-mysql的安装配置
- jdk环境变量配置
- 正则表达式的规则
- SAP推出SAP Digital Boardroom