Ansible 使用roles安裝服務


  • 創建所需要的目錄

[root@HA2 nginx]# mkdir /etc/ansible/roles/nginx/{files,handlers,meta,vars,tasks,templates,default} -pv
  • 新建tasks任務

[root@HA2 nginx]# cd /etc/ansible/roles/nginx/  //進入nginx的roles目錄[root@HA2 nginx]# cat tasks/main.yml            //查看配置文件內容- name: install nginx
  yum: name=nginx state=present
- name: install config
  template: src=files/nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx
  tags: ngxconf
- name: start nginx
  service: name=nginx state=started enabled=true
  • 提供nginx的template文件

[root@HA2 nginx]# cat files/nginx.conf.j2 | head -30
# For more information on configuration, see:
#   * Official English Documentation: 
#   * Official Russian Documentation: 
http://nginx.org/ru/docs/user {{ runuser }};     //這個是我們自定義變量,在vars/main.yml定義
worker_processes {{ ansible_processor_vcpus }};     //以an開頭通常是setup獲取fastc變量
...
server {
        listen        {{ nginx_prot }} default_server;        
        #listen       [::]:80 default_server;
        ...
  • 提供vars的定義變量

[root@HA2 nginx]# cat vars/main.yml 
runuser: daemon
  • 提供handlers配置文件

[root@HA2 nginx]# cat handlers/main.yml 
- name: restart nginx
  service: name=nginx state=restarted
  • 查看/etc/ansible/hosts

[root@HA2 ansible]# cat hosts  | egrep -v  "^#"
[webserver]
172.16.0.2 nginx_prot=808
172.16.0.4 nginx_prot=8080
[dbserver]
172.16.0.5 hname=dbserver
ps:使用roles其實就是把yaml的整個結構拆分到每個對應的同名的文件目錄中去

實例操作:

  • 查看是否安裝nginx

[root@HA2 nginx]# ansible webserver -a "rpm -q nginx"
172.16.0.4 | FAILED | rc=1 >>
package nginx is not installed
172.16.0.2 | FAILED | rc=1 >>
package nginx is not installed
  • 運行前一定要檢查,生產環境中

[root@HA2 nginx]# ansible-playbook --check nginx.yml PLAY 
[webserver] ***************************************************************TASK 
[setup] *******************************************************************
ok: [172.16.0.4]
ok: [172.16.0.2]

TASK [nginx : install nginx] ***************************************************
changed: [172.16.0.4]
changed: [172.16.0.2]

TASK [nginx : install config] **************************************************
changed: [172.16.0.2]
changed: [172.16.0.4]

TASK [nginx : start nginx] *****************************************************
changed: [172.16.0.4]
changed: [172.16.0.2]

RUNNING HANDLER [nginx : restart nginx] //這裏提示,主要看清楚,因爲我remote沒安裝使用提示not find 
****************************************
fatal: [172.16.0.2]: FAILED! => {"changed": false, "failed": true, "msg": "systemd could not find the requested service \"'nginx'\": "}
fatal: [172.16.0.4]: FAILED! => {"changed": false, "failed": true, "msg": "systemd could not find the requested service \"'nginx'\": "}

NO MORE HOSTS LEFT *************************************************************
	to retry, use: --limit @/etc/ansible/roles/nginx/nginx.retry
	PLAY RECAP *********************************************************************
172.16.0.2                 : ok=4    changed=3    unreachable=0    failed=1   
172.16.0.4                 : ok=4    changed=3    unreachable=0    failed=1   

[root@HA2 nginx]#
  • 運行,是正常OK的

[root@HA2 nginx]# ansible-playbook  nginx.yml 
PLAY [webserver] ***************************************************************
TASK [setup] *******************************************************************
ok: [172.16.0.4]
ok: [172.16.0.2]

TASK [nginx : install nginx] ***************************************************
changed: [172.16.0.4]
changed: [172.16.0.2]

TASK [nginx : install config] **************************************************
changed: [172.16.0.4]
changed: [172.16.0.2]

TASK [nginx : start nginx] *****************************************************
changed: [172.16.0.4]
changed: [172.16.0.2]

RUNNING HANDLER [nginx : restart nginx] ****************************************
changed: [172.16.0.2]
changed: [172.16.0.4]

PLAY RECAP *********************************************************************
172.16.0.2                 : ok=5    changed=4    unreachable=0    failed=0   
172.16.0.4                 : ok=5    changed=4    unreachable=0    failed=0   

[root@HA2 nginx]#
  • 查看,發現端口不一樣,是因爲nginx.conf.j2配置模板引用了/etc/ansible/hosts中的nginx_prot變量

[root@HA2 nginx]# ansible webserver -m shell -a " netstat -tunlp | grep nginx"
172.16.0.4 | SUCCESS | rc=0 >>
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      26689/nginx: master 

172.16.0.2 | SUCCESS | rc=0 >>
tcp        0      0 0.0.0.0:808             0.0.0.0:*               LISTEN      26402/nginx: master
  • 修改nginx的work process運行用戶爲user5(remote必須有該賬號)及修改爲80端口並且指定tags定義的ngxconf運行,並觸發handles

[root@HA2 nginx]# ansible-playbook -t ngxconf -e "nginx_prot=80" -e"runuser=user5"  nginx.yml 
PLAY [webserver] ***************************************************************
TASK [setup] *******************************************************************
ok: [172.16.0.4]
ok: [172.16.0.2]

TASK [nginx : install config] **************************************************
changed: [172.16.0.2]
changed: [172.16.0.4]

RUNNING HANDLER [nginx : restart nginx] ****************************************
changed: [172.16.0.4]
changed: [172.16.0.2]

PLAY RECAP *********************************************************************
172.16.0.2                 : ok=3    changed=2    unreachable=0    failed=0   
172.16.0.4                 : ok=3    changed=2    unreachable=0    failed=0
  • 驗證,如我們所想的那樣

[root@HA2 nginx]# ansible webserver -m shell -a "netstat -tunlp| grep nginx;echo "" ;ps aux| grep 'nginx: worker process'| grep -v grep"
172.16.0.2 | SUCCESS | rc=0 >>
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      26856/nginx: master 

user5     26857  0.0  0.7 124736  3500 ?        S    13:46   0:00 nginx: worker process
user5     26858  0.0  0.7 124736  3500 ?        S    13:46   0:00 nginx: worker process
172.16.0.4 | SUCCESS | rc=0 >>
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      27142/nginx: master 

user5     27143  0.0  0.7 124736  3500 ?        S    13:46   0:00 nginx: worker process
user5     27144  0.0  0.7 124736  3500 ?        S    13:46   0:00 nginx: worker process


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章