最新消息:

Ansible入门setup模块和条件判断

Linux ipcpu 19939浏览

Ansible入门setup模块和条件判断.md

一、setup模块

setup模块用于收集远程主机的一些基本信息。
而在playbook中,默认参数"gather_facts: True"的含义就是在远程主机运行setup模块,并将收集的信息记录起来。

这样在后面的playbook里面可以调用并进行一些判断和对照。

使用方法如下:

  1. [root@ansible test]$ansible all -m setup |more
  2. 211.127.129.182 | success >> {
  3. "ansible_facts": {
  4. "ansible_all_ipv4_addresses": [
  5. "211.127.129.182"
  6. ],
  7. "ansible_all_ipv6_addresses": [],
  8. "ansible_architecture": "x86_64",
  9. "ansible_bios_date": "09/21/2014",
  10. "ansible_bios_version": "6.00",
  11. "ansible_cmdline": {
  12. "KEYBOARDTYPE": "pc",
  13. "KEYTABLE": "us",
  14. "LANG": "en_US.UTF-8",
  15. OUTPUT OMITTED.

因显示篇幅过长,这列只列举一些常用项目

  1. "ansible_all_ipv4_addresses": [
  2. "211.97.148.137",
  3. "10.6.7.24"
  4. ],
  5. #@这里列出了所有IPv4地址
  6. "ansible_architecture": "x86_64",
  7. #@操作系统架构
  8. "ansible_distribution": "RedHat",
  9. "ansible_distribution_major_version": "5",
  10. "ansible_distribution_release": "Tikanga",
  11. "ansible_distribution_version": "5.8",
  12. #@操作系统版本信息
  13. "ansible_eth0": {
  14. "active": true,
  15. "device": "eth0",
  16. "ipv4": {
  17. "address": "10.6.7.24",
  18. "netmask": "255.255.255.0",
  19. "network": "10.6.7.0"
  20. },
  21. "macaddress": "52:54:00:89:ba:15",
  22. #@网卡eth0的信息
  23. "ansible_kernel": "2.6.18-308.el5",
  24. #@内核版本

如果使用ansible操作不同的操作系统例如Redhat和Debian,使用前需要对照好相关的输出项,找出不同的地方和相同的地方才能准确使用。

二、条件判断

现在有这样一个需求,生产环境现在有Redhat 5 和CentOS 6 两种操作系统环境,都需要在syslog配置文件添加一个远程syslog服务器,并且添加完成后重启服务器。

所以我们收集了两个操作系统相关的参数如下:

  1. "ansible_distribution": "RedHat",
  2. "ansible_distribution_major_version": "5",
  3. "ansible_distribution_release": "Tikanga",
  4. "ansible_distribution_version": "5.8",
  5. "ansible_os_family": "RedHat",
  6. "ansible_distribution": "CentOS",
  7. "ansible_distribution_major_version": "6",
  8. "ansible_distribution_release": "Final",
  9. "ansible_distribution_version": "6.4",
  10. "ansible_os_family": "RedHat",

接下来我们就可以编写playbook

  1. ---
  2. - hosts: webserver
  3. vars:
  4. logserver: 10.127.2.170
  5. gather_facts: True
  6. tasks:
  7. - name: add conf to config files to CentOS6
  8. lineinfile: dest=/etc/rsyslog.conf line="*.* @{{ logserver }}"
  9. when: ansible_distribution == 'CentOS' and ansible_distribution_major_version == "6"
  10. - name: restart syslog @CentOS6
  11. when: ansible_distribution == 'CentOS' and ansible_distribution_major_version == "6"
  12. service: name=rsyslog state=restarted
  13. - name: add conf to config files to RedHat 5
  14. lineinfile: dest=/etc/syslog.conf line="*.* @{{ logserver }}"
  15. when: ansible_distribution == 'RedHat' and ansible_distribution_major_version == "5"
  16. - name: restart syslog @RedHat 5
  17. when: ansible_distribution == 'RedHat' and ansible_distribution_major_version == "5"
  18. service: name=syslog state=restarted

在这里我们进行了操作系统的判断,如果是CentOS 6 则修改rsyslog.conf并重启rsyslog服务。
如果是RedHat 5 则修改syslog.conf,并重启syslog服务。

在修改文件时采用了lineinfile模块,只要文件中有语句存在,下次运行就不会改变,所以playbook可以多次运行。

一点疑惑
有同学要问,为什么要进行四次when判断,两次不就够了,写成这样

  1. - name: restart syslog @CentOS6
  2. when: ansible_distribution == 'CentOS' and ansible_distribution_major_version == "6"
  3. lineinfile: dest=/etc/rsyslog.conf line="*.* @{{ logserver }}"
  4. service: name=rsyslog state=restarted

这是不行的,ansible要求每一个play里面智能使用一个模块,使用多个会报错
ERROR: multiple actions specified in task

参考资料

http://sapser.github.io/ansible/2014/07/21/ansible-conditionals/

转载请注明:IPCPU-网络之路 » Ansible入门setup模块和条件判断