Ansible

Ansible blockinfile 模組 - 插入插入程式碼行

  • December 10, 2021

我嘗試使用 Ansible blockinfile模組插入程式碼行(shell 腳本)。

name: Customized prompt
blockinfile:
path: /etc/profile.d/customized_prompt.sh
create: yes
block: |

#!/bin/bash

# customized prompt normal user and root

if (( "$(id -u)" == "1000" ))
then
   PS1="[\u@\H \w]$ "
elif (( "$(id -u)" == "0" ))
then
   PS1="[\u@\H \w]# "
fi

我有這個 Ansible 錯誤

[admin@srvansible /etc/ansible]$ ansible-playbook playbook_prompt.yml --ask-become-pass
BECOME password: 
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
could not find expected ':'

The error appears to be in '/etc/ansible/roles/utilities/tasks/main.yml': line 14, column 4, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

if (( "$(id -u)" == "1000" ))
then
^ here

我需要幫助:)

謝謝你

YAML 需要正確縮進。

試試這個:

name: Customized prompt
blockinfile:
 path: /etc/profile.d/customized_prompt.sh
 create: yes
 block: |
   if (( "$(id -u)" == "1000" ))
   then
       PS1="[\u@\H \w]$ "
   elif (( $(id -u)" == "0" ))
       PS1="[\u@\H \w]# "
   fi

引用自:https://serverfault.com/questions/1085972