Ansible 角色应该根据子网安装不同的挂载点,将所有挂载点安装在任何设备上

Ansible 角色应该根据子网安装不同的挂载点,将所有挂载点安装在任何设备上

我的 ansible 脚本在将所有挂载点放到我指定的任何系统上时表现非常出色。但我的意图是安装依赖于子网的挂载点。

.35 上的人员享有特权并需要访问特殊文档,而每个人都可以访问我们的公共文档和应用程序驱动器。'

有人知道我在这里做错了什么吗?

[更新] 听了 Vladimir Botka 的精彩回复后,我差不多就完成了。唯一的问题是我这边的部分(因为我现在已经放弃了角色):

tasks:
    - name: Mount the entries in list
      mount:
        name: '{{ item }}'
        path: '{{ item.mount_point }}'
        src: '{{ item.shared_storage }}'
        fstype: nfs
        opts: rw
        state: mounted
      with_items: '{{ my_mounts }}'

您知道为什么它会在 fstab 中输出这样的行吗:

srvr:/docs {'mount_point':\040'/mnt/doc',\040'shared_storage':\040'srvr:/docs'} nfs rw 0 0 

- - 代替 - -

srvr:/docs /mnt/doc nfs rw 0 0

我尝试设置路径:'{{ item.mount_point.1 }}' 以及其他一些猜测,但都无济于事。

原始剧本是:

---

- name: My mounts
  hosts: accounting
  become: yes

  vars:
    subnet: "{{ ansible_default_ipv4.address.split('.')[2:3] | join('.') }}"

  tasks:
    - debug:
        msg: subnet is {{ subnet }}

  roles:
    - role: nfsmount
      vars:
        mount_point: /mnt/docs
        shared_storage: bay05:/docs/spreadsheets
      when: ( "subnet == '20'" ) or ( "subnet == '35'" )

    - role: nfsmount
      vars:
        mount_point: /mnt/pub
        shared_storage: cheetah:/pub
      when: ( "subnet == '20'" )

答案1

例如,将下面的声明写为合适的

    subnet: "{{ ansible_default_ipv4.address.split('.')|last }}"
    nfsmounts:
      20:
        - mount_point: /mnt/pub
          shared_storage: cheetah:/pub
      35:
        - mount_point: /mnt/docs
          shared_storage: bay05:/docs/spreadsheets
        - mount_point: /mnt/pub
          shared_storage: cheetah:/pub
      default:
        - mount_point: /mnt/scratch
          shared_storage: bay09:/scratch
    my_mounts: "{{ nfsmounts[subnet|int]|d(nfsmounts.default) }}"

然后,任务

    - debug:
        var: ansible_default_ipv4
    - debug:
        var: subnet
    - debug:
        msg: |-
          mount_points: {{ my_mounts|map(attribute='mount_point')|list }}
          shared_store: {{ my_mounts|map(attribute='shared_storage')|list }}

TASK [debug] *****************************************************
ok: [test_11] => 
  ansible_default_ipv4:
    address: 10.3.0.35
ok: [test_12] => 
  ansible_default_ipv4:
    address: 10.3.0.20
ok: [test_13] => 
  ansible_default_ipv4:
    address: 10.3.0.19

TASK [debug] *****************************************************
ok: [test_11] => 
  subnet: '35'
ok: [test_12] => 
  subnet: '20'
ok: [test_13] => 
  subnet: '19'

TASK [debug] *****************************************************
ok: [test_11] => 
  msg: |-
    mount_points: ['/mnt/docs', '/mnt/pub']
    shared_store: ['bay05:/docs/spreadsheets', 'cheetah:/pub']
ok: [test_12] => 
  msg: |-
    mount_points: ['/mnt/pub']
    shared_store: ['cheetah:/pub']
ok: [test_13] => 
  msg: |-
    mount_points: ['/mnt/scratch']
    shared_store: ['bay09:/scratch']

使结构和代码适合您的需要。

相关内容