可以通过git bash连接linux
关闭防火墙,禁用防火墙开机启动,并更爱selinux文件,重启
重新登录并检查禁用
getenforce
安装git
yum -y install git nss curl
下载python安装包
wget http://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
解压 tar xf Python-3.6.5.tar.xz
cd Python-3.6.5
./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-wl, -rpath /usr/local/lib"
make && make altinstall
查看pip3.6
which pip3.6
给pip3.6做软连接
ln -s /usr/local/bin/pip3.6 /usr/local/bin/pip
安装virtualenv工具
pip install virtualenv
在新用户下创建virtualenv
useradd deploy
su - deploy
virtualenv -p /usr/local/bin/python3.6 .py3-a2.5-env
cd /home/deploy/.py3-a2.5-env
which git
没有安装
切换到root安装git
su - root
安装git
yum -y install git nss curl
切换回用户deploy
su - deploy
git克隆ansible
git clone https://github.com/ansible/ansible.git
加载virtualenv环境
source /home/deploy/.py3-a2.5-env/bin/activate
安装ansible依赖包
pip install parammiko PyYAML jinja2
把ansible源代码移动到python3.6的virtualenv环境下
mv ansible .py3-a2.5-env/
cd .py3-a2.5-env/ansible/
切换到ansible到2.5版本
git checkout stable-2.5
加载
source /home/deploy/.py3-a2.5-env/ansible/hacking/envsetup -q
看是否安装成功
ansible --version
加载python 环境和ansible环境
su -deploy
source .py3-a2.5-env/bin/activate
source .py3-a2.5-env/ansible/hancking/env-setup -q
验证是否开启ansible服务
ansible-playbook --version
创建若干文件
mkdir test_playbooks
cd test_playbooks/
mkdir inventory
mkdir roles
cd inventory/
vi testenv
[testservers]
test.example.com
[testservers:vars]
server_name=test.example.com
user=root
output=/root/test.txt
cd ..
ls
cd roles/
mkdir testbox
cd testbox
mkdir tasks
cd tasks/
vim main.yml
- name: Print server name and user to remote testbox
shell: "echo 'Currently {{ user }} is logining {{ server_name}}' > {{ output}}"
输入一条数据到目标主机
回到test_playbooks
pwd
cd ../../..
pwd
vim deploy.yml (入口文件)
- host: "testservers"
gather_facts: true
remote_user: root
roles:
- testbox
tree .
创建密钥
实际操作
连接ansible服务器
su -deploy
启动py3
source .py3-a2.5-env/bin/activate
启动ansible
source .py3-a2.5-env/ansible/hacking/env-setup -q
查看ansible是否启动
ansible-playbook --version
为了推到目标服务器的任务顺利执行
ssh root@test.example.com
useradd foo
useradd deploy
mkdir /etc/nginx
给目标服务器安装nginx环境
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.e17.ngx.noarch.rpm
退出回到ansible
pwd
ls
cd test_playbooks/
ls
vi roles/testbox/tasks/main.yml
- name: create a file
file: 'path=/root/foo.txt state=touch mode=0775 owner=foo group=foo'
测试脚本是否可用
ansible-playbook -i inventory/testenv ./deploy.yml
另外的检查方式
ssh root@test.example.com ls -l /root/foo.txt
创建另一个模块
mkdir /root/testbox/files
vi roles/testbox/files/foo.sh
echo "This is a test script"
vi roles/testbox/tasks/main.yml
- name: copy a file
copy: 'remote_src=no src=roles/testbox/files/foo.sh dest=/root/foo.sh mode=0644 force=yes'
然后执行,看是否执行成功
ansible-playbook -i inventory/testenv ./deploy.yml
vi roles/testbox/tasks/main.yml
获取文件状态信息
- name: check if foo.sh exists
stat: 'path=/root/foo.sh'
register: script_stat
状态判断
- debug: msg="foo.sh exists"
when: script_stat.stat.exists
执行并查看状态
ansible-playbook -i inventory/testenv ./deploy.yml
vi roles/testbox/tasks/main.yml
执行远程脚本
- name: run the script
command: 'sh /root/foo.sh'
ansible-playbook -i inventory/testenv ./deploy.yml
vim inventory/testenv
server_name=test.example.com
port=80
user=deploy
worker_processes=4
max_open_file=65505
root=/www
mkdir roles/testbox/templates
vim roles/testbox/templates/nginx.conf.j2
vi roles/testbox/tasks/main.yml
查看执行状态
ansible-playbook -i inventory/testenv ./deploy.yml
另一种查看,然后有配置,并且替换了参数,说明成功
ssh root@test.example.com cat /etc/nginx/nginx.conf
查看nginx是否启动成功