学海无涯
go,go,go

ansible使用做roles综合实验

nginx角色功能使用模板和变量实现拷贝模板的任务
卸载nginx和httpd软件
ansible web -m shell -a 'yum remove nginx -y'
ansible web -m shell -a 'yum remove httpd -y'

#清空文件夹
ansible web -m shell -a 'rm -f /app/*'


#此列子为使用nginx的角色,创建模板目录使用模板拷贝到被管理的机器,在模板使用变量ansible_processor_vcpus配置nginx的worke进程数
#重新创建nginx的角色目录
mkdir /ansible/roles/nginx
#创建tasks任务目录、templtes模板目录,handlers自动触发任务目录,vars变量目录
mkdir /ansible/roles/nginx/{tasks,templates,handlers,vars}


#复制模板并且修改模板,使用setup模块cpu变量来配置#setup模块获取到cpu的变量为ansible_processor_vcpus
#复制模板到/ansible/roles/nginx/templates模板目录
cp /etc/nginx/nginx.conf /ansible/roles/nginx/templates/nginx.conf.j2

#在模板配置变量
[root@ansible ansible]# grep ansible_processor_vcpus /ansible/roles/nginx/templates/nginx.conf.j2
worker_processes {{ansible_processor_vcpus*2}};

#创建main文件
[root@ansible ansible]# cat /ansible/roles/nginx/tasks/main.yml
- name: install package
  yum: name=nginx

- name: template
  template: src=/ansible/roles/nginx/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart service

- name: start service
  service: name=nginx state=started
=====================================================
- name: install package  配置安装任务使用yum模块
  yum: name=nginx        安装nginx

- name: template         配置模板调用模块名称
  template: src=/ansible/roles/nginx/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf  
调用此模板src=/ansible/roles/nginx/templates/nginx.conf.j2拷贝到此目录dest=/etc/nginx/nginx.conf 
  notify: restart service   
配置notify监控template模块只要触发了拷贝模板到dest=/etc/nginx/nginx.conf会执行restart service定义的service模块重启nginx

- name: start service  配置启动nginx的模块
  service: name=nginx state=started   模块配置启动nginx
=====================================================
#在handlers目录编写handlers的任务,创建main.yml文件存放任务
[root@ansible ansible]# cat /ansible/roles/nginx/handlers/main.yml
- name: restart service
  service: name=nginx state=restarted
=====================================================
- name: restart service    当notify被触发的时候,就会执行此模块重启nginx
  service: name=nginx state=restarted  配置service重启nginx
=====================================================
#配置调用角色的剧本
[root@ansible ansible]# cat test-role.yml
- hosts: web
  remote_user: root

  roles:
    - role: nginx  #调用nginx的角色

#执行剧本
[root@ansible ansible]# ansible-playbook test-role.yml 

PLAY [web] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [192.168.2.189]
ok: [192.168.2.196]

TASK [nginx : install package] ****************************************************************************************************************************************************************
changed: [192.168.2.189]
changed: [192.168.2.196]

TASK [nginx : template] ***********************************************************************************************************************************************************************
changed: [192.168.2.196]
changed: [192.168.2.189]

TASK [nginx : start service] ******************************************************************************************************************************************************************
changed: [192.168.2.189]
changed: [192.168.2.196]

RUNNING HANDLER [nginx : restart service] *****************************************************************************************************************************************************
changed: [192.168.2.189]
changed: [192.168.2.196]

PLAY RECAP ************************************************************************************************************************************************************************************
192.168.2.189              : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.196              : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

#检查
[root@ansible ansible]# ansible web -m shell -a 'ss -lntp'
192.168.2.189 | CHANGED | rc=0 >>
State      Recv-Q Send-Q Local Address:Port   Peer Address:Port              
LISTEN     0      128          *:111          *:*                   users:(("rpcbind",pid=824,fd=4),("systemd",pid=1,fd=69))
LISTEN     0      511          *:80           *:*                   users:(("nginx",pid=20159,fd=6),("nginx",pid=20158,fd=6),("nginx",pid=20157,fd=6))
LISTEN     0      128          *:22           *:*                   users:(("sshd",pid=1208,fd=3))
LISTEN     0      100    127.0.0.1:25           *:*                   users:(("master",pid=1481,fd=13))
LISTEN     0      128         :::111          ::*                   users:(("rpcbind",pid=824,fd=6),("systemd",pid=1,fd=71))
LISTEN     0      511         :::80           ::*                   users:(("nginx",pid=20159,fd=7),("nginx",pid=20158,fd=7),("nginx",pid=20157,fd=7))
LISTEN     0      128         :::22           ::*                   users:(("sshd",pid=1208,fd=4))
LISTEN     0      100        ::1:25           ::*                   users:(("master",pid=1481,fd=14))

192.168.2.196 | CHANGED | rc=0 >>
State      Recv-Q Send-Q Local Address:Port   Peer Address:Port              
LISTEN     0      128          *:111          *:*                   users:(("rpcbind",pid=832,fd=4),("systemd",pid=1,fd=44))
LISTEN     0      511          *:80           *:*                   users:(("nginx",pid=24080,fd=6),("nginx",pid=24079,fd=6),("nginx",pid=24078,fd=6),("nginx",pid=24077,fd=6),("nginx",pid=24076,fd=6))
LISTEN     0      128          *:22           *:*                   users:(("sshd",pid=1221,fd=3))
LISTEN     0      100    127.0.0.1:25           *:*                   users:(("master",pid=1481,fd=13))
LISTEN     0      128         :::111          ::*                   users:(("rpcbind",pid=832,fd=6),("systemd",pid=1,fd=51))
LISTEN     0      511         :::80           ::*                   users:(("nginx",pid=24080,fd=7),("nginx",pid=24079,fd=7),("nginx",pid=24078,fd=7),("nginx",pid=24077,fd=7),("nginx",pid=24076,fd=7))
LISTEN     0      128         :::22           ::*                   users:(("sshd",pid=1221,fd=4))
LISTEN     0      100        ::1:25           ::*                   users:(("master",pid=1481,fd=14))


#检查进程数
[root@ansible ansible]# ansible web -m shell -a 'ps aux|grep nginx'
192.168.2.196 | CHANGED | rc=0 >>
root      24076  0.0  0.1 120888  2252 ?        Ss   00:45   0:00 nginx: master process /usr/sbin/nginx
nginx     24077  0.0  0.1 121272  3332 ?        S    00:45   0:00 nginx: worker process
nginx     24078  0.0  0.1 121272  3332 ?        S    00:45   0:00 nginx: worker process
nginx     24079  0.0  0.1 121272  3332 ?        S    00:45   0:00 nginx: worker process
nginx     24080  0.0  0.1 121272  3132 ?        S    00:45   0:00 nginx: worker process
root      24221  0.0  0.0 113172  1196 pts/0    S+   00:47   0:00 /bin/sh -c ps aux|grep nginx
root      24223  0.0  0.0 112704   944 pts/0    S+   00:47   0:00 grep nginx

192.168.2.189 | CHANGED | rc=0 >>
root      20157  0.0  0.1 120888  2100 ?        Ss   00:45   0:00 nginx: master process /usr/sbin/nginx
nginx     20158  0.0  0.1 121272  3332 ?        S    00:45   0:00 nginx: worker process
nginx     20159  0.0  0.1 121272  3132 ?        S    00:45   0:00 nginx: worker process
root      20301  0.0  0.0 113172  1200 pts/0    S+   00:47   0:00 /bin/sh -c ps aux|grep nginx
root      20303  0.0  0.0 112704   940 pts/0    S+   00:47   0:00 grep nginx


#修改模板的文件执行配置文件的用户为daemon
[root@ansible ansible]# grep daemon /ansible/roles/nginx/templates/nginx.conf.j2
user daemon;

#再次执行剧本
[root@ansible ansible]# ansible-playbook test-role.yml 

PLAY [web] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [192.168.2.196]
ok: [192.168.2.189]

TASK [nginx : install package] ****************************************************************************************************************************************************************
ok: [192.168.2.196]
ok: [192.168.2.189]

TASK [nginx : template] ***********************************************************************************************************************************************************************
changed: [192.168.2.189]
changed: [192.168.2.196]

TASK [nginx : start service] ******************************************************************************************************************************************************************
ok: [192.168.2.196]
ok: [192.168.2.189]

RUNNING HANDLER [nginx : restart service] *****************************************************************************************************************************************************
changed: [192.168.2.189]
changed: [192.168.2.196]

PLAY RECAP ************************************************************************************************************************************************************************************
192.168.2.189              : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.196              : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

#检查进程管理用户是否为daemon
[root@ansible ansible]# ansible web -m shell -a 'ps aux|grep daemon'
192.168.2.196 | CHANGED | rc=0 >>
dbus        824  0.0  0.1  58196  2496 ?        Ss   Jan16   5:16 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root        833  0.0  0.4 476140  9108 ?        Ssl  Jan16   2:28 /usr/sbin/NetworkManager --no-daemon
daemon    24595  0.0  0.1 121272  3332 ?        S    00:49   0:00 nginx: worker process
daemon    24596  0.0  0.1 121272  3332 ?        S    00:49   0:00 nginx: worker process
daemon    24597  0.0  0.1 121272  3332 ?        S    00:49   0:00 nginx: worker process
daemon    24598  0.0  0.1 121272  3128 ?        S    00:49   0:00 nginx: worker process
root      24667  0.0  0.0 113172  1200 pts/0    S+   00:49   0:00 /bin/sh -c ps aux|grep daemon
root      24669  0.0  0.0 112708   940 pts/0    S+   00:49   0:00 grep daemon

192.168.2.189 | CHANGED | rc=0 >>
dbus        796  0.0  0.1  58196  2496 ?        Ss   Jan17   5:15 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root        805  0.0  0.4 476256  9116 ?        Ssl  Jan17   2:27 /usr/sbin/NetworkManager --no-daemon
daemon    20672  0.0  0.1 121272  3336 ?        S    00:49   0:00 nginx: worker process
daemon    20673  0.0  0.1 121272  3132 ?        S    00:49   0:00 nginx: worker process
root      20742  0.0  0.0 113172  1200 pts/0    S+   00:49   0:00 /bin/sh -c ps aux|grep daemon
root      20744  0.0  0.0 112708   940 pts/0    S+   00:49   0:00 grep daemon
在nginx的角色之后使用变量功能
#在成功创建nginx的角色后使用vars目录创建变量,在main.yml文件定义变量给角色任务调用
[root@ansible ansible]# cat /ansible/roles/nginx/vars/main.yml
lisenport: 8080    #定义变量为lisenport: 端口为8080


#修改模板直接调用vars目录定义的变量修改端口
[root@ansible ansible]# grep {{lisenport}} /ansible/roles/nginx/templates/nginx.conf.j2
        listen       {{lisenport}} default_server;
        listen       [::]:{{lisenport}} default_server;

#执行剧本
[root@ansible ansible]# ansible-playbook test-role.yml 

PLAY [web] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [192.168.2.196]
ok: [192.168.2.189]

TASK [nginx : install package] ****************************************************************************************************************************************************************
ok: [192.168.2.189]
ok: [192.168.2.196]

TASK [nginx : template] ***********************************************************************************************************************************************************************
changed: [192.168.2.189]
changed: [192.168.2.196]

TASK [nginx : start service] ******************************************************************************************************************************************************************
ok: [192.168.2.189]
ok: [192.168.2.196]

RUNNING HANDLER [nginx : restart service] *****************************************************************************************************************************************************
changed: [192.168.2.189]
changed: [192.168.2.196]

PLAY RECAP ************************************************************************************************************************************************************************************
192.168.2.189              : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.196              : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

#检查端口是否配置成功
[root@ansible ansible]# ansible web -m shell -a 'ss -lntup|grep 8080'
192.168.2.189 | CHANGED | rc=0 >>
tcp    LISTEN     0      511       *:8080  *:*   users:(("nginx",pid=21637,fd=6),("nginx",pid=21636,fd=6),("nginx",pid=21635,fd=6))
tcp    LISTEN     0      511      :::8080 :::*   users:(("nginx",pid=21637,fd=7),("nginx",pid=21636,fd=7),("nginx",pid=21635,fd=7))

192.168.2.196 | CHANGED | rc=0 >>
tcp    LISTEN     0      511       *:8080  *:*   users:(("nginx",pid=25573,fd=6),("nginx",pid=25572,fd=6),("nginx",pid=25571,fd=6),("nginx",pid=25570,fd=6),("nginx",pid=25569,fd=6))
tcp    LISTEN     0      511      :::8080 :::*   users:(("nginx",pid=25573,fd=7),("nginx",pid=25572,fd=7),("nginx",pid=25571,fd=7),("nginx",pid=25570,fd=7),("nginx",pid=25569,fd=7))
给剧本加标签执行单独的任务
接着做完变量后,给剧本加标签执行单独的任务
#接着做完变量后,给剧本加标签执行单独的任务
[root@ansible ansible]# cat /ansible/roles/nginx/tasks/main.yml
- name: install package
  yum: name=nginx

- name: template
  template: src=/ansible/roles/nginx/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart service
  tags: tmplfile

- name: start service
  service: name=nginx state=started


#再次修改模板,修改用户为nginx
[root@ansible ansible]# grep user /ansible/roles/nginx/templates/nginx.conf.j2
user nginx;

#执行剧本,使用标签执,行复制文件的动作
[root@ansible ansible]# ansible-playbook -t tmplfile test-role.yml 

PLAY [web] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [192.168.2.189]
ok: [192.168.2.196]

TASK [nginx : template] ***********************************************************************************************************************************************************************
changed: [192.168.2.196]
changed: [192.168.2.189]

RUNNING HANDLER [nginx : restart service] *****************************************************************************************************************************************************
changed: [192.168.2.196]
changed: [192.168.2.189]

PLAY RECAP ************************************************************************************************************************************************************************************
192.168.2.189              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.196              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

#检查剧本执行成功
[root@ansible ansible]# ansible web -m shell -a 'ps aux|grep nginx'
192.168.2.196 | CHANGED | rc=0 >>
root      25980  0.0  0.1 120888  2252 ?        Ss   01:06   0:00 nginx: master process /usr/sbin/nginx
nginx     25981  0.0  0.1 121272  3332 ?        S    01:06   0:00 nginx: worker process
nginx     25982  0.0  0.1 121272  3332 ?        S    01:06   0:00 nginx: worker process
nginx     25983  0.0  0.1 121272  3332 ?        S    01:06   0:00 nginx: worker process
nginx     25984  0.0  0.1 121272  3132 ?        S    01:06   0:00 nginx: worker process
root      26053  0.0  0.0 113172  1196 pts/0    S+   01:06   0:00 /bin/sh -c ps aux|grep nginx
root      26055  0.0  0.0 112704   940 pts/0    S+   01:06   0:00 grep nginx

192.168.2.189 | CHANGED | rc=0 >>
root      22042  0.0  0.1 120888  2100 ?        Ss   01:06   0:00 nginx: master process /usr/sbin/nginx
nginx     22043  0.0  0.1 121272  3332 ?        S    01:06   0:00 nginx: worker process
nginx     22044  0.0  0.1 121272  3132 ?        S    01:06   0:00 nginx: worker process
root      22113  0.0  0.0 113172  1200 pts/0    S+   01:06   0:00 /bin/sh -c ps aux|grep nginx
root      22115  0.0  0.0 112704   940 pts/0    S+   01:06   0:00 grep nginx
使用when进行判断和在角色剧本直接喷在变量列子
#使用when进行判断和在角色剧本直接喷在变量列子

#再次卸载nginx
ansible web -m shell -a 'yum remove nginx'


根据ip执行角色剧本如果是189的就安装nginx并拷贝模板到189
#判断条件
when: ansible_all_ipv4_addresses==["192.168.2.189"]

#编写剧本文件
[root@ansible ansible]# cat test2-role.yml
- hosts: web
  remote_user: root

  roles:
    - role: nginx
      when: ansible_all_ipv4_addresses==["192.168.2.189"]
      lisenport: 8080
=============================
- hosts: web
  remote_user: root

  roles:
    - role: nginx
      when: ansible_all_ipv4_addresses==["192.168.2.189"]  只要ip等于192.168.2.189的时候才执行nginx的角色
      lisenport: 8080  这里配置的lisenport会覆盖掉vars里面配置的变量
=============================
#执行剧本
[root@ansible ansible]# ansible-playbook  test2-role.yml 

PLAY [web] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [192.168.2.196]
ok: [192.168.2.189]

TASK [nginx : install package] ****************************************************************************************************************************************************************
skipping: [192.168.2.196]
changed: [192.168.2.189]

TASK [nginx : template] ***********************************************************************************************************************************************************************
skipping: [192.168.2.196]
changed: [192.168.2.189]

TASK [nginx : start service] ******************************************************************************************************************************************************************
skipping: [192.168.2.196]
changed: [192.168.2.189]

RUNNING HANDLER [nginx : restart service] *****************************************************************************************************************************************************
changed: [192.168.2.189]

PLAY RECAP ************************************************************************************************************************************************************************************
192.168.2.189              : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.196              : ok=1    changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   

#检查
[root@ansible ansible]# ansible web -m shell -a 'ss -ntlp'
192.168.2.196 | CHANGED | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:111                      *:*                   users:(("rpcbind",pid=832,fd=4),("systemd",pid=1,fd=30))
LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=1221,fd=3))
LISTEN     0      100    127.0.0.1:25                       *:*                   users:(("master",pid=1481,fd=13))
LISTEN     0      128         :::111                     :::*                   users:(("rpcbind",pid=832,fd=6),("systemd",pid=1,fd=32))
LISTEN     0      128         :::22                      :::*                   users:(("sshd",pid=1221,fd=4))
LISTEN     0      100        ::1:25                      :::*                   users:(("master",pid=1481,fd=14))

192.168.2.189 | CHANGED | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:111                      *:*                   users:(("rpcbind",pid=824,fd=4),("systemd",pid=1,fd=69))
LISTEN     0      511          *:8080                     *:*                   users:(("nginx",pid=23131,fd=6),("nginx",pid=23130,fd=6),("nginx",pid=23129,fd=6))
LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=1208,fd=3))
LISTEN     0      100    127.0.0.1:25                       *:*                   users:(("master",pid=1481,fd=13))
LISTEN     0      128         :::111                     :::*                   users:(("rpcbind",pid=824,fd=6),("systemd",pid=1,fd=71))
LISTEN     0      511         :::8080                    :::*                   users:(("nginx",pid=23131,fd=7),("nginx",pid=23130,fd=7),("nginx",pid=23129,fd=7))
LISTEN     0      128         :::22                      :::*                   users:(("sshd",pid=1208,fd=4))
LISTEN     0      100        ::1:25                      :::*                   users:(("master",pid=1481,fd=14))
在调用角色的剧本test2-role.yml配置的变量直接覆盖掉了vars的变量
test2-roles.yml剧本文件配置变量比vars目录定义的变量优先级要高

一个剧本调用多个角色测试
在使用nginx角色和filecopy的角色一起调用测试
#filecopy角色剧本
[root@ansible ansible]# cat /ansible/roles/filecopy/tasks/main.yml
- name: file copy
  copy: src=fstab dest=/app/

- name: file create
  file: name=/app/testfile mode=600 state=touch


#nginx角色剧本
[root@ansible ansible]# cat /ansible/roles/nginx/tasks/main.yml 
- name: install package
  yum: name=nginx

- name: template
  template: src=/ansible/roles/nginx/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart service
  tags: tmplfile

- name: start service
  service: name=nginx state=started




#获取主机名称变量"ansible_nodename": "189-testweb"
[root@ansible ~]# ansible web -m setup -a 'filter="*nodename*"'
192.168.2.196 | SUCCESS => {
    "ansible_facts": {
        "ansible_nodename": "196-testweb", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
192.168.2.189 | SUCCESS => {
    "ansible_facts": {
        "ansible_nodename": "189-testweb", 主机名称
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}



#编写调用多个角色的剧本
[root@ansible ansible]# cat test3-role.yml
- hosts: web
  remote_user: root

  roles:
    - role: nginx
      when: ansible_all_ipv4_addresses==["192.168.2.189"]
      lisenport: 8888
    - role: filecopy
      when: ansible_nodename=="189-testweb"
================================
- hosts: web
  remote_user: root

  roles:
    - role: nginx
      when: ansible_all_ipv4_addresses==["192.168.2.189"]  只有ip是189的才会执行角色nginx
      lisenport: 8888
    - role: filecopy
      when: ansible_nodename=="189-testweb"   只要主机名称为189-testweb的时候才执行角色filecopy
================================
#执行剧本
[root@ansible ansible]# ansible-playbook test3-role.yml 

PLAY [web] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [192.168.2.196]
ok: [192.168.2.189]

TASK [nginx : install package] ****************************************************************************************************************************************************************
skipping: [192.168.2.196]
ok: [192.168.2.189]

TASK [nginx : template] ***********************************************************************************************************************************************************************
skipping: [192.168.2.196]
changed: [192.168.2.189]

TASK [nginx : start service] ******************************************************************************************************************************************************************
skipping: [192.168.2.196]
ok: [192.168.2.189]

TASK [filecopy : file copy] *******************************************************************************************************************************************************************
skipping: [192.168.2.196]
ok: [192.168.2.189]

TASK [filecopy : file create] *****************************************************************************************************************************************************************
skipping: [192.168.2.196]
changed: [192.168.2.189]

RUNNING HANDLER [nginx : restart service] *****************************************************************************************************************************************************
changed: [192.168.2.189]

PLAY RECAP ************************************************************************************************************************************************************************************
192.168.2.189              : ok=7    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.196              : ok=1    changed=0    unreachable=0    failed=0    skipped=5    rescued=0    ignored=0   

#检查端口
[root@ansible ansible]# ansible web -m shell -a 'ss -lnt|grep 8888'
192.168.2.196 | FAILED | rc=1 >>
non-zero return code

192.168.2.189 | CHANGED | rc=0 >>
LISTEN     0      511          *:8888                     *:*                  
LISTEN     0      511         :::8888                    :::*     
成功吧只要是189的ip的主机安装nginx并配置模板文件

#检查filecopy角色执行结果
[root@ansible ansible]# ansible web -m shell -a 'ls -l /app'
192.168.2.196 | CHANGED | rc=0 >>
total 0

192.168.2.189 | CHANGED | rc=0 >>
total 4
-rw-r--r-- 1 root root 501 Mar  5 02:11 fstab 只要主机名称为189-testweb的才拷贝了文件fstab
-rw------- 1 root root   0 Mar  5 02:14 testfile
安装memcache使用变量配置memcache配置的内存
#安装memcache使用变量配置memcache配置的内存文件使用多大的内存实验
#配置memcache的角色,并且配置的主机ip是196的才安装memcache,并且memcache的配置由模板文件直接拷贝过去
#其中模板文件里面的内存配置直接使用setup模块获取到的内存变量使用//进行整数整除让memcache的配置文件使用内存配置为主机的4分之一

#在管理端安装memcache
yum install memcached -y


#创建测试目录
mkdir /ansible/roles/memcache/{tasks,templates} -p


#拷贝memcechad模板文件到memcache角色的模板目录templates
cp /etc/sysconfig/memcached /ansible/roles/memcache/templates/

#查找内存大小的变量名,"ansible_memtotal_mb": 1821
[root@ansible ansible]# ansible all -m setup -a 'filter="*mem*"'
192.168.2.189 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 1087, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 1595, 
                "used": 226
            }, 
            "real": {
                "free": 1087, 
                "total": 1821, 
                "used": 734
            }, 
            "swap": {
                "cached": 0, 
                "free": 3813, 
                "total": 3813, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 1821,   内存的变量名称
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

#修改memcache角色模板目录的memcached.j2文件里面的内存配置为setup模块获取的内存变量名称ansible_memtotal_mb进行运行使用//4
#进行整除,让拷贝到被管理机器的memcached只使用主机4分之1的内存
[root@ansible templates]# cat /ansible/roles/memcache/templates/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ansible_memtotal_mb//4}}"  这里表示使用变量ansible_memtotal_mb获取总内存然后使用//4除于让配置文件获取主机的4分之一的内存
OPTIONS=""
使用//取整数,取出4分之1的内存

#编写memcache的main.yml剧本任务文件
[root@ansible templates]# cat /ansible/roles/memcache/tasks/main.yml
- name: install package
  yum: name=memcached

- name: template
  template: src=/ansible/roles/memcache/templates/memcached.j2 dest=/etc/sysconfig/memcached

- name: start service
  service: name=memcached state=started enabled=yes
==================================
- name: install package
  yum: name=memcached  安装memcache

- name: template
  template: src=/ansible/roles/memcache/templates/memcached.j2 dest=/etc/sysconfig/memcached
表示吧此文件src=/ansible/roles/memcache/templates/memcached.j2拷贝到被管理的机器目录dest=/etc/sysconfig/memcached
- name: start service
  service: name=memcached state=started enabled=yes  启动memcache配置模块
==================================

#编写调用角色的剧本,只在189的机器上执行memcache的角色安装memcache
[root@ansible ansible]# cat test4-role.yml
- hosts: web
  remote_user: root

  roles:
    - role: memcache
      when: ansible_all_ipv4_addresses==["192.168.2.189"]

#执行剧本
[root@ansible ansible]# ansible-playbook test4-role.yml 

PLAY [web] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************************
ok: [192.168.2.196]
ok: [192.168.2.189]

TASK [memcache : install package] *************************************************************************************************************************************************************
skipping: [192.168.2.196]
changed: [192.168.2.189]

TASK [memcache : template] ********************************************************************************************************************************************************************
skipping: [192.168.2.196]
changed: [192.168.2.189]

TASK [memcache : start service] ***************************************************************************************************************************************************************
skipping: [192.168.2.196]
changed: [192.168.2.189]

PLAY RECAP ************************************************************************************************************************************************************************************
192.168.2.189              : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.196              : ok=1    changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   


#检查
[root@ansible ansible]# ansible web -m shell -a 'ps -aux|grep memcached|grep -v grep'
192.168.2.196 | FAILED | rc=1 >>
non-zero return code

192.168.2.189 | CHANGED | rc=0 >>
memcach+  25403  0.0  0.0 344080  1668 ?        Ssl  02:38   0:00 /usr/bin/memcached -u memcached -p 11211 -m 455 -c 1024
只要ip是189结尾的才安装了memcached


#检查memcache默认的11211端口只有189安装了memcache
[root@ansible ansible]# ansible web -m shell -a 'ss -lnt|grep 11211'
192.168.2.196 | FAILED | rc=1 >>
non-zero return code

192.168.2.189 | CHANGED | rc=0 >>
LISTEN     0      1024         *:11211                    *:*                  
LISTEN     0      1024        :::11211                   :::*  

#检查文件内存配置
[root@ansible ansible]# ansible 192.168.2.189 -m shell -a 'cat /etc/sysconfig/memcached'
192.168.2.189 | CHANGED | rc=0 >>
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="455" 189的机器的内存为1.8g,4分之一刚好为455MB
OPTIONS=""
赞(0) 打赏
未经允许不得转载:YYQ运维技术博客_运维的工作学习之路 » ansible使用做roles综合实验
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

运维devops

联系我们关于本博客

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏