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

后端 未结 5 1730
天命终不由人
天命终不由人 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:16

    I had the same problem. I needed add user to sudoers group, let's say 'testuser' to line:

    User_Alias SOMEADMIN = smoeuser1, someuser2, someuser3
    

    This worked well for me:

    - name: add testuser to end of line
          lineinfile:
            dest: /etc/sudoers.d/somegroup
            state: present
            regexp: '^(User_Alias(.*)$)'
            backrefs: yes
            line: '\1, testuser'
    

    The point is that if I had '^User_Alias(..)$'* in regexp and not '^(User_Alias(..)$)'* it didn't work and whole line was replaced. With () arround searched text the result was OK:

    User_Alias SOMEADMIN = smoeuser1, someuser2, someuser3, testuser
    

    So then anything can work in line:, included ansible variables like "{{ usernames | join(', ') }}"

提交回复
热议问题