当前位置: 代码迷 >> 综合 >> Ansible学习笔记——ansible-galaxy的用法及示例
  详细解决方案

Ansible学习笔记——ansible-galaxy的用法及示例

热度:64   发布时间:2023-11-10 14:18:24.0

ansible-galaxy的用法及示例

  • 常用命令
  • galaxy实验——创建角色

官方文档:http://www.ansible.com.cn/docs/galaxy.html
PS:我个人觉得这一部分有点太抽象了,其实类似于git或者svn的意思,可以拉取不同的“模板内容”。研究了一天,感觉内容太丰富了,只学到了皮毛。当前是为了准备考试时间紧迫,等考完了再细细研究。

常用命令

  • 查看角色:ansible-galaxy list
[student@workstation wangxc]$ ansible-galaxy list
# /home/student/wangxc/roles
- wangxc_role, (unknown version)

ansible-galaxy list会查看三个地方的role文件:

  • ./roles
  • /usr/share/ansible/role
  • /etc/ansible/roles
  • 创建角色
[student@workstation roles]$ ansible-galaxy init wangxc_role
- wangxc_role was created successfully
- 
[student@workstation roles]$ ll
total 4
drwxrwxr-x.  5 student student  47 Mar 31 18:59 testroles
-rw-rw-r--.  1 student student  68 Mar 31 19:01 test.yml
drwxrwxr-x. 10 student student 135 Apr  1 13:33 wangxc_role[student@workstation roles]$ tree wangxc_role/
wangxc_role/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars└── main.yml8 directories, 8 files

galaxy实验——创建角色

[student@workstation role-create]$ cat ansible.cfg 
[defaults]
inventory=inventory
remote_user=devops#Try me...
#callback_whitelist=timer[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False[student@workstation role-create]$ cat inventory 
[webservers]
servera.lab.example.com[student@workstation role-create]$ cat vhost.conf.j2 
# {
   { ansible_managed }}<VirtualHost *:80>ServerAdmin webmaster@{
   { ansible_fqdn }}ServerName {
   { ansible_fqdn }}ErrorLog logs/{
   { ansible_hostname }}-error.logCustomLog logs/{
   { ansible_hostname }}-common.log commonDocumentRoot /var/www/vhosts/{
   { ansible_hostname }}/<Directory /var/www/vhosts/{
   { ansible_hostname }}/>Options +Indexes +FollowSymlinks +IncludesOrder allow,denyAllow from all</Directory>
</VirtualHost>
  1. 创建文件夹,初始化myvhost角色,并删除不必要的配置文件
[student@workstation roles]$ ansible-galaxy init myvhost
- myvhost was created successfully
[student@workstation roles]$ ls
myvhost
[student@workstation roles]$ tree myvhost/
myvhost/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars└── main.yml8 directories, 8 files
[student@workstation roles]$ 
[student@workstation roles]$ rm -rvf myvhost/{defaults,vars,tests}
removed 'myvhost/defaults/main.yml'
removed directory 'myvhost/defaults'
removed 'myvhost/vars/main.yml'
removed directory 'myvhost/vars'
removed 'myvhost/tests/inventory'
removed 'myvhost/tests/test.yml'
removed directory 'myvhost/tests'
[student@workstation roles]$ tree myvhost/
myvhost/
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
└── templates
  1. 在tasks文件夹中编写yml文件,包含安装httpd服务并启动,配置web服务器配置文件。
[student@workstation role-create]$ cat roles/myvhost/tasks/main.yml 
---
# tasks file for myvhost
- name: ensure httpd is installedyum:name: httpdstate: latest- name: ensure httpd service is enabledservice:name: httpdstate: startedenabled: true- name: config web filetemplate: src: vhost.conf.j2dest: /etc/httpd/conf.d/vhost.confowner: rootgroup: rootmode: 0644notify: restart httpd
  1. 在handlers文件夹中,编写yml文件设置控制程序,用于重启httpd服务
[student@workstation role-create]$ cat roles/myvhost/handlers/main.yml 
---
# handlers file for myvhost
- name: restart httpdservice:name: httpdstate: restarted
  1. 创建自定义files/html文件夹,并写入html文件作为主页
[student@workstation role-create]$ mkdir -pv files/html
mkdir: created directory 'files'
mkdir: created directory 'files/html'
[student@workstation html]$ echo simple index > index.html
  1. 编写主playbook,验证myvhost的作用:使用role,并配置web服务
[student@workstation role-create]$ cat user-vhost.yml 
---
- name:hosts: webserverspre_tasks:- name:debug:msg: pre id okroles:- myvhostpost_tasks:- name:copy:src: files/html/dest: "/var/www/vhosts/{
    { ansible_hostname }}"- name:debug:msg: wen server is configured
  1. 执行playbook
[student@workstation role-create]$ ansible-playbook user-vhost.yml PLAY [webservers] ********************************************************************************************************************TASK [Gathering Facts] ***************************************************************************************************************
ok: [servera.lab.example.com]TASK [debug] *************************************************************************************************************************
ok: [servera.lab.example.com] => {"msg": "pre id ok"
}TASK [myvhost : ensure httpd is installed] *******************************************************************************************
changed: [servera.lab.example.com]TASK [myvhost : ensure httpd service is enabled] *************************************************************************************
changed: [servera.lab.example.com]TASK [myvhost : config web file] *****************************************************************************************************
changed: [servera.lab.example.com]RUNNING HANDLER [myvhost : restart httpd] ********************************************************************************************
changed: [servera.lab.example.com]TASK [copy] **************************************************************************************************************************
changed: [servera.lab.example.com]TASK [debug] *************************************************************************************************************************
ok: [servera.lab.example.com] => {"msg": "wen server is configured"
}PLAY RECAP ***************************************************************************************************************************
servera.lab.example.com    : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  1. 测试
    查看httpd服务是否启动
[student@workstation role-create]$ ansible webservers -a 'systemctl status httpd'
servera.lab.example.com | CHANGED | rc=0 >>
● httpd.service - The Apache HTTP ServerLoaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)Active: active (running) since Thu 2021-04-01 01:58:37 EDT; 15min agoDocs: man:httpd.service(8)Main PID: 11174 (httpd)Status: "Running, listening on: port 80"Tasks: 213 (limit: 4956)Memory: 24.2MCGroup: /system.slice/httpd.service├─11174 /usr/sbin/httpd -DFOREGROUND├─11178 /usr/sbin/httpd -DFOREGROUND├─11179 /usr/sbin/httpd -DFOREGROUND├─11180 /usr/sbin/httpd -DFOREGROUND└─11181 /usr/sbin/httpd -DFOREGROUNDApr 01 01:58:37 servera.lab.example.com systemd[1]: Stopped The Apache HTTP Server.
Apr 01 01:58:37 servera.lab.example.com systemd[1]: Starting The Apache HTTP Server...
Apr 01 01:58:37 servera.lab.example.com httpd[11174]: AH00112: Warning: DocumentRoot [/var/www/vhosts/servera/] does not exist
Apr 01 01:58:37 servera.lab.example.com httpd[11174]: Server configured, listening on: port 80
Apr 01 01:58:37 servera.lab.example.com systemd[1]: Started The Apache HTTP Server.

查看httpd服务的配置文件

[student@workstation role-create]$ ansible webservers -a 'cat /etc/httpd/conf.d/vhost.conf'
servera.lab.example.com | CHANGED | rc=0 >>
# Ansible managed<VirtualHost *:80>ServerAdmin webmaster@servera.lab.example.comServerName servera.lab.example.comErrorLog logs/servera-error.logCustomLog logs/servera-common.log commonDocumentRoot /var/www/vhosts/servera/<Directory /var/www/vhosts/servera/>Options +Indexes +FollowSymlinks +IncludesOrder allow,denyAllow from all</Directory>
</VirtualHost>

查看index.html文件

[student@workstation role-create]$ ansible webservers -a 'cat /var/www/vhosts/servera/index.html'
servera.lab.example.com | CHANGED | rc=0 >>
simple index

测试是否可以访问到

[student@workstation role-create]$ ansible webservers -m uri -a 'url=http://localhost return_content=true'
servera.lab.example.com | SUCCESS => {"accept_ranges": "bytes","ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"connection": "close","content": "simple index\n","content_length": "13","content_type": "text/html; charset=UTF-8","cookies": {},"cookies_string": "","date": "Thu, 01 Apr 2021 06:21:06 GMT","elapsed": 0,"etag": "\"d-5bee2ec3992a6\"","last_modified": "Thu, 01 Apr 2021 05:58:38 GMT","msg": "OK (13 bytes)","redirected": false,"server": "Apache/2.4.37 (Red Hat Enterprise Linux)","status": 200,"url": "http://localhost"
}
[student@workstation role-create]$ curl http://servera.lab.example.com
simple index