{"id":13576064,"url":"https://github.com/lework/Ansible-roles","last_synced_at":"2025-04-05T05:30:44.852Z","repository":{"id":47054230,"uuid":"83111111","full_name":"lework/Ansible-roles","owner":"lework","description":"Ansible Roles","archived":false,"fork":false,"pushed_at":"2022-10-31T03:39:44.000Z","size":968,"stargazers_count":609,"open_issues_count":5,"forks_count":423,"subscribers_count":35,"default_branch":"master","last_synced_at":"2025-03-28T00:13:41.337Z","etag":null,"topics":["ansible","ansible-playbook","ansible-playbooks","ansible-roles","cdh5","java","k8s","k8s-cluster","mysql","roles"],"latest_commit_sha":null,"homepage":"https://ansible.leops.cn/roles/","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-25T06:18:11.000Z","updated_at":"2025-03-25T08:27:14.000Z","dependencies_parsed_at":"2023-01-20T10:34:49.253Z","dependency_job_id":null,"html_url":"https://github.com/lework/Ansible-roles","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lework%2FAnsible-roles","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lework%2FAnsible-roles/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lework%2FAnsible-roles/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lework%2FAnsible-roles/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lework","download_url":"https://codeload.github.com/lework/Ansible-roles/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247103307,"owners_count":20884023,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansible","ansible-playbook","ansible-playbooks","ansible-roles","cdh5","java","k8s","k8s-cluster","mysql","roles"],"created_at":"2024-08-01T15:01:06.713Z","updated_at":"2025-04-05T05:30:39.840Z","avatar_url":"https://github.com/lework.png","language":"HTML","funding_links":[],"categories":["HTML"],"sub_categories":[],"readme":"# **Ansible roles 怎么用？** ![GitHub](https://img.shields.io/github/license/lework/Ansible-roles) \n\n# 系统环境\nos:  `Centos 6.7 X64`\n\npython: `2.6.6`\n\n# 安装ansible和git\n\n```bash\n[root@node ~]# yum install -y ansible git\n[root@node ~]# ansible --version\nansible 2.2.1.0\n  config file = /etc/ansible/ansible.cfg\n  configured module search path = Default w/o overrides\n```\n\n## 设置ansible\n关闭主机ssh known_hosts检查\n配置文件：/etc/ansible/ansible.cfg\n```vim\n 59 host_key_checking = False\n```\n\n## 使用git 克隆ansible role\n```bash\n[root@node roles]# git clone https://github.com/lework/Ansible-roles.git /etc/ansible/roles/\nInitialized empty Git repository in /etc/ansible/roles/.git/\nremote: Counting objects: 165, done.\nremote: Compressing objects: 100% (95/95), done.\nremote: Total 165 (delta 37), reused 152 (delta 24), pack-reused 0\nReceiving objects: 100% (165/165), 26.01 KiB | 19 KiB/s, done.\nResolving deltas: 100% (37/37), done.\n[root@node roles]# ll /etc/ansible/roles/\n总用量 36\ndrwxr-xr-x 5 root root 4096 3月  11 11:54 iptables\ndrwxr-xr-x 5 root root 4096 3月  11 11:54 java\ndrwxr-xr-x 4 root root 4096 3月  11 11:54 mvn\ndrwxr-xr-x 5 root root 4096 3月  11 11:54 nodejs\ndrwxr-xr-x 5 root root 4096 3月  11 11:54 os-init\ndrwxr-xr-x 5 root root 4096 3月  11 11:54 python2.7\ndrwxr-xr-x 4 root root 4096 3月  11 11:54 repo-epel\ndrwxr-xr-x 4 root root 4096 3月  11 11:54 ruby\ndrwxr-xr-x 5 root root 4096 3月  11 11:54 supervisor\n[root@node roles]# \n```\n\n## 添加主机清单\n这里以192.168.77.130主机为例，并将其设为node2组内。\n```\n#/etc/ansible/hosts\n[node2]\n192.168.77.130 ansible_ssh_pass=123456\n```\n\n## 查看role的README.md文档\n\n```\n# Ansible Role: python2.7\n\nCentos 6.7版本中的python2.6升级到2.7\n\n## 要求\n\n此角色仅在RHEL及其衍生产品上运行。\n\n## 测试环境\n\nansible `2.2.1.0`\nos `Centos 6.7 X64`\n\n## 角色变量\n\tsoftware_files_path: \"/opt/software/\"\n\tsoftware_install_path: \"/usr/local\"\n\n\tpython_version: \"2.7.13\"\n\n\tpython_file: \"Python-{{ python_version }}.tgz\"\n\tpython_file_path: \"{{ software_files_path }}/{{ python_file }}\"\n\tpython_file_url: \"http://www.python.org/ftp/python/{{ python_version }}/Python-{{ python_version }}.tgz\"\n\n\tpip_source_url: \"https://pypi.tuna.tsinghua.edu.cn/simple\"\n\tchange_pip_source: true\n\n\n## 依赖\n\n没有\n\n## github地址\nhttps://github.com/lework/Ansible-roles/tree/master/python2.7\n\n## Example Playbook\n\n    - hosts: servers\n      roles:\n        - python2.7\n\t\t\n\t- hosts: server\n      roles:\n        - { role: python2.7, python_version: \"2.7.13\"}\n```\n\n- 上诉说明了此role在哪个环境下正常运行，其他环境暂不保证运行成功。\n- 可以自定义角色变量，达到自己想要的结果。比如想要安装python2.7.12的版本，可以在playbook中定义`python_version: \"2.7.12\"`,就可以安装2.7.12版本的python了。\n- 如果文章中有依赖，必须先安装依赖，才能运行此role。\n- 在编写Playbook时也可以参考上列的Playbook例子。\n\n## 编写playbook\n\n这里以python2.7角色为例，将为node2组内的所有主机安装python2.7服务。\n查看[Ansible Role系统环境 之【python2.7】](http://www.jianshu.com/p/2b38b943a177) 使用帮助，根据自身情况设置相应的变量，这里设置python的版本为2.7.14。\n文件路径：/etc/ansible/roles/python2.7.yml\n\n```yml\n---\n\n- hosts: node2\n  \n  roles:\n    - { role: python2.7, python_version: \"2.7.14\"}\n```\n\n## 执行playbook\n```bash\n[root@node ansible]# ansible-playbook python2.7.yml \n\nPLAY [node2] *******************************************************************\n\nTASK [setup] *******************************************************************\nok: [192.168.77.130]\n\nTASK [python2.7 : Copy python file to agent.] **********************************\nok: [192.168.77.130]\n\nTASK [python2.7 : Create software files .] *************************************\nok: [192.168.77.130]\n\nTASK [python2.7 : Download python file.] ***************************************\nchanged: [192.168.77.130]\n\nTASK [python2.7 : Check if python remote soft link is already configured.] *****\nok: [192.168.77.130]\n\nTASK [python2.7 : Check if python remote soft old link is already configured.] *\nok: [192.168.77.130]\n\nTASK [python2.7 : Ensure packages are installed.] ******************************\nok: [192.168.77.130] =\u003e (item=[u'zlib', u'zlib-devel', u'openssl', u'openssl-devel', u'python-devel', u'gcc'])\n\nTASK [python2.7 : Copy python file to agent.] **********************************\nchanged: [192.168.77.130]\n\nTASK [python2.7 : Build python.] ***********************************************\nchanged: [192.168.77.130]\n\nTASK [python2.7 : Move python old version and Create python dir soft link.] ****\nchanged: [192.168.77.130]\n\nTASK [python2.7 : Replace file python env path.] *******************************\nchanged: [192.168.77.130] =\u003e (item=/usr/sbin/iotop)\nchanged: [192.168.77.130] =\u003e (item=/usr/bin/yum)\n\nTASK [python2.7 : Check if pip is already configured.] *************************\nok: [192.168.77.130]\n\nTASK [python2.7 : Install python2.7 pip.] **************************************\nchanged: [192.168.77.130]\n [WARNING]: Consider using get_url or uri module rather than running curl\n\n\nTASK [python2.7 : Create pip config dir.] **************************************\nchanged: [192.168.77.130]\n\nTASK [python2.7 : Change pip source.] ******************************************\nchanged: [192.168.77.130]\n\nPLAY RECAP *********************************************************************\n192.168.77.130             : ok=15   changed=8    unreachable=0    failed=0 \n```\n## 查看主机是否安装了python2.7版本\n使用ad-hoc方式查看\n```bash\n[root@node ansible]# ansible node2 -m shell -a 'python -V'\n192.168.77.130 | SUCCESS | rc=0 \u003e\u003e\nPython 2.7.12\n```\n在主机上查看\n```bash\n[root@node2 ~]# python -V\nPython 2.7.12\n```\n\n至此，ansible role怎么使用大家都明白了把。如果有疑问的话，欢迎大家来QQ群【425931784 已满员】【756527917】交流\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flework%2FAnsible-roles","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flework%2FAnsible-roles","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flework%2FAnsible-roles/lists"}