ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ansible-galaxy 介绍

2022-02-09 12:35:14  阅读:287  来源: 互联网

标签:roles 介绍 ansible nfs 172.16 root yml galaxy


一:介绍 

Ansible 的 Galaxy 工具,类似程序员使用的 github,docker 镜像仓库,yum仓库和deb仓库等。可以将自己编写的 Role 通过 Galaxy 这个平台进行分享。同样,我们也可以通过 Galaxy 这个平台去获取一些我们想要的 Role

Galaxy 官网:https://galaxy.ansible.com

ansible-galaxy 则是一个使用 Galaxy 命令行的工具。

二进制文件一般不放到角色中管理,因此我们下载的角色基本都是文本文件
galaxy有银河、星系的意思,没啥特殊含义,就是起了这么个名字

1.获取帮助信息

[root@jettoloader test]# ansible-galaxy -h
usage: ansible-galaxy [-h] [--version] [-v] TYPE ...

Perform various Role and Collection related operations.

positional arguments:
  TYPE
    collection   Manage an Ansible Galaxy collection.
    role         Manage an Ansible Galaxy role.

optional arguments:
  --version      show program's version number, config file location,
                 configured module search path, module location, executable
                 location and exit
  -h, --help     show this help message and exit
  -v, --verbose  verbose mode (-vvv for more, -vvvv to enable connection
                 debugging)

1.1 ansible-galaxy role:        

[root@jettoloader test]# ansible-galaxy role -h
usage: ansible-galaxy role [-h] ROLE_ACTION ...

positional arguments:
  ROLE_ACTION
    init       Initialize new role with the base structure of a role. 初始化新角色的基本结构
    remove     Delete roles from roles_path.从角色路径中删除角色。
    delete     Removes the role from Galaxy. It does not remove or alter the
               actual GitHub repository. 从Galaxy中删除角色。它不会删除或更改实际的GitHub存储库
    list       Show the name and version of each role installed in the
               roles_path.查看角色列表
    search     Search the Galaxy database by tags, platforms, author and
               multiple keywords.搜索角色
    import     Import a role.导入角色
    setup      Manage the integration between Galaxy and the given source.管理Galaxy和给定源之间的集成
    info       View more details about a specific role.查看有关特定角色的详细信息
    install    Install role(s) from file(s), URL(s) or Ansible Galaxy.从文件、URL或Ansible Galaxy安装角色

optional arguments:
  -h, --help   show this help message and exit

 1.2 ansible-galaxy collection: 

[root@jettoloader test]# ansible-galaxy collection -h
usage: ansible-galaxy collection [-h] COLLECTION_ACTION ...

positional arguments:
  COLLECTION_ACTION
    init             Initialize new collection with the base structure of a
                     collection.使用集合的基本结构初始化新集合
    build            Build an Ansible collection artifact that can be publish
                     to Ansible Galaxy.构建一个可发布到Ansible Galaxy的Ansible集合工件
    publish          Publish a collection artifact to Ansible Galaxy.向Ansible Galaxy发布一个集合工件
    install          Install collection(s) from file(s), URL(s) or Ansible
                     Galaxy.从文件、URL或Ansible Galaxy安装集合

optional arguments:
  -h, --help         show this help message and exit

1.3 Collections 和role关系:

       Collections是Ansible比较新的版本引入的一个概念,它是roles的集合,比如我们可以把很多相关的roles定义成Collections。以下在galaxy 可以看到

Collections:

 roles:

大家执行下载命令的时候也可以看到,基本上都是到github上去下载的,毕竟这些roles只是一组文本文件,文件大小都不大,我们也可以自己去github上下载并解压,不过那样还是有些麻烦。

1.4 常用指令

1.4.1)在 galaxy 上搜索共享的 role

[root@jettoloader test]# ansible-galaxy search nginx
 
Found 1641 roles matching your search. Showing first 1000.

 Name                                                    Description
 ----                                                    -----------
 0x0i.prometheus                                         Prometheus - a multi-dimensional time-series data monitoring and alerting toolkit
 0x5a17ed.ansible_role_netbox                            Installs and configures NetBox, a DCIM suite, in a production setting.
 1davidmichael.ansible-role-nginx                        Nginx installation for Linux, FreeBSD and OpenBSD.
 1it.sudo                                                Ansible role for managing sudoers

1.4.2)安装 galaxy 上共享的 role

[root@jettoloader test]# ansible-galaxy role install -v -p ./ nginx

参考资料

ansible详解之部署简介和使用_码农崛起-CSDN博客

https://docs.ansible.com/ansible/latest/galaxy/user_guide.html
https://galaxy.ansible.com

http://www.ansible.com.cn/docs/galaxy.html

二 案例 nfs

2.1 主机规划

主机名称操作系统版本内网IP安装软件
172.16.10.21CentOS7.5172.16.10.21ansible
172.16.10.15CentOS7.5172.16.10.15不用安装
172.16.10.5CentOS7.5172.16.10.5不用安装

2.2 添加用户账号

说明:

1、 运维人员使用的登录账号;

2、 所有的业务都放在 /app/jettech 下「jettech用户的家目录」,避免业务数据乱放;

3、 该用户也被 ansible 使用,因为几乎所有的生产环境都是禁止 root 远程登录的(因此该 jettech用户也进行了 sudo 提权)。

# 使用一个专门的用户,避免直接使用root用户
# 添加用户、指定家目录并指定用户密码
# sudo提权
# 让其它普通用户可以进入该目录查看信息


[root@jettoloader k3s-ansible-master]# mkdir /app
[root@jettoloader k3s-ansible-master]# useradd -u 1050  -d /app/jettech -m jettech 
[root@jettoloader k3s-ansible-master]# echo '123456aA' | /usr/bin/passwd --stdin jettech
Changing password for user jettech.
passwd: all authentication tokens updated successfully.
[root@jettoloader k3s-ansible-master]# echo "jettech  ALL=(ALL)       NOPASSWD: ALL" >>  /etc/sudoers
[root@jettoloader k3s-ansible-master]# chmod 755 /app

2.3 Ansible 配置清单Inventory

 之后文章都是如下主机配置清单

[root@jettoloader jettech]# cd /app/jettech/
[root@jettoloader jettech]# mkdir ansible

[root@jettoloader ansible]# pwd
/app/jettech/ansible
[root@jettoloader ansible]# cat hosts.ini 
[master]
#172.16.10.5  ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
#方式1 别名 + 主机 + 端口 + 密码
#web01 ansible_ssh_host=172.16.1.183 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
#web02 ansible_ssh_host=172.16.1.184 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
#web03 ansible_ssh_host=172.16.1.185 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
# 方式2、主机 + 端口 + 密钥
172.16.10.5:22 

#[master:vars]

[node]
#172.16.10.15 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
172.16.10.15
172.16.10.21

[k3s_cluster:children]
master
node

[registry]
172.16.10.21

2.4 Ansible Roles 基本概述

前面已经学习了 变量、tasks 和 handlers,那怎样组织 playbook 才是最好的方式呢?

使用 roles。roles 基于一个已知的文件结构,去自动的加载某些 vars_files,tasks 以及 handlers。以便 playbook 更好的调用。相比 playbook,roles 的结构更加的清晰有层次。

2.5 Roles 目录结构

在 roles 目录下,可以使用如下命令创建目录

[root@jettoloader ansible]# mkdir ansible_roles
[root@jettoloader ansible]# cd ansible_roles/
[root@jettoloader ansible_roles]# ansible-galaxy role init roles/nfs

[root@jettoloader ansible]# tree 
.
├── ansible_roles
│   └── roles 
│       └── nfs                       #角色名称
│           ├── defaults              # 角色默认变量(最低优先级)
│           │   └── main.yml
│           ├── files                 # 文件存放
│           ├── handlers              # 触发任务
│           │   └── main.yml
│           ├── meta                  # 依赖关系
│           │   └── main.yml
│           ├── README.md             # 使用说明
│           ├── tasks                 # 具体任务
│           │   └── main.yml 
│           ├── templates             # 模板文件
│           ├── tests                 # 本role测试
│           │   ├── inventory
│           │   └── test.yml
│           └── vars                  # 角色其他变量
│               └── main.yml
└── hosts.ini                         # 主机信息

目录说明:

1、首先要有 roles 目录,然后在 roles 目录下创建相应的目录。

2、roles 下的目录名最好见文知意,如 common 目录表示基础目录,是必要的;nfs 目录表示安装 nfs 服务;redis目录表示安装 redis服务;等等。

3、可以根据自身需要创建 roles 下的二级目录,不需要的目录可以不创建,没需要全目录创建。

4、roles 目录下的二级目录中,有些目录必须包含一个 main.yml 文件,以便 ansible 使用。

2.6 Roles 依赖关系

roles 允许在使用 role 时自动引入其他 role。roles 的依赖关系存储在 role 目录中的 meta/main.yml 文件中。

例如:安装 nfs是需要先确保 nginx和 mysql都能正常运行,此时都可以在 nfs的 role 中定义依赖 nginx和mysql 的 role。

[root@jettoloader ansible]# cat ansible_roles/roles/nfs/meta/main.yml 
---
dependencies:
  - { role: nginx }
  - { role: mysql }

此时 nfs 的 role 会先执行 nginx的 role,然后执行mysql 的 role,最后再执行 nfs 本身的 role。

2.7 Ansible Roles 案例-部署 NFS 服务

2.7.1)整体目录结构

[root@jettoloader ansible_roles]# pwd
/app/jettech/ansible/ansible_roles
[root@jettoloader ansible_roles]# ll
total 4
-rw-r--r--  1 root root 55 Feb  9 10:39 all.yml
drwxr-xr-x. 2 root root 41 Feb  9 10:31 group_vars
drwxr-xr-x  3 root root 17 Feb  9 10:14 roles
[root@jettoloader ansible_roles]# tree
.
├── all.yml                         
├── group_vars                      #全局变量
│   └── all.yml
└── roles
    ├── nfs                         # 服务端
    │   ├── defaults
    │   │   └── main.yml
    │   ├── files
    │   ├── handlers
    │   │   └── main.yml
    │   ├── meta
    │   │   └── main.yml
    │   ├── README.md
    │   ├── tasks
    │   │   ├── config.yml
    │   │   ├── install.yml
    │   │   ├── main.yml
    │   │   ├── mkdir.yml
    │   │   ├── start_NFS.yml
    │   │   └── start_rpcbind.yml
    │   ├── templates
    │   │   └── exports.j2
    │   ├── tests
    │   │   ├── inventory
    │   │   └── test.yml
    │   └── vars
    │       └── main.yml
    └── nfs_client                      #客户端
        ├── defaults
        │   └── main.yml
        ├── files
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   └── main.yml
        ├── templates
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

2.7.2)整体目录结构服务端信息

目录结构

[root@jettoloader ansible_roles]# tree roles/nfs
roles/nfs
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   ├── config.yml
│   ├── install.yml
│   ├── main.yml
│   ├── mkdir.yml
│   ├── start_NFS.yml
│   └── start_rpcbind.yml
├── templates
│   └── exports.j2
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 14 files

2.7.2.1)tasks任务目录信息

[root@jettoloader ansible_roles]# cat roles/nfs/tasks/main.yml 
---
# tasks file for roles/nfs
- include_tasks: install.yml
- include_tasks: config.yml
- include_tasks: mkdir.yml
- include_tasks: start_rpcbind.yml
- include_tasks: start_NFS.yml

[root@jettoloader ansible_roles]# cat roles/nfs/tasks/install.yml 
- name: "install package NFS "
  yum:
    name:
      - nfs-utils
      - rpcbind
    state: present
[root@jettoloader ansible_roles]# cat roles/nfs/tasks/config.yml 
- name: "NFS server config and edit restart"
  template:
    src: exports.j2
    dest: /etc/exports
    owner: root
    group: root
    mode: '644'
  notify: "reload NFS server"
[root@jettoloader ansible_roles]# cat roles/nfs/tasks/mkdir.yml 
- name: "create NFS dir"
  file:
    path: "{{ nfs_dir }}"
    owner: root
    group: root
    state: directory
    recurse: yes
[root@jettoloader ansible_roles]# cat roles/nfs/tasks/start_rpcbind.yml 
- name: "rpcbind server start"
  systemd:
    name: rpcbind
    state: started
    daemon_reload: yes
    enabled: yes
[root@jettoloader ansible_roles]# cat roles/nfs/tasks/start_NFS.yml 
- name: "NFS server start"
  systemd:
    name: nfs
    state: started
    daemon_reload: yes
    enabled: yes

2.7.2.2)handlers任务目录信息

[root@jettoloader ansible_roles]# cat roles/nfs/handlers/main.yml 
---
# handlers file for roles/nfs
- name: "reload NFS server"
  systemd:
    name: nfs
    state: reloaded

2.7.2.2)模板目录信息

[root@jettoloader ansible_roles]# cat roles/nfs/templates/exports.j2 
{{ nfs_dir }} *(rw,sync,root_squash,all_squash,anonuid=1050,anongid=1050)

2.7.2.3)变量信息

全局的:

[root@jettoloader ansible_roles]# cat group_vars/all.yml 
---
# NFS 服务端目录
nfs_dir: /opt/jettech/work/nfs/data

局部的:


[root@jettoloader ansible_roles]# cat roles/nfs/defaults/main.yml 
---
# defaults file for roles/nfs
nfs_dir: /opt/jettech/work/nfs/data


[root@jettoloader ansible_roles]# cat roles/nfs/vars/main.yml 
---
# vars file for roles/nfs[root@jettoloader ansible_roles]#
nfs_dir: /opt/jettech/work/nfs/data

优先级: group_vars/all.yml >  roles/nfs/vars/main.yml  > roles/nfs/defaults/main.yml 

2.7.3)整体目录结构客户端信息

2.7.3.1)目录结构,客户端就比较简单了,就一个挂载任务

[root@jettoloader ansible_roles]# tree roles/nfs_client/
roles/nfs_client/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 8 files

2.7.3.2)tasks任务目录信息 

# vars file for roles/nfs[root@jettoloader ansible_roles]# cat roles/nfs_client/tasks/main.yml
---
# tasks file for roles/nfs_client
- name: "mount NFS server"
  mount:
    src: 172.16.10.21:{{ nfs_dir }}
    path: /mnt
    fstype: nfs
    opts: defaults
    state: mounted

2.7.4)playbook 信息

[root@jettoloader ansible_roles]# cat all.yml 
---

# NFS server
- hosts: registry
  roles:
    - nfs

- hosts: node
  roles:
    - nfs_client

2.7.5) 任务执行

[root@jettoloader ansible_roles]# ansible-playbook -b -i ../hosts.ini --syntax-check all.yml   # 语法检测

playbook: all.yml
[root@jettoloader ansible_roles]# ansible-playbook -b -i ../hosts.ini -C  all.yml  # 预执行,测试执行

PLAY [registry] *******************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/install.yml for 172.16.10.21

TASK [nfs : install package NFS] **************************************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/config.yml for 172.16.10.21

TASK [nfs : NFS server config and edit restart] ***********************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/mkdir.yml for 172.16.10.21

TASK [nfs : create NFS dir] *******************************************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/start_rpcbind.yml for 172.16.10.21

TASK [nfs : rpcbind server start] *************************************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/start_NFS.yml for 172.16.10.21

TASK [nfs : NFS server start] *****************************************************************************************************************************************************
ok: [172.16.10.21]

PLAY [node] ***********************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [172.16.10.21]
ok: [172.16.10.15]

TASK [nfs_client : mount NFS server] ********************************************** 
 ************************************************************************************************
changed: [172.16.10.21]
changed: [172.16.10.15]

PLAY RECAP ************************************************************************************************************************************************************************
172.16.10.15               : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.10.21               : ok=13   changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0





[root@jettoloader ansible_roles]# ansible-playbook -b -i ../hosts.ini  all.yml  # 执行

PLAY [registry] *******************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/install.yml for 172.16.10.21

TASK [nfs : install package NFS] **************************************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/config.yml for 172.16.10.21

TASK [nfs : NFS server config and edit restart] ***********************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/mkdir.yml for 172.16.10.21

TASK [nfs : create NFS dir] *******************************************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/start_rpcbind.yml for 172.16.10.21

TASK [nfs : rpcbind server start] *************************************************************************************************************************************************
ok: [172.16.10.21]

TASK [nfs : include_tasks] ********************************************************************************************************************************************************
included: /app/jettech/ansible/ansible_roles/roles/nfs/tasks/start_NFS.yml for 172.16.10.21

TASK [nfs : NFS server start] *****************************************************************************************************************************************************
ok: [172.16.10.21]

PLAY [node] ***********************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [172.16.10.21]
ok: [172.16.10.15]

TASK [nfs_client : mount NFS server] **********************************************************************************************************************************************
changed: [172.16.10.21]
changed: [172.16.10.15]

PLAY RECAP ************************************************************************************************************************************************************************
172.16.10.15               : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.10.21               : ok=13   changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

2.7.5) 检查各节点挂载 情况:

[root@jettoloader ~]# mount | grep 172
172.16.10.21:/opt/jettech/work/nfs/data on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.10.15,local_lock=none,addr=172.16.10.21)

标签:roles,介绍,ansible,nfs,172.16,root,yml,galaxy
来源: https://blog.csdn.net/Michaelwubo/article/details/122837578

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有