Ansible under Lineinfile detailed use

Source: Internet
Author: User
Tags tagname

First, brief

these days in the Ansible official website, Harvest quite a lot. Intercept a lineinfile module to make a summary. If you bulk modify a line of a configuration file, Lineinfile will not be able to avoid writing playbook.

According to the official website: Lineinfile-ensure A particular line was in a file, or replace an existing, using a back-referenced regular exp Ression. To the effect that, for file special rows, use the back-end reference regular expression to replace

Second, practice

Playbook, I define the front common section first.

----hosts: "{{host}}" Remote_user: "{{user}}" Gather_facts:false tasks:

Since I have defined tag tags, to perform a specific task in playbook, just perform to the corresponding tagname to

Ansible-playbook line1.yml--extra-vars "Host=gitlab user=root"--tags "TAGNAME"-V

1, regular match, change a key parameter value

-Name:seline Modify enforcing Lineinfile:dest:/etc/selinux/config regexp: ' ^selinux= ' Lin E: ' selinux=enforcing '

Validation

[Email protected] test]# cat/etc/selinux/config# This file controls the state of SELinux on the system.# selinux= can Ta Ke one of these three values:# Enforcing-selinux security policy is enforced.# permissive-selinux prints Warni NGS instead of enforcing.# disabled-no SELinux policy is loaded. selinux=enforcing# Selinuxtype= can take one of these, values:# targeted-targeted processes is protected,# m Ls-multi level Security protection. selinuxtype=targeted


2. Add a line before or after matching content

2.1 http.conf

[email protected] test]# cat http.conf #Listen 12.34.56.78:80#listen 80#port

2.2 InsertBefore matching content is added earlier

-name:httpd.conf Modify 8080 lineinfile:dest:/opt/playbook/test/http.conf regexp: ' ^listen ' InsertBefore: ' ^ #Port ' line: ' Listen 8080 ' Tags:-http8080

Validation

[email protected] test]# cat http.conf #Listen 12.34.56.78:80#listen 80Listen 8080#port

2.3 InsertAfter matching content is added later

-name:httpd.conf Modify 8080 lineinfile:dest:/opt/playbook/test/http.conf regexp: ' ^listen ' InsertAfter: ' ^ #Port ' line: ' Listen 8080 ' Tags:-http8080

Validation

[email protected] test]# cat http.conf #Listen 12.34.56.78:80#listen 80#portlisten 8080

3. modify file Contents and permissions

3.1 Original file contents and permissions

[email protected] test]# cat hosts127.0.0.1localhost.localdomain localhost:: 1localhost6.localdomain6 localhost6192.168.1.2 foo.lab.net foo
[[email protected] test]# ls-l hosts-rwxrwxr-x 1 root Qingyun 111 December 18:07 hosts

3.2 Plays

-Name:modify hosts Lineinfile:dest:/opt/playbook/test/hosts regexp: ' ^127\.0\.0\.1 ' line: ' 127.0.0.1 localhosts ' owner:root group:root mode:0644 Tags:-hosts

3.3 Performing validation

[email protected] test]# cat hosts127.0.0.1 localhosts192.168.1.2 foo.lab.net foo[[email protected] test]# ls-l HOSTS-RW -r--r--1 root root 49 December 18:16 hosts

4. Delete a line of content

4.1 Original documents

[email protected] test]# cat hosts127.0.0.1 localhosts192.168.1.2 foo.lab.net foo

4.2 Absent script

-Name:delete 192.168.1.1 lineinfile:dest:/opt/playbook/test/hosts state:absent regexp:      ' ^192\. ' Tags:-delete192

4.3 verification

[email protected] test]# Cat hosts

127.0.0.1 localhosts


5. Add a row if the file exists

5.1 Original documents

[email protected] test]# cat hosts127.0.0.1 localhosts

5.2 Plays

-Name:add a line lineinfile:dest:/opt/playbook/test/hosts line: ' 192.168.1.2 foo.lab.net foo ' Tags:-add_a_line

5.3 Verification

[email protected] test]# cat hosts127.0.0.1 localhosts192.168.1.2 foo.lab.net foo


6. If it matches, refer to line as replacement. If no match is reached, the line is fully referenced as the add

6.1 Original documents

[[email protected] test]# cat testfile#%wheelall= (All) all

6.2 Plays

-Name:fully quoted a line lineinfile:dest:/opt/playbook/test/testfile state:present Rege XP: ' ^%wheel ' line: '%wheel all= (All) Nopasswd:all ' Tags:-testfile

6.3 Verification

[[email protected] test]# cat testfile#%wheelall= (All) All%wheel all= (All) Nopasswd:all

6.4 Original Documents

[[email protected] test]# cat testfile#%wheelall= (All) all%wheel 1234 all = (All) nopasswd

6.5 Verification

using /etc/ansible/ansible.cfg as config fileplay  [gitlab] ******************************************************************TASK [Fully  Quoted a line] *****************************************************changed: [master]  => {"Backup":  ", " changed ": true, " MSG ": " line replaced "}PLAY  RECAP *********************************************************************master                      :  ok=1    changed=1    unreachable=0    failed =0   [[email protected] test]# cat testfile# %wheelall= (All) ALL%wheel   all= (All)        nopasswd: all 

7, about the parameter backrefs,backup use.

    • When Backrefs is no, add a line if there is no match. If it matches, the match is replaced with the line content.

    • When Backrefs is yes, the file remains the same if there is no match. If it matches, the match is replaced with the line content.

    • When backup is no, no match is added. If a match is made, the replacement

    • When backup is yes, there is no match, add, if matched, replace


7.1 Need to be concerned, Backrefs is yes when the scene

7.1.1 Original File

[[email protected] test]# cat testfile#%wheelall= (All) All%wheel all= (All) Nopasswd:all#?bar

7.1.2 Script

-Name:test backrefs lineinfile:# backup:yes state:present dest:/opt/playbook/test/tes Tfile regexp: ' ^#\?bar ' backrefs:yes line: ' Bar ' Tags:-test_backrefs

7.1.3 Verification

[[email protected] test]# cat testfile#%wheelall= (All) All%wheel all= (All) Nopasswd:allbar

7.1.3 no match

[[email protected] test]# cat testfile#%wheelall= (All) All%wheel all= (All) Nopasswd:all

7.1.4 Verification

using/etc/ansible/ansible.cfg as config fileplay [Gitlab] ********************************************************* TASK [Test backrefs] ***********************************************************ok: [Master] + = {"Backup ":" "," changed ": false," MSG ":" "}play RECAP ********************************************************************* Master:ok=1 changed=0 unreachable=0 failed=0

The file remains the same


8, using the Valiate parameter, before saving the sudoers file, verify the syntax, if the error, execution, will be reported, re-edit playbook

8.1 Plays

-Name:test Validate lineinfile:dest:/etc/sudoers state:present regexp: ' ^%admin all= ' Line: '%admin all= (All) ' Validate: ' visudo-cf%s ' Tags:-testsudo

 8.2 Perform validation to say grammar is not clear

using/etc/ansible/ansible.cfg as config fileplay [Gitlab] ********************************************************* TASK [Test Validate] ***********************************************************fatal: [Master]: failed!  = = {"Changed": false, "failed": True, "MSG": "Failed to validate:rc:1 error:visudo:>>>/tmp/tmpgqjhym:syntax Error near Row 114 <<<\n "}to Retry, use:--limit @/opt/playbook/test/line1.retryplay RECAP *********************** Master:ok=0 changed=0 unreachable=0 failed=1


Iii. Summary

For specific modules, Ansible-doc can be used to view detailed usage.

This article is from the "Scattered People" blog, please be sure to keep this source http://zouqingyun.blog.51cto.com/782246/1882367

Ansible under Lineinfile detailed use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.