安装和使用ansible———附带详细步骤
原创
文章目录
安装和使用ansible
1 安装ansible
1.1使用pip安装
首先,我们需要安装一个python-pip包,安装完成以后,则直接使用pip命令来安装我们的包,具体操作过程如下:
$ yum install python-pip
$ pip install ansible
1.2 使用 yum 安装
yum 安装是我们很熟悉的安装方式了。我们需要先安装一个epel-release包,然后再安装我们的 ansible 即可,具体操作过程如下:
$ yum install epel-release -y
$ yum install ansible –y
查看是否安装成功:
2 配置ansible
使用yum安装,无需创建文件夹:
修改配置文件:
修改管理主机:
添加如下内容:
[local]
10.20.3.75
[remote]
10.20.3.72
10.20.3.73
10.20.3.74
3 测试ansible是否正常运行
执行:
$ cd cd /etc/ansible/
$ root ansible -i ./hosts --connection=local local -m ping
输出:
10.20.3.75 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
执行:
$ root ansible -i ./hosts remote -m ping
输出:
10.20.3.73 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.20.3.74 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
10.20.3.72 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
4 分发脚本执行
创建脚本:
内容如下:
#!bin/sh
touch ansibleTest.log
echo "Test ansible file." > /software/ansibleTest.log
分发脚本:
$ ansible remote -m copy -a "src=testAnsible.sh dest=/software/testAnsible.sh"
执行脚本:
$ ansible remote -m shell -a "sh /software/testAnsible.sh chdir=/software"
执行成功后,会在/software路径下创建ansibleTest.log文件,并且内容为Test ansible file.
