如何使用 Ansible-playbook 在磁盘的 /etc/fstab 中附加条目?
How to append entries in `/etc/fstab` of disks using Ansible-playbook?
我正在使用下面的剧本将条目写入 /etc/fstab
。并创建 swap
文件:
---
- name: Configure SWAP
hosts: localhost
become: yes
become_user: root
tasks:
- name: Configuring a SWAP
command: "{{ item }}"
loop:
- mkswap -f "{{ ebs_swap }}"
- echo "UUID=$(blkid -s UUID -o value {{ ebs_swap }}) swap swap defaults 0 0" | sudo tee -a /etc/fstab
- swapon -a
register: output
- name: Display the variable
debug:
msg: "{{ output}}"
我们正在运行使用以下命令对其进行设置:ansible-playbook mount.yml -e "ebs_swap=/dev/xvdj"
O/P:
"item": "echo \"UUID=$(blkid -s UUID -o value /dev/xvdj) swap swap defaults 0 0\" | sudo tee -a /etc/fstab",
"rc": 0,
"start": "2020-04-09 14:51:23.890047",
"stderr": "",
"stderr_lines": [],
"stdout": "UUID=$(blkid -s UUID -o value /dev/xvdj) swap swap defaults 0 0 | sudo tee -a /etc/fstab",
"stdout_lines": [
"UUID=$(blkid -s UUID -o value /dev/xvdj) swap swap defaults 0 0 | sudo tee -a /etc/fstab"
任何人都可以告诉我为什么我无法在 /etc/fstab
中获取条目以及当我尝试 运行 上面的命令时它会成功。
根据模块文档 https://docs.ansible.com/ansible/latest/modules/command_module.html:
If you want to run a command through the shell (say you are using <, >, |, etc), you actually want the shell module instead. Parsing shell metacharacters can lead to unexpected commands being executed if quoting is not done correctly so it is more secure to use the command module when possible.
无论如何,我认为您不希望此剧本的每个 运行 都将此行添加到您的文件中,请改用 lineinfile 以确保此行存在:
- shell: blkid -s UUID -o value {{ ebs_swap }})
register: blkid_out
- lineinfile:
path: /etc/fstab
regexp: "^UUID={{ blkid_out.stdout }}"
line: "UUID={{ blkid_out.stdout }} swap swap defaults 0 0"
现在只有当该行不存在时才会添加
我已经通过以下方法解决了这个问题:
- name: Dispaly uuid & store in variable
command: blkid -s UUID -o value {{ ebs_swap }}
register: uuid_swap
- name: Add the below lines
blockinfile:
path: /etc/fstab
state: present
block: |
UUID={{ uuid_swap.stdout }} swap swap defaults 0 0
我正在使用下面的剧本将条目写入 /etc/fstab
。并创建 swap
文件:
---
- name: Configure SWAP
hosts: localhost
become: yes
become_user: root
tasks:
- name: Configuring a SWAP
command: "{{ item }}"
loop:
- mkswap -f "{{ ebs_swap }}"
- echo "UUID=$(blkid -s UUID -o value {{ ebs_swap }}) swap swap defaults 0 0" | sudo tee -a /etc/fstab
- swapon -a
register: output
- name: Display the variable
debug:
msg: "{{ output}}"
我们正在运行使用以下命令对其进行设置:ansible-playbook mount.yml -e "ebs_swap=/dev/xvdj"
O/P:
"item": "echo \"UUID=$(blkid -s UUID -o value /dev/xvdj) swap swap defaults 0 0\" | sudo tee -a /etc/fstab",
"rc": 0,
"start": "2020-04-09 14:51:23.890047",
"stderr": "",
"stderr_lines": [],
"stdout": "UUID=$(blkid -s UUID -o value /dev/xvdj) swap swap defaults 0 0 | sudo tee -a /etc/fstab",
"stdout_lines": [
"UUID=$(blkid -s UUID -o value /dev/xvdj) swap swap defaults 0 0 | sudo tee -a /etc/fstab"
任何人都可以告诉我为什么我无法在 /etc/fstab
中获取条目以及当我尝试 运行 上面的命令时它会成功。
根据模块文档 https://docs.ansible.com/ansible/latest/modules/command_module.html:
If you want to run a command through the shell (say you are using <, >, |, etc), you actually want the shell module instead. Parsing shell metacharacters can lead to unexpected commands being executed if quoting is not done correctly so it is more secure to use the command module when possible.
无论如何,我认为您不希望此剧本的每个 运行 都将此行添加到您的文件中,请改用 lineinfile 以确保此行存在:
- shell: blkid -s UUID -o value {{ ebs_swap }})
register: blkid_out
- lineinfile:
path: /etc/fstab
regexp: "^UUID={{ blkid_out.stdout }}"
line: "UUID={{ blkid_out.stdout }} swap swap defaults 0 0"
现在只有当该行不存在时才会添加
我已经通过以下方法解决了这个问题:
- name: Dispaly uuid & store in variable
command: blkid -s UUID -o value {{ ebs_swap }}
register: uuid_swap
- name: Add the below lines
blockinfile:
path: /etc/fstab
state: present
block: |
UUID={{ uuid_swap.stdout }} swap swap defaults 0 0