官方文档:http://www.ansible.com.cn/docs/galaxy.html
PS:我个人觉得这一部分有点太抽象了,其实类似于git或者svn的意思,可以拉取不同的“模板内容”。研究了一天,感觉内容太丰富了,只学到了皮毛。当前是为了准备考试时间紧迫,等考完了再细细研究。
[student@workstation wangxc]$ ansible-galaxy list
# /home/student/wangxc/roles
- wangxc_role, (unknown version)
ansible-galaxy list会查看三个地方的role文件:
[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.yml
8 directories, 8 files
[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.log
CustomLog logs/{{ ansible_hostname }}-common.log common
DocumentRoot /var/www/vhosts/{{ ansible_hostname }}/
<Directory /var/www/vhosts/{{ ansible_hostname }}/>
Options +Indexes +FollowSymlinks +Includes
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
[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.yml
8 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
[student@workstation role-create]$ cat roles/myvhost/tasks/main.yml
---
# tasks file for myvhost
- name: ensure httpd is installed
yum:
name: httpd
state: latest
- name: ensure httpd service is enabled
service:
name: httpd
state: started
enabled: true
- name: config web file
template:
src: vhost.conf.j2
dest: /etc/httpd/conf.d/vhost.conf
owner: root
group: root
mode: 0644
notify: restart httpd
[student@workstation role-create]$ cat roles/myvhost/handlers/main.yml
---
# handlers file for myvhost
- name: restart httpd
service:
name: httpd
state: restarted
[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
[student@workstation role-create]$ cat user-vhost.yml
---
- name:
hosts: webservers
pre_tasks:
- name:
debug:
msg: pre id ok
roles:
- myvhost
post_tasks:
- name:
copy:
src: files/html/
dest: "/var/www/vhosts/{{ ansible_hostname }}"
- name:
debug:
msg: wen server is configured
[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
[student@workstation role-create]$ ansible webservers -a 'systemctl status httpd'
servera.lab.example.com | CHANGED | rc=0 >>
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-04-01 01:58:37 EDT; 15min ago
Docs: man:httpd.service(8)
Main PID: 11174 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 4956)
Memory: 24.2M
CGroup: /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 -DFOREGROUND
Apr 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.com
ServerName servera.lab.example.com
ErrorLog logs/servera-error.log
CustomLog logs/servera-common.log common
DocumentRoot /var/www/vhosts/servera/
<Directory /var/www/vhosts/servera/>
Options +Indexes +FollowSymlinks +Includes
Order allow,deny
Allow 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