[[email protected]:/etc]# tree /etc/ansible/ /etc/ansible/ ├── ansible.cfg ├── hosts ├── python.yml └── roles └── python_install ├── files │ └── Python-3.6.6.tgz ├── tasks │ ├── copy.yml │ ├── install.yml │ └── main.yml └── templates └── python_install.sh 说明: files:存放需要同步到异地服务器的源码文件及配置文件; handlers:当资源发生变化时需要进行的操作,若没有此目录可以不建或为空; meta:角色定义可留空; tasks:python安装过程成需要进行的执行的任务; templates:用于执行python安装的模板文件,一般为脚本; vars:本次安装定义的变量,若无必要可以不建 python3.6.6源码存放目录: python_install/files/Python-3.6.6.tgz 具体操作 1.创建python角色文件,用于调用python_install [[email protected]:/etc/ansible]# cat /etc/ansible/python.yml --- - hosts: all remote_user: root roles: - python_install 2.创建任务文件 [[email protected]:/etc/ansible/roles]# cat python_install/tasks/copy.yml - name: copy python_tgz to client copy: src=/etc/ansible/roles/python_install/files/Python-3.6.6.tgz dest=/usr/local/src/ - name: copy install_python_script to client copy: src=/etc/ansible/roles/python_install/templates/python_install.sh dest=/tmp/python_install.sh owner=root group=root mode=755 [[email protected]:/etc/ansible/roles]# cat python_install/tasks/install.yml - name: install python shell: /bin/bash /tmp/python_install.sh [[email protected]:/etc/ansible/roles]# cat python_install/tasks/main.yml - include: copy.yml - include: install.yml 注意: a.copy如果复制目录,需要加上递归参数,recurse; b.copy如果复制目录,没有目录将会在目标服务器上创建; c.copy如果复制文件到目标服务器的某一个目录下,需要在dest参数上加上/usr/local/src/,而不是/usr/local/src,否则ansible将会把文件复制为src,而不是放在src目录下。 4.编写模板脚本 [[email protected]:/etc/ansible/roles]# cat python_install/templates/python_install.sh #!/bin/bash # install python3.6.6 # yum tools yum -y groupinstall "Development tools" yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel cd /usr/local/src mkdir /usr/local/python3 tar -zxf Python-3.6.6.tgz cd Python-3.6.6 ./configure --prefix=/usr/local/python3 make && make install ln -s /usr/local/python3/bin/python3 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 # end 安装脚本功能: 1)安装yum依赖包 2)创建目录,解压文件并编译安装到目标目录 3)生成软连接 执行playbook 测试 [[email protected]:/etc/ansible]# ansible-playbook -C python.yml 执行 [[email protected]:/etc/ansible]# ansible-playbook python.yml
原文地址:https://www.cnblogs.com/reblue520/p/9385354.html
时间: 2024-12-13 16:32:44