Ansible: /etc/passwd から抽出したユーザー名に基づいて crontab ファイルを grep する

Ansible: /etc/passwd から抽出したユーザー名に基づいて crontab ファイルを grep する

/etc/passwd からユーザーのリストを抽出し、無効な (コメント: ^#) ジョブの crontab ファイルを grep で検索したいと考えています。

大まかな手順は次のようになります。

  1. /etc/passwd からユーザー名の配列を取得します ("my_users")
  2. 手順 1 で指定したファイル (/var/spool/cron/{{my_users}}) に対して grep を実行します。
  3. 結果を印刷するには、「デバッグ」を使用します。

何かご提案があれば幸いです。今のところ私が提案しているのは以下のとおりです。

  - name: ANSIBLE PLAYBOOK - disabled cronjob check
  hosts: "{{ variable_host | default('testvm') }}"
  remote_user: admin
  gather_facts: no
  become: yes

  tasks:    
  - getent:
      database: passwd
  - name: set_fact
    debugger: on_failed
    ansible.builtin.set_fact:
      my_users: "{{ getent_passwd|dict2items|json_query('[?contains(value,`/bin/bash`)].key') }}"
      cacheable: yes

  - name: set_fact_2
    ansible.builtin.set_fact:
      array_length: "{{ my_users|length }}"
  - debug:
      msg: "Debugging 2: {{ my_users|length }}"

  - name: Get disabled cron jobs
    debugger: always
    loop: "{{ my_users }}"
    ansible.builtin.lineinfile:
      path: "/var/spool/cron/{{ my_users }}"
      regexp: "^#"

問題の出力は次のとおりです。これは私が期待していたものとは少し異なります。

TASK [無効な cron ジョブを取得する]************************************************************************************************************************************************************************************************ 失敗: [testvm] (item=n2disk) => {"ansible_loop_var": "item", "changed": false, "item": "n2disk", "msg": "line is required with state=present"} 失敗: [testvm] (item=cento) => {"ansible_loop_var": "item", "changed": false, "item": "cento", "msg": "line is required with state=present"} 失敗: [testvm] (item=admin) => {"ansible_loop_var": "item", "changed": false, "item": "admin", "msg": "line is required with state=present"} 失敗: [testvm] (item=nprobe) => {"ansible_loop_var": "item", "changed": false, "item": "nprobe", "msg": "state=present の行が必要です"} 失敗: [testvm] (item=root) => {"ansible_loop_var": "item", "changed": false, "item": "root", "msg": "state=present の行が必要です"} 失敗: [testvm] (item=backup) => {"ansible_loop_var": "item", "changed": false, "item": "backup", "msg": "state=present の行が必要です"}

答え1

ヒントとして、crontab情報をすべて含む辞書を作成する方法を以下で参照してください。

  1. 変数を宣言する
  cron_tabs_path: /var/cron/tabs
  cron_users: "{{ cron_user.files|map(attribute='path')|
                                  map('basename')|list }}"
  cron_tabs: "{{ cron_tab.results|map(attribute='stdout')|
                                  map('community.general.jc', 'crontab')|list }}"
  cron_tabs_dict_all: "{{ dict(ansible_play_hosts|
                               zip(ansible_play_hosts|
                                   map('extract', hostvars, ['cron_tabs_dict']))) }}"
  1. すべてのcrontabファイルを検索します(cron_users
    - find:
        paths: "{{ cron_tabs_path }}"
      register: cron_user
  1. すべてのcrontabを読み取り
    - command: "crontab -u {{ item }} -l"
      register: cron_tab
      loop: "{{ cron_users }}"
  1. ユーザーとそのcrontabの辞書を作成する
    - set_fact:
        cron_tabs_dict: "{{ dict(cron_users|zip(cron_tabs)) }}"

たとえば、リモートホストtest_11とtest_13のcrontabが与えられている場合

shell> ssh admin@test_11 sudo crontab -u admin -l
#Ansible: test_1
5 12 * * * echo test 1
#Ansible: test_5
5 14 * * * echo test 5
#Ansible: test_4
5 13 * * * echo test 4
shell> ssh admin@test_11 sudo crontab -u alice -l
#Ansible: test_2
5 13 * * * echo test 2
shell> ssh admin@test_11 sudo crontab -u bob -l
#Ansible: test_3
5 14 * * * echo test 3
shell> ssh admin@test_13 sudo crontab -u admin -l
#Ansible: test_1
5 12 * * * echo test 1
#Ansible: test_4
5 13 * * * echo test 4
#Ansible: test_5
5 14 * * * echo test 5

プレイブック

shell> cat pb.yml
- hosts: test_11,test_13

  vars:

    cron_tabs_path: /var/cron/tabs
    cron_tabs: "{{ cron_tab.results|map(attribute='stdout')|
                                    map('community.general.jc', 'crontab')|list }}"
    cron_users: "{{ cron_user.files|map(attribute='path')|
                                    map('basename')|list }}"
    cron_tabs_dict_all: "{{ dict(ansible_play_hosts|
                                 zip(ansible_play_hosts|
                                     map('extract', hostvars, ['cron_tabs_dict']))) }}"

  tasks:

    - find:
        paths: "{{ cron_tabs_path }}"
      register: cron_user
    - debug:
        var: cron_users

    - command: "crontab -u {{ item }} -l"
      register: cron_tab
      loop: "{{ cron_users }}"
    - debug:
        var: cron_tabs

    - set_fact:
        cron_tabs_dict: "{{ dict(cron_users|zip(cron_tabs)) }}"
    - debug:
        var: cron_tabs_dict|to_yaml

    - debug:
        var: cron_tabs_dict_all|to_yaml
      run_once: true

与える

  cron_tabs_dict_all:
    test_11:
      admin:
        schedule:
        - command: echo test 1
          day_of_month: ['*']
          day_of_week: ['*']
          hour: ['12']
          minute: ['5']
          month: ['*']
        - command: echo test 5
          day_of_month: ['*']
          day_of_week: ['*']
          hour: ['14']
          minute: ['5']
          month: ['*']
        - command: echo test 4
          day_of_month: ['*']
          day_of_week: ['*']
          hour: ['13']
          minute: ['5']
          month: ['*']
        variables: []
      alice:
        schedule:
        - command: echo test 2
          day_of_month: ['*']
          day_of_week: ['*']
          hour: ['13']
          minute: ['5']
          month: ['*']
        variables: []
      bob:
        schedule:
        - command: echo test 3
          day_of_month: ['*']
          day_of_week: ['*']
          hour: ['14']
          minute: ['5']
          month: ['*']
        variables: []
    test_13:
      admin:
        schedule:
        - command: echo test 1
          day_of_month: ['*']
          day_of_week: ['*']
          hour: ['12']
          minute: ['5']
          month: ['*']
        - command: echo test 4
          day_of_month: ['*']
          day_of_week: ['*']
          hour: ['13']
          minute: ['5']
          month: ['*']
        - command: echo test 5
          day_of_month: ['*']
          day_of_week: ['*']
          hour: ['14']
          minute: ['5']
          month: ['*']
        variables: []

使用ゲットエント/etc/passwdを読む

    - getent:
        database: passwd

関連情報