Ansible:在服务列表上循环并从实际存在的不需要的服务列表中禁用这些服务

时间:2018-07-02 13:44:24

标签: ansible ansible-2.x

我正在使用Ansible从RHEL7.5的基础安装中构建基础映像 我要做的一件事就是禁用不需要的服务。所以我这样做:

from kivy.adapters.listadapter import ListAdapter
from kivy.uix.listview import ListItemButton, ListView

data = [{"text": "blue", "color": (0, 0, 1, 1)},
        {"text": "green", "color": (0, 1, 0, 1)}, 
        {"text":"yellow", "color": (1, 1, 0, 1)}, 
        {"text": "black", "color": (0, 0, 0, 1)}, 
        {"text": "white", "color": (1, 1, 1, 1)}]

args_converter = lambda row_index, rec: {'text': rec['text'],
                                         'deselected_color': rec['color'],
                                         'selected_color': rec['color'],
                                         'size_hint_y': None,
                                         'height': 25}

list_adapter = ListAdapter(data=data,
                           args_converter=args_converter,
                           cls=ListItemButton,
                           selection_mode='single',
                           allow_empty_selection=False)

list_view = ListView(adapter=list_adapter)

if __name__ == '__main__':
    from kivy.base import runTouchApp
    runTouchApp(list_view)

哪个可以正常工作,可以在localhost上进行测试;然后我在测试版本上进行了尝试,但由于我要管理的服务之一甚至不存在而出错。

例如,disabled_services ==“ ntp postfix ip6tables”,但未安装ip6tables。我会从这样的模块中得到一个错误:

- name: "| disable unwanted services"
  service:
    name: "{{ item }}"
    enabled: no
    state: stopped
  loop: "{{ disabled_services }}"
  when: disabled_services is defined

因此,我正在调用service_facts模块以生成正在运行的服务的列表。在此循环中,我会将“如果服务中有服务”放在此循环中的条件(和位置):

ok: [udggsydasd48] => (item=postfix)
failed: [udggsydasd48] (item=ip6tables) => {"changed": false, "item":"ip6tables", "msg": "Could not find the requested service ip6tables: host"}

因此,如果存在该软件,它将仅尝试从“ disabled_services”中的阵列禁用服务?

我宁愿不使用fail_when:永远不要,因为这会隐藏其他错误。

谢谢

2 个答案:

答案 0 :(得分:2)

加载正在运行的services的列表后,使用union filter

  loop: "{{ disabled_services | union(services) }}"

答案 1 :(得分:0)

如果firewalld未安装/未运行,则可以使用“ failed_when:”简单地忽略错误消息

要避免弃用警告,可以通过在ansible.cfg中设置deprecation_warnings = False来禁用

- name: 'Disable firewalld Services'
   service:
     name: "{{item}}"
     state: stopped  
     enabled: no
   loop:
     - firewalld
   register: firewalld_service_disable
   failed_when: "firewalld_service_disable|failed and ('Could not find the requested service' not in firewalld_service_disable.msg)"
   ignore_errors: yes
   tags: test

下面是烦人的剧本执行输出

# ansible-playbook main.yml --tags test

PLAY [all] **********************************************************

TASK [Gathering Facts] **********************************************
ok: [ANSIBLECLIENTNODE]

TASK [hardening : Disable firewalld Services] ***********************
changed: [ANSIBLECLIENTNODE] => (item=firewalld)

PLAY RECAP **********************************************************

ANSIBLECLIENTNODE             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

如果您的ansible版本2.9及更高版本,请遵循以下“ service_facts”方法

- name: 'Populate service facts'
  service_facts:

- name: 'Disable firewalld Services'
  service:
    name: "{{item}}"
    state: stopped
    enabled: no
  loop:
   - firewalld
  when: ansible_facts.services[item] is defined
  ignore_errors: yes