Ansible – операция со строками в файле

Как добавить,удалить строку в файл и другие операции через Ansible

hosts: test
user: root
tasks:
# - name: create a complete empty file
# command: /usr/bin/touch /test/test.conf
- name: create a new file with lineinfile
lineinfile: dest=/test/test.conf
regexp='^' line=''
state=present
create=True
- name: add a string to the new file
lineinfile: dest=/test/test.conf
regexp='^'
line='Hello, World!'
state=present
- name: add a multiline string to the file and delete the string from before
# Be aware, with the given regex the string will be added everytime the playbook runs
lineinfile: dest=/test/test.conf
regexp='^'
line='#This is a comment\n#Another comment\n#Another comment, again\n#Again a comment\n#The last comment'
state=present
- name: add a single line, in this case the same as the comment but uncommented
lineinfile: dest=/test/test.conf
regexp='^Another'
insertafter='^#Another'
line='Another comment, no longer a comment'
state=present
- name: remove the line '#Again a comment'
lineinfile: dest=/test/test.conf
regexp='^#Again'
state=absent
- name: add a new string at the beginning of the file
lineinfile: dest=/test/test.conf
regexp='^This'
insertbefore=BOF
line='This is no longer a comment'
- name: add a new string before the match
lineinfile: dest=/test/test.conf
regexp='^Another'
insertbefore='^#Another'
line='Another comment, no longer'
- name: add a new string at the end of the file
lineinfile: dest=/test/test.conf
regexp=''
insertafter=EOF
line='The latest entry'

Как мы видим всё очень просто

- lineinfile: dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=enforcing
- lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"
- lineinfile: dest=/etc/hosts regexp='^127\.0\.0\.1' line='127.0.0.1 localhost' owner=root group=root mode=0644
- lineinfile: dest=/etc/httpd/conf/httpd.conf regexp="^Listen " insertafter="^#Listen " line="Listen 8080"
- lineinfile: dest=/etc/services regexp="^# port for http" insertbefore="^www.*80/tcp" line="# port for http by default"

Add a line to a file if it does not exist, without passing regexp

- lineinfile: dest=/tmp/testfile line="192.168.1.99 foo.lab.net foo"

Fully quoted because of the ‘: ‘ on the line. See the Gotchas in the YAML docs.

- lineinfile: "dest=/etc/sudoers state=present regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL'"
- lineinfile: dest=/opt/jboss-as/bin/standalone.conf regexp='^(.*)Xms(\d+)m(.*)$' line='\1Xms${xms}m\3' backrefs=yes

Validate the sudoers file before saving

- lineinfile: dest=/etc/sudoers state=present regexp='^%ADMIN ALL\=' line='%ADMIN ALL=(ALL) NOPASSWD:ALL' validate='visudo -cf %s'

Замена блока в конфиге apache2(прости боже!!!), исключи строки начинающиеся с #

     SSLEngine on
    SSLCertificateFile /etc/ssl/itclide_certs/itc-life.ru.crt
    SSLCertificateKeyFile /etc/ssl/private/itc-life.ru.key
    SSLCertificateChainFile /etc/ssl/siteitc-life_certs/ca.pem
    BrowserMatch "MSIE [2-6]" \
        nokeepalive ssl-unclean-shutdown \
        downgrade-1.0 force-response-1.0
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

на

Include /etc/apache2/ssl.d/itc-life.ru/ssl.itс-life.ru.conf```
    - name: Замена ssl old
      replace:
          path: "{{ item }}"
          regexp: '^((?!\#).).*SSLEngine on\n.*\n.*\n.*ca.pem\b.*\n.*\n.*\n.*\.*\n.*shutdown$'
          replace: "\t  Include /etc/apache2/ssl.d/itc-life.ru/ssl.itc-life.ru.conf"
      with_items: "{{ conf_dir.stdout_lines }}"
      validate: '/usr/sbin/apache2ctl -f %s -t'

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

 

Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.