我有一个可运行大约 20 个主机的 ansible 剧本。我正在尝试仅编辑作为根('/')安装的条目的 pass 选项。
这是我改变每个非“0”的传递选项的内容,这是我不想要的。
- name: Modify mount options in /etc/fstab
replace:
path: /etc/fstab
regexp: '(\b)0 0(\b)'
replace: '\g<1>0 1\g<2>'
when: ansible_distribution == 'Ubuntu'
ChatGPT 建议类似的内容,但添加我的值不起作用。我的所有文件系统都有不同的命名格式,因此并非全部都以 UUID 开头。
- name: Change pass option
lineinfile:
path: /etc/fstab
regexp: '^UUID=[a-f0-9-]+\s+/\s'
line: 'UUID=your_uuid / ext4 defaults,pass=new_pass_option 0 0'
例如,给定库存
shell> cat hosts
test_01
test_02
test_03
以及用于测试的文件 /tmp/fstab(全部相同)
shell> ssh admin@test_01 cat /tmp/fstab
UUID=423d168c-5a90-44f9-94b2-d2da9f0e1e4f / ext4 errors=remount-ro 0 1
UUID=0F15-7883 /boot/efi vfat umask=0077 0 1
/swapfile none swap sw 0 0
shell> ssh admin@test_02 cat /tmp/fstab
UUID=423d168c-5a90-44f9-94b2-d2da9f0e1e4f / ext4 errors=remount-ro 0 1
UUID=0F15-7883 /boot/efi vfat umask=0077 0 1
/swapfile none swap sw 0 0
shell> ssh admin@test_03 cat /tmp/fstab
UUID=423d168c-5a90-44f9-94b2-d2da9f0e1e4f / ext4 errors=remount-ro 0 1
UUID=0F15-7883 /boot/efi vfat umask=0077 0 1
/swapfile none swap sw 0 0
file_remote: /tmp/fstab
并获取文件到控制器
- fetch:
src: "{{ file_remote }}"
dest: /tmp/ansible/fetch
这将在控制器上创建文件
shell> tree /tmp/ansible/fetch/
/tmp/ansible/fetch/
├── test_01
│ └── tmp
│ └── fstab
├── test_02
│ └── tmp
│ └── fstab
└── test_03
└── tmp
└── fstab
file_local: "{{ '/tmp/ansible/fetch/' ~ inventory_hostname ~ file_remote }}"
files: |
[{% for h in ansible_play_hosts %}
{{ lookup('file', file_local) | community.general.jc('fstab') }},
{% endfor %}]
并创建当前 fstab 选项的字典
- set_fact:
files_dict: "{{ dict(ansible_play_hosts|zip(files)) }}"
给予
files_dict:
test_01:
- fs_file: /
fs_freq: 0
fs_mntops: errors=remount-ro
fs_passno: 1
fs_spec: UUID=423d168c-5a90-44f9-94b2-d2da9f0e1e4f
fs_vfstype: ext4
- fs_file: /boot/efi
fs_freq: 0
fs_mntops: umask=0077
fs_passno: 1
fs_spec: UUID=0F15-7883
fs_vfstype: vfat
- fs_file: none
fs_freq: 0
fs_mntops: sw
fs_passno: 0
fs_spec: /swapfile
fs_vfstype: swap
test_02:
- fs_file: /
fs_freq: 0
fs_mntops: errors=remount-ro
fs_passno: 1
fs_spec: UUID=423d168c-5a90-44f9-94b2-d2da9f0e1e4f
fs_vfstype: ext4
- fs_file: /boot/efi
fs_freq: 0
fs_mntops: umask=0077
fs_passno: 1
fs_spec: UUID=0F15-7883
fs_vfstype: vfat
- fs_file: none
fs_freq: 0
fs_mntops: sw
fs_passno: 0
fs_spec: /swapfile
fs_vfstype: swap
test_03:
- fs_file: /
fs_freq: 0
fs_mntops: errors=remount-ro
fs_passno: 1
fs_spec: UUID=423d168c-5a90-44f9-94b2-d2da9f0e1e4f
fs_vfstype: ext4
- fs_file: /boot/efi
fs_freq: 0
fs_mntops: umask=0077
fs_passno: 1
fs_spec: UUID=0F15-7883
fs_vfstype: vfat
- fs_file: none
fs_freq: 0
fs_mntops: sw
fs_passno: 0
fs_spec: /swapfile
fs_vfstype: swap
pass=new_pass_option
添加选项 /
- ansible.posix.mount:
fstab: "{{ file_remote }}"
path: "{{ my_item.fs_file }}"
src: "{{ my_item.fs_spec }}"
fstype: "{{ my_item.fs_vfstype }}"
# opts: "{{ my_item.fs_mntops }}"
opts: "{{ [my_item.fs_mntops, 'pass=new_pass_option'] |
unique |
join(',') }}"
dump: "{{ my_item.fs_freq }}"
passno: "{{ my_item.fs_passno }}"
state: present
vars:
fs_file: /
my_item: "{{ files_dict[inventory_hostname] |
selectattr('fs_file', 'eq', fs_file) |
first }}"
这只会更改文件。如果您想更新当前安装点,请使用
state: mounted
。请参阅状态。
用于测试的完整剧本示例
- hosts: all
vars:
file_remote: /tmp/fstab
file_local: "{{ '/tmp/ansible/fetch/' ~ inventory_hostname ~ file_remote }}"
files: |
[{% for h in ansible_play_hosts %}
{{ lookup('file', file_local) | community.general.jc('fstab') }},
{% endfor %}]
tasks:
- fetch:
src: "{{ file_remote }}"
dest: /tmp/ansible/fetch
when: fetch_files | d(false) | bool
- block:
- debug:
var: files
- set_fact:
files_dict: "{{ dict(ansible_play_hosts|zip(files)) }}"
- debug:
var: files_dict
run_once: true
- ansible.posix.mount:
fstab: "{{ file_remote }}"
path: "{{ my_item.fs_file }}"
src: "{{ my_item.fs_spec }}"
fstype: "{{ my_item.fs_vfstype }}"
opts: "{{ my_item.fs_mntops }}"
# opts: "{{ [my_item.fs_mntops, 'pass=new_pass_option'] |
# unique |
# join(',') }}"
dump: "{{ my_item.fs_freq }}"
passno: "{{ my_item.fs_passno }}"
state: present
vars:
fs_file: /
my_item: "{{ files_dict[inventory_hostname] |
selectattr('fs_file', 'eq', fs_file) |
first }}"