Ansible: insert a single word on an existing line in a file

后端 未结 5 1774
天命终不由人
天命终不由人 2021-01-11 15:34

I have to use Ansible modules in order to edit the /etc/ssh/sshd_config file - every time I create a new user I want to append it at these two lines:

AllowUs         


        
5条回答
  •  情书的邮戳
    2021-01-11 16:18

    You could do it in a single play with a newline, but I think it's cleaner to use two lineinfile plays for this.

    - hosts: '127.0.0.1'
      vars:
        usernames:
           - larry
           - curly
           - moe
        usergroups:
           - stooges
           - admins
      tasks:
        - lineinfile:
            dest: /etc/ssh/sshd_config
            regexp: '^AllowUsers'
            line: "AllowUsers {{usernames | join(' ')}}"
        - lineinfile:
            dest: /etc/ssh/sshd_config
            regexp: '^AllowGroups'
            line: "AllowGroups {{usergroups | join(' ')}}"
    

    Note that groups is a reserved word so don't use that as a variable name.

提交回复
热议问题