• Stars
    star
    520
  • Rank 85,117 (Top 2 %)
  • Language
    HTML
  • License
    MIT License
  • Created almost 8 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ansible Roles

Ansible roles 怎么用? GitHub

系统环境

os: Centos 6.7 X64

python: 2.6.6

安装ansible和git

[root@node ~]# yum install -y ansible git
[root@node ~]# ansible --version
ansible 2.2.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

设置ansible

关闭主机ssh known_hosts检查 配置文件:/etc/ansible/ansible.cfg

 59 host_key_checking = False

使用git 克隆ansible role

[root@node roles]# git clone https://github.com/lework/Ansible-roles.git /etc/ansible/roles/
Initialized empty Git repository in /etc/ansible/roles/.git/
remote: Counting objects: 165, done.
remote: Compressing objects: 100% (95/95), done.
remote: Total 165 (delta 37), reused 152 (delta 24), pack-reused 0
Receiving objects: 100% (165/165), 26.01 KiB | 19 KiB/s, done.
Resolving deltas: 100% (37/37), done.
[root@node roles]# ll /etc/ansible/roles/
总用量 36
drwxr-xr-x 5 root root 4096 3月  11 11:54 iptables
drwxr-xr-x 5 root root 4096 3月  11 11:54 java
drwxr-xr-x 4 root root 4096 3月  11 11:54 mvn
drwxr-xr-x 5 root root 4096 3月  11 11:54 nodejs
drwxr-xr-x 5 root root 4096 3月  11 11:54 os-init
drwxr-xr-x 5 root root 4096 3月  11 11:54 python2.7
drwxr-xr-x 4 root root 4096 3月  11 11:54 repo-epel
drwxr-xr-x 4 root root 4096 3月  11 11:54 ruby
drwxr-xr-x 5 root root 4096 3月  11 11:54 supervisor
[root@node roles]# 

添加主机清单

这里以192.168.77.130主机为例,并将其设为node2组内。

#/etc/ansible/hosts
[node2]
192.168.77.130 ansible_ssh_pass=123456

查看role的README.md文档

# Ansible Role: python2.7

Centos 6.7版本中的python2.6升级到2.7

## 要求

此角色仅在RHEL及其衍生产品上运行。

## 测试环境

ansible `2.2.1.0`
os `Centos 6.7 X64`

## 角色变量
	software_files_path: "/opt/software/"
	software_install_path: "/usr/local"

	python_version: "2.7.13"

	python_file: "Python-{{ python_version }}.tgz"
	python_file_path: "{{ software_files_path }}/{{ python_file }}"
	python_file_url: "http://www.python.org/ftp/python/{{ python_version }}/Python-{{ python_version }}.tgz"

	pip_source_url: "https://pypi.tuna.tsinghua.edu.cn/simple"
	change_pip_source: true


## 依赖

没有

## github地址
https://github.com/lework/Ansible-roles/tree/master/python2.7

## Example Playbook

    - hosts: servers
      roles:
        - python2.7
		
	- hosts: server
      roles:
        - { role: python2.7, python_version: "2.7.13"}
  • 上诉说明了此role在哪个环境下正常运行,其他环境暂不保证运行成功。
  • 可以自定义角色变量,达到自己想要的结果。比如想要安装python2.7.12的版本,可以在playbook中定义python_version: "2.7.12",就可以安装2.7.12版本的python了。
  • 如果文章中有依赖,必须先安装依赖,才能运行此role。
  • 在编写Playbook时也可以参考上列的Playbook例子。

编写playbook

这里以python2.7角色为例,将为node2组内的所有主机安装python2.7服务。 查看Ansible Role系统环境 之【python2.7】 使用帮助,根据自身情况设置相应的变量,这里设置python的版本为2.7.14。 文件路径:/etc/ansible/roles/python2.7.yml

---

- hosts: node2
  
  roles:
    - { role: python2.7, python_version: "2.7.14"}

执行playbook

[root@node ansible]# ansible-playbook python2.7.yml 

PLAY [node2] *******************************************************************

TASK [setup] *******************************************************************
ok: [192.168.77.130]

TASK [python2.7 : Copy python file to agent.] **********************************
ok: [192.168.77.130]

TASK [python2.7 : Create software files .] *************************************
ok: [192.168.77.130]

TASK [python2.7 : Download python file.] ***************************************
changed: [192.168.77.130]

TASK [python2.7 : Check if python remote soft link is already configured.] *****
ok: [192.168.77.130]

TASK [python2.7 : Check if python remote soft old link is already configured.] *
ok: [192.168.77.130]

TASK [python2.7 : Ensure packages are installed.] ******************************
ok: [192.168.77.130] => (item=[u'zlib', u'zlib-devel', u'openssl', u'openssl-devel', u'python-devel', u'gcc'])

TASK [python2.7 : Copy python file to agent.] **********************************
changed: [192.168.77.130]

TASK [python2.7 : Build python.] ***********************************************
changed: [192.168.77.130]

TASK [python2.7 : Move python old version and Create python dir soft link.] ****
changed: [192.168.77.130]

TASK [python2.7 : Replace file python env path.] *******************************
changed: [192.168.77.130] => (item=/usr/sbin/iotop)
changed: [192.168.77.130] => (item=/usr/bin/yum)

TASK [python2.7 : Check if pip is already configured.] *************************
ok: [192.168.77.130]

TASK [python2.7 : Install python2.7 pip.] **************************************
changed: [192.168.77.130]
 [WARNING]: Consider using get_url or uri module rather than running curl


TASK [python2.7 : Create pip config dir.] **************************************
changed: [192.168.77.130]

TASK [python2.7 : Change pip source.] ******************************************
changed: [192.168.77.130]

PLAY RECAP *********************************************************************
192.168.77.130             : ok=15   changed=8    unreachable=0    failed=0 

查看主机是否安装了python2.7版本

使用ad-hoc方式查看

[root@node ansible]# ansible node2 -m shell -a 'python -V'
192.168.77.130 | SUCCESS | rc=0 >>
Python 2.7.12

在主机上查看

[root@node2 ~]# python -V
Python 2.7.12

至此,ansible role怎么使用大家都明白了把。如果有疑问的话,欢迎大家来QQ群【425931784 已满员】【756527917】交流

More Repositories

1

RedisDesktopManager-Windows

RedisDesktopManager Windows版本
2,133
star
2

kainstall

Use shell scripts to install kubernetes(k8s) high availability clusters and addon components based on kubeadmin with one click.使用shell脚本基于kubeadmin一键安装kubernetes 高可用集群和addon组件。
Shell
901
star
3

lenav

一个简便的公司内部网址导航站,省去到处找服务地址烦恼......
Vue
147
star
4

lemonitor

列出开源软件的中国境内镜像站点 (List of mirror sites in China for open source software)
Vue
58
star
5

cloudflare-workers

Cloudflare Worker scripts
JavaScript
48
star
6

script

存放一些使用过的脚本
PowerShell
43
star
7

kainstall-offline

kainstall tools offline file
Shell
40
star
8

kubeadm-certs

kubeadm-certs sets the validity period of kubeadm certificate to 10 years. kubeadm-certs 将 kubeadm 的证书有效期设置为10年。
37
star
9

jenkins-update-center

Jenkins mirror update center generator
Python
32
star
10

reference

专为运维人员而分享的速查表(备忘单),目的是为了方便运维小伙伴在日常工作中查阅。
EJS
30
star
11

skopeo-binary

Generate skopeo binaries.
23
star
12

Ansible-dev

ansible开发
Python
22
star
13

Docker-compose-file

记录使用过的Docker-compose
Shell
20
star
14

lework.github.io

personal blog
SCSS
17
star
15

sync_image

Synchronize container image
Python
16
star
16

kubernetes-manual

kubernetes 安装手册
Shell
15
star
17

leversion

列出开源软件的最新版本 (List the latest version of open source software)
Vue
13
star
18

kubectl-check

用于检查deployment的所有pod是否就绪的kubectl插件
Shell
10
star
19

fr

事故记录demo
Python
8
star
20

kube-api

聚合 Kubernetes API 版本信息,方便查询在 Kubernetes 版本 中可用的 API 版本。
Vue
8
star
21

grafana-lenav-panel

A panel to display website navigation. (grafana panel plugin)
TypeScript
7
star
22

lesec

基于openresty的lua简单防火墙
Lua
6
star
23

ci-demo-springboot

ci demo springboot
Java
2
star
24

ci-demo-go

ci demo go
Go
2
star
25

lework

1
star
26

Docker-kubectl

kubectl client image
Dockerfile
1
star
27

ci-demo-vue

ci demo vue
Vue
1
star