0

我正在编写一本剧本来为所有本地鱼用户安装“哦,我的鱼”。

我想为本地 fish 用户触发安装(默认 shell 设置为 /usr/bin/fish)

并且当 'omf' 没有安装在他们的家中(检查存在 ~/.local/share/omf 目录)

这是我制作的

- name: Read local user database
  getent:
    database: passwd

- name: List local user of fish
  set_fact:
    fish_users: "{{ getent_passwd | dict2items | json_query('[? contains(value, `/usr/bin/fish`)].key') }}"

- name: Check if omf install for fish users
  stat:
    path: "/home/{{ item }}/.local/share/omf"
  loop: "{{ fish_users }}"
  register: omf_user_status

- name: Install omf when absent of fish user home
  block:
  - name: Get last omf repos
    git:
      repo: 'https://github.com/oh-my-fish/oh-my-fish'
      dest: '/tmp/omf'
      clone: yes

  - name: Installing omf for fish user
    become: yes
    become_user: "{{ item }}"
    command: /tmp/omf/bin/install -y --offline --noninteractive
    loop: "{{ fish_users }}"

  when: omf_user_status.item.stat.exists == 'False'

所以也许我的方法不好......

目前我可以获得为所有鱼类用户安装的“omf”。但我真的很难生成一个可用的列表,只为没有它的用​​户安装 omf

显然条件不成立。

omf_user_status变量是一个字典,这里是内容的一个例子。

"omf_user_status": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": false, 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1", 
                        "follow": false, 
                        "get_attributes": true, 
                        "get_checksum": true, 
                        "get_md5": false, 
                        "get_mime": true, 
                        "path": "/home/test_ansible/.local/share/omf"
                    }
                }, 
                "item": "test_ansible", 
                "stat": {
                    "atime": 1608164175.7067902, 
                    "attr_flags": "e", 
                    "attributes": [
                        "extents"
                    ], 
                    "block_size": 4096, 
                    "blocks": 8, 
                    "charset": "binary", 
                    "ctime": 1608164176.1907954, 
                    "dev": 1800, 
                    "device_type": 0, 
                    "executable": true, 
                    "exists": true, 
                    "gid": 1343, 
                    "gr_name": "test_ansible", 
                    "inode": 397192, 
                    "isblk": false, 
                    "ischr": false, 
                    "isdir": true, 
                    "isfifo": false, 
                    "isgid": false, 
                    "islnk": false, 
                    "isreg": false, 
                    "issock": false, 
                    "isuid": false, 
                    "mimetype": "inode/directory", 
                    "mode": "0755", 
                    "mtime": 1608164176.1907954, 
                    "nlink": 12, 
                    "path": "/home/test_ansible/.local/share/omf", 
                    "pw_name": "test_ansible", 
                    "readable": true, 
                    "rgrp": true, 
                    "roth": true, 
                    "rusr": true, 
                    "size": 4096, 
                    "uid": 1343, 
                    "version": "3120069218", 
                    "wgrp": false, 
                    "woth": false, 
                    "writeable": true, 
                    "wusr": true, 
                    "xgrp": true, 
                    "xoth": true, 
                    "xusr": true
                }
            }
        ]
    }

干杯

4

1 回答 1

0

我终于使它适用于这段代码。

- name: Register users who need omf
  set_fact:
    list_of_user: "{{ omf_user_status.results | rejectattr('stat.exists') | map(attribute='item') | list }}"

这里是完整的plabook

# OMF install
- name: Read local user database
  getent:
    database: passwd

- name: List local user of fish
  set_fact:
    fish_users: "{{ getent_passwd | dict2items | json_query('[? contains(value, `/usr/bin/fish`)].key') }}"

- name: Check if omf is installed for fish users
  stat:
    path: "/home/{{ item }}/.local/share/omf"
  loop: "{{ fish_users }}"
  register: omf_user_status

- name: Register users who need omf
  set_fact:
    user_need_omf: "{{ omf_user_status.results | rejectattr('stat.exists') | map(attribute='item') | list }}"

- name: Install omf when absent of fish user's home
  block:
  - name: Get lastest omf from git 
    git:
      repo: 'https://github.com/oh-my-fish/oh-my-fish'
      dest: '/tmp/omf'
      clone: yes

  - name: Installing omf for fish user
    become: yes
    become_user: "{{ item }}"
    command: /tmp/omf/bin/install -y --offline --noninteractive
    loop: "{{ user_need_omf }}"

  - name: Cleanup omf install files
    file:
      path: '/tmp/omf'
      state: absent
  # only run this block if the user_need_omf list is not empty
  when: user_need_omf | length > 0
于 2021-01-05T22:07:56.353 回答