Modules commonly used in ansible

Posted by overlordhu on Tue, 21 Dec 2021 05:01:35 +0100

1.ansible management mode

Ad-Hoc		##Use the ansible command to directly complete the management, which is mainly used in the use scenario of temporary commands
playbook	##ansible script is mainly used for large-scale project scenarios and requires early planning

2. How to get help in ad hoc execution mode

ansible-doc	##Instructions for displaying module help
ansible-doc -s shell     ##View brief usage
#format
ansible-doc [parameter] [modular...]

#Common parameters
-l		##List available modules
-s		##Displays the playbook fragment of the specified module

3. Operation mode and common parameters of ansible command

Format:

ansible	detailed list	-m modular	-a Module parameters

Common parameters:

#--version		##Display version
#-m module		##Specify the module. The default is command module
#-v			##Detailed process - vv -vvv more detailed process
#--list			##To display the host list, you can also use -- List hosts
#-k			##Prompt for ssh connection password. The default is key authentication
#-C			##Pre execution detection
#-T			##The timeout for executing the command, which is 10s by default
#-u			##Specify the user to execute remotely
#-b			##Perform sudo identity switching operation
#-become-user=USERNAME	##Specify the user of sudo
#-K			##Prompt for sudo password

4. The basic color of ansible represents information

green		##The execution was successful, but no changes were made to the remote host
 yellow		##Execute successfully and make changes to the remote host
 gules		##Execution failed

5. Common modules in ansible

(1).script

Function:
Scripts written in the ansible host are executed in the controlled host

experiment:

On an ansible host:

[lee@ansible ansible]$ cat /mnt/test.sh 
touch /mnt/file
[lee@ansible ansible]$ ansible 172.25.254.211 -m script -a "/mnt/test.sh" -k
SSH password: 
172.25.254.211 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 172.25.254.211 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 172.25.254.211 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}

View the execution results in the controlled host:

[root@note1 mnt]# rm -fr *
[root@note1 mnt]# ls
[root@note1 mnt]# ls
file

(2).copy

function
Copying files from an ansible host to a controlled host
Use this command to query copy usage: ansible doc - s copy

Common parameters

src		##source file
dest		##Destination file
owner		##Specify destination file owner
group
mode		##Specify destination file permissions
backup=yes	##Back up the original file when the file exists in the controlled host
content		##The specified text content generates the file directly in the controlled host

experiment:

(1)

ansible all -m copy -a "src=/root/westos dest=/mnt/westos  owner=lee mode=777 backup=yes"
ansible all -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/westosfile1 owner=lee mode=600"
stay ansible host:
[lee@ansible ansible]$ ansible 172.25.254.211 -m copy -a "src=/mnt/test.sh dest=/mnt/westos  owner=devops mode=777 backup=yes"
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "221029bd3b8b7d5a264e3e62ddd7e24af235417d",
    "dest": "/mnt/westos",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "devops",
    "path": "/mnt/westos",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 16,
    "state": "file",
    "uid": 1001
}

View the execution results in the controlled host:

[root@note1 mnt]# ls
file  westos
[root@note1 mnt]# cat westos 
touch /mnt/file
[root@note1 mnt]# ls -l westos 
-rwxrwxrwx. 1 devops root 16 Aug 21 09:49 westos

(2)

[lee@ansible ansible]$ ansible 172.25.254.211 -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/westosfile1 owner=westos mode=600"    ##Newline \ nhello Linux \ nfold line after outputting program
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "7edbc023b406807d55423480b2bfd908870d5919",
    "dest": "/mnt/westosfile1",
    "gid": 0,
    "group": "root",
    "md5sum": "e79f6eb05e162f95e496e8d4d8a24275",
    "mode": "0600",
    "owner": "westos",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 25,
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1629510801.6436186-3219-260351840101459/source",
    "state": "file",
    "uid": 1000
}

View the results on the controlled host:

[root@note1 mnt]# ls
file  westos  westosfile1
[root@note1 mnt]# ls -l westosfile1
-rw-------. 1 westos root 25 Aug 21 09:53 westosfile1
[root@note1 mnt]# cat westosfile1
hello westos
hello linux

(3).command

Function: execute commands on the remote host. This module is the default module

Common parameters

chdir		##Enter the specified directory before executing the command
cmd		##Run command assignment
creates		##If the file exists, it will not run
removes		##If the file exists, it will run
free_form	##This parameter does not need to be added to the command executed in the remote host

example

ansible all -m command -a "useradd lee" -u root -k
ansible all -m command -a "userdel -r lee" -u root -k
ansible all -m command -a "chdir=/etc cat passwd " -u root -k
ansible all -m command -a "chdir=/etc creates=/etc/passwd cat passwd " -u root -k 
ansible all -m command -a "chdir=/etc removes=/etc/passwd cat passwd " -u root -k

Note: many wildcards in Linux are not supported in the command module

(4).shell

Function: similar to command function

Common parameters

chdir           ##Enter the specified directory before executing the command
cmd             ##Run command assignment
creates         ##If the file exists, it will not run
removes         ##If the file exists, it will run
free_form 	##This parameter does not need to be added to the command executed in the remote host
executable	##Specify the execution environment. The default is sh

example

ansible all -m shell -a "executable=sh ps ax | grep $$ " -k

(5).fetch

function
Copying files from a controlled host to an ansible host, but directories are not supported
Use this command to query fetch usage: ansible doc - s fetch

Common parameters

src		##Source file of controlled host
dest		##Native directory
flat		##Basic name function

experiment:
(1)
On an ansible host:

[lee@ansible ansible]$ ansible 172.25.254.211 -m fetch -a "src=/etc/hostname dest=/mnt ower=devops" -k
SSH password: 
172.25.254.211 | CHANGED => {
    "changed": true,
    "checksum": "0954a176b8e0bbe57b4864c61c6c6a4d36152bd9",
    "dest": "/mnt/172.25.254.211/etc/hostname",
    "md5sum": "4758b9faca9636866be7d37a198c93cd",
    "remote_checksum": "0954a176b8e0bbe57b4864c61c6c6a4d36152bd9",
    "remote_md5sum": null
}
[lee@ansible ansible]$ cd /mnt
[lee@ansible mnt]$ ls
172.25.254.211
[lee@ansible mnt]$ cd 172.25.254.211
[lee@ansible 172.25.254.211]$ ls
etc
[lee@ansible 172.25.254.211]$ cd etc
[lee@ansible etc]$ ls
hostname
[lee@ansible etc]$ cat hostname
note1.westos.org

(6).file

function
Set file properties
Use this command to query file usage: ansible doc - s file

Common parameters

path		##Specify file name
state		##Specify operation status
		##touch 		 establish
		##absent 	 delete
		##directory  	 recursion
		##link 		 Establish soft link
		#hard 		 Establish hard link
mode		##Set permissions
owner		##Set file user
group		##Set file group
src		##source file
dest		##Target file
recurse=yes	##Recursive change

experiment:

On an ansible host:

[lee@ansible ansible]$ ansible 172.25.254.211 -m file -a 'path=/mnt/hh state=touch'
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/hh",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}

View the results on the controlled host:

[root@note1 mnt]# ls
file  hh  westos  westosfile1

(7).unarchive

Function: decompression

Common parameters

copy		##The default is yes. Copy files from the ansible host to the controlled host
		##Set to no to find the src source file from the controlled host
remote_src	##The function is the same as and opposite to copy
		##Set to yes to indicate that the package is on the controlled host
		##Set to no to indicate that the package is on the ansible host
src		##Package path, which can make an ansible host or a controlled host
dest		##Controlled host directory
mode		##File permissions after pressurization < copy = yes >

experiment:
On the controlled host:

[root@note1 mnt]# tar cf etc.tar /etc/
tar: Removing leading `/' from member names
[root@note1 mnt]# gzip etc.tar
[root@note1 mnt]# ls
etc.tar.gz  file  hh  westos  westosfile1

On an ansible host:

[lee@ansible ansible]$ ansible 172.25.254.211 -m unarchive -a "src=/mnt/etc.tar.gz dest=/mnt copy=no"
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt",
    "extract_results": {
        "cmd": [
            "/bin/gtar",
            "--extract",
            "-C",
            "/mnt",
            "-z",
            "-f",
            "/mnt/etc.tar.gz"
        ],
        "err": "",
        "out": "",
        "rc": 0
    },
    "gid": 0,
    "group": "root",
    "handler": "TgzArchive",
    "mode": "0755",
    "owner": "root",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 90,
    "src": "/mnt/etc.tar.gz",
    "state": "directory",
    "uid": 0
}
[lee@ansible ansible]$ cd /mnt
[lee@ansible mnt]$ ls
172.25.254.211  etc.tar  ##Decompression succeeded

(8).archive

Action: compression

Common parameters

path		##Package directory name
dest		##Claim package file name
format		##Packaging format
owner		##Specify the person to whom the document belongs
mode		##Specify file permissions

experiment:
On an ansible host:

[lee@ansible ansible]$ ansible 172.25.254.211 -m archive -a 'path=/mnt/westos dest=/mnt/westos.tar.gz format=gz owner=devops mode=700' -k
SSH password: 
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "archived": [
        "/mnt/westos"
    ],
    "arcroot": "/mnt/",
    "changed": true,
    "dest": "/mnt/westos.tar.gz",
    "expanded_exclude_paths": [],
    "expanded_paths": [
        "/mnt/westos"
    ],
    "gid": 0,
    "group": "root",
    "missing": [],
    "mode": "0700",
    "owner": "devops",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 47,
    "state": "file",
    "uid": 1001
}

On note1 host:

[root@note1 mnt]# ls
etc  etc.tar.gz  file  hh  westos  westosfile1  westos.tar.gz

(9).hostname

Role: manage host name

Common parameters

name		##Specify host name

experiment:
On an ansible host:

[lee@ansible ansible]$ ansible 172.25.254.211 -m hostname -a 'name=lee.westos.com'
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "westos.com",
        "ansible_fqdn": "lee.westos.com",
        "ansible_hostname": "lee",
        "ansible_nodename": "lee.westos.com",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "lee.westos.com"
}

To view the results on the note1 host:

[root@note1 mnt]# hostname
lee.westos.com

(10).cron

Role: plan tasks

Common parameters

minute		##minute
hour		##hour
day		##day
month		##month
weekday		##week
name		##Task name
job		##Task script or command
disabled	##yes disable scheduled tasks
		##no start scheduled task
state		##absent delete scheduled task

experiment:
On an ansible host:

[lee@ansible ansible]$ ansible 172.25.254.211 -m cron -a "job='echo hello' name=test hour=5 user=westos" 
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "test"
    ]
}

View on note1 host and create successfully:

[root@note1 ~]# crontab -l -u westos
#Ansible: test
* 5 * * * echo hello

(11).yum_repository

Function: configure the source file of system software warehouse

Common parameters

name		##Specify warehouse name
baseurl		##Specify source path
description	##Specify warehouse description
file		##Specify warehouse file name
enabled		##Enable warehouse
gpgcheck	##Does the warehouse detect gpgkey
state		##The default value is present
		#absent 	 For deletion

experiment:
On an ansible host:

[lee@ansible ansible]$ ansible 172.25.254.211 -m yum_repository -a "name=AppStream baseurl=http://172.25.254.250/rhel8.2/AppStream description=AppStream gpgcheck=no file=westos_test"
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "AppStream",
    "state": "present"
}

View on note1 host and create successfully:

[root@note1 ~]# cd /etc/yum.repos.d/
[root@note1 yum.repos.d]# ls
redhat.repo  westos.repo  westos_test.repo
[root@note1 yum.repos.d]# cat westos_test.repo
[AppStream]
baseurl = http://172.25.254.250/rhel8.2/AppStream
gpgcheck = 0
name = AppStream

(2)
On an ansible host:

[lee@ansible ansible]$ ansible 172.25.254.211 -m yum_repository -a "name=AppStream  file=westos_test state=absent" -k
SSH password: 
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "AppStream",
    "state": "absent"
}

View on note1 host and delete successfully:

[root@note1 yum.repos.d]# ls
redhat.repo

(12).dnf

Function: dnf warehouse and management software in the management system

Common parameters

name		##Specify package
state		##Specify action
		#present 	 install
		#latest 		 to update
		#absent 		 delete
autoremove      ##=no, do not uninstall dependencies, only uninstall the specified installation package

list		##Lists the specified information
		# httpd	
		# installed
		# all
		# available
disable_gpg_check #=yes, disable gpgkey detection
enablerepo	##Specify the source of the installation package
disablerepo	##Disable package source

example

ansible all -m dnf -a "name=httpd state=latest"
ansible all -m dnf -a 'name="httpd,mariadb-server" state=present'
ansible all -m dnf -a 'name=httpd state=absent'
ansible all -m dnf -a 'name=httpd state=absent autoremove=no'
ansible all -m dnf -a 'name=httpd state=present enablerepo=AppStream'
ansible all -m dnf -a 'name="*" state=latest'
ansible all -m dnf -a 'name=http://172.25.254.250/software/wps-office-xxx.rpm state=present'
ansible all -m dnf -a 'name="@Virtual Tools" state=present'

(13).service

Role: manage system service status

Common parameters

name		##Specify the service name
state		##Specifies the action on the service
		#started
		#stoped
		#restarted
		#reloaded
enabled		##Set whether the service is started
		#yes start
		#No start no start

experiment:
On an ansible host:

[lee@ansible ansible]$ ansible 172.25.254.211 -m service -a "name=httpd state=started enabled=yes" -k
SSH password: 
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"

Test: on the note1 host, the service is started:

[root@note1 yum.repos.d]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; v>
   Active: active (running) since Sat 2021-08-21 13:41:13 CST; 15s a>
     Docs: man:httpd.service(8)
 Main PID: 40794 (httpd)
   Status: "Running, listening on: port 80"

(14).firewalld

Common parameters

zone		##Fire wall domain
service		##Service name
permanent	##Permanent effect

state		
 	 enabled	##allow
  	disabled	##refuse

immediate	##Effective immediately

Example:

ansible 172.25.254.211 -m firewalld -a 'zone=public service=http permanent=yes   state=enabled immediate=yes'

(15).user

Function: the module can help us manage users on the remote host, such as creating users, modifying users, deleting users, and creating key peer-to-peer operations for users

Common parameters

name		##A required parameter that specifies the name of the user to operate on.
group		##Specifies the base group where the user resides.
gourps		##Specify the additional group that the user is in.
append		##Specifies to add an additional group. The default value is no
shell		##Specifies the default shell for the user.
uid		##Specifies the uid number of the user.
comment		##Specifies the user's comment information.

state		##Specifies whether the user exists on the remote host
		#present 	 establish
		#absent 		 delete

remove		##When deleting a user is to delete the user's home directory, the default value is no
password	##This parameter is used to specify the password of the user. But the password is clear text,
		##You can use openssl password -6 'password' to generate encrypted characters
generate_ssh_key ##Generate sshkey

(1,2)
On an ansible host:

[lee@ansible ansible]$ ansible all -m user -a 'name=dehua'   ##Establish user dehua on all remote hosts
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1002,
    "home": "/home/dehua",
    "name": "dehua",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1002
}
[lee@ansible ansible]$ ansible all -m user -a 'name=dehua state=absent'  ##Delete user on remote host
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "dehua",
    "remove": false,
    "state": "absent"
}

View on ante1:

[root@note1 yum.repos.d]# su - dehua
[dehua@lee ~]$ exit
logout
[root@note1 yum.repos.d]# su - dehua  ##Show that this user does not exist after deletion
su: user dehua does not exist
[root@note1 dehua]# cd /home/
[root@note1 home]# ls
dehua  devops  westos  ##User home directory still exists

(3)
On an ansible host:

[lee@ansible ansible]$ ansible all -m user -a 'name=dehua remove=yes state=absent'  ##Delete user and delete user home directory
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "dehua",
    "remove": true,
    "state": "absent"
}

View on ante1:

[root@note1 ~]# cd /home/
[root@note1 home]# ls
devops  westos

(4)

On an ansible host:

[lee@ansible ansible]$ ansible all -m user -a 'name=dehua group=8888 groups="devops,westos"'
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 8888,
    "groups": "devops,westos",
    "home": "/home/dehua",
    "name": "dehua",
    "shell": "/bin/bash",
    "state": "present",
    "stderr": "useradd: warning: the home directory already exists.\nNot copying any file from skel directory into it.\nCreating mailbox file: File exists\n",
    "stderr_lines": [
        "useradd: warning: the home directory already exists.",
        "Not copying any file from skel directory into it.",
        "Creating mailbox file: File exists"
    ],
    "system": false,
    "uid": 1002
}

View on ante1:

With this command: watch -n 5 "tail -n 3 /etc/passwd /etc/group ;ls -l /home"
==> /etc/passwd <==
dehua:x:1002:8888::/home/dehua:/bin/bash

(5) Set password for user

[lee@ansible ansible]$ openssl passwd -6 'westos'  ##Generate an encrypted password for westos
$6$qU6qwsqgo5kb9Tyd$.yHMKDe5CiLFfqZsNeLDNZYoboF8g.xENmfvtvaloN94pWBlOfY893K7xfhKGMnf7e9POUY2/CSOvk7FHAFln.

[lee@ansible ansible]$ ansible all -m user -a 'name=dehua password="$6$qU6qwsqgo5kb9Tyd$.yHMKDe5CiLFfqZsNeLDNZYoboF8g.xENmfvtvaloN94pWBlOfY893K7xfhKGMnf7e9POUY2/CSOvk7FHAFln."'  ##Set the password after using this encryption
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "append": false,
    "changed": true,
    "comment": "",
    "group": 8888,
    "home": "/home/dehua",
    "move_home": false,
    "name": "dehua",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "uid": 1002
}

View on ante1:

==> /etc/shadow <==
apache:!!:18860::::::
dehua:$6$qU6qwsqgo5kb9Tyd$.yHMKDe5CiLFfqZsNeLDNZYoboF8g.xENmfvtvaloN9
4pWBlOfY893K7xfhKGMnf7e9POUY2/CSOvk7FHAFln.:18860:0:99999:7:::

Generate sshkey:

ansible all -m user -a 'name=lee generate_ssh_key=yes'
//
"ssh_key_file": "/home/dehua/.ssh/id_rsa",
//

View on ante1:

[dehua@lee ~]$ cd .ssh
[dehua@lee .ssh]$ ls
id_rsa  id_rsa.pub

(16).group

Function: the group module can help us manage groups on remote hosts.

Common parameters

name		##Specifies the name of the group to operate on.
state		##Specifies the status of the group
		#present 	 establish
		#absent 		 delete
gid		##gid used to specify the group

experiment:

[lee@ansible ansible]$ ansible all -m group -a 'name=westoslee gid=8888'
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 8888,
    "name": "westoslee",
    "state": "present",
    "system": false
}

(17).lineinfile

path		##Specify the file to operate on.
line		##Specifies the text content. "| +" indicates formatted input 

regexp		##Use regular expressions to match the corresponding lines when replacing text
                ##1. If no text can be matched, if there is no text replaced with in the file, the text to be replaced will be added to the last line
		##2. If multiple lines of text can be matched
		##Only the last line of text that is matched will be replaced
		##3. When deleting text, multiple lines of text can be matched
		##So these lines will be deleted.

state		##When you want to delete the corresponding text, you need to set the value of the state parameter to absent
		#The default value of state is present.

backrefs	##=yes, when there is no matching rule for the content, no change will be made to the file. The default value is No
		##Backward reference to regexp variable information

insertafter	##The insertafter parameter allows you to insert text after the specified line
		##The value of the insertafter parameter can be set to EOF or regular expression

insertbefore	##The insertbefore parameter allows you to insert text before the "specified line"
		#The value of the insertbefore parameter can be set to BOF or regular expression

backup		##Whether to back up the file before modifying it.

create		##=yes, whether to create the corresponding file when the file to be operated does not exist

Create a new file first
 

touch /mnt/westos

experiment:

(2)ansible all -m lineinfile -a 'path=/mnt/file10 line="hello hh" backup=yes '     ##Back up files before modifying them
//
"backup": "/mnt/file10.67044.2021-08-21@15:35:55~",   ##The backup file name is file10 67044.2021-08- 21@15 :35:55~
//

(3)ansible all -m lineinfile -a 'path=/mnt/westos line="hello westos"'   ##Add a line at the end

(4)
ansible all -m lineinfile -a 'path=/mnt/westos regexp="^westos" line="hello westos" '  ##Replace the text, replacing the text beginning with westos with hello westos
ansible all -m lineinfile -a 'path=/mnt/westos regexp="jwenh$" line="hi westos" '   ##Replace the text, replacing the text ending with jwenh with hi westos
ansible all -m lineinfile -a 'path=/mnt/westos regexp='^test' line="westos test new" backrefs=yes'  ##No changes are made to the file when there are no matching rules for the content

(5)ansible all -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes'

(6)
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos end #####" insertafter=EOF'  ##Inserts the specified text on the last line
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos end lee #####" insertafter="westos new"'  ##Inserts the specified text after the westos new line

(7)
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos test #####" insertbefore=BOF'  ##Inserts the specified text into the first line
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos test lee #####" insertbefore="westos new"'   ##Inserts the specified text before the westos new line

(18).replace

Function: the replace module can replace the strings in the file according to the regular expression specified by us, and all matched strings in the file will be replaced

Common parameters

path		##Specify the file to operate on
regexp		##Specify a regular expression
		#Strings in the file that match the regular will be replaced.
replace		##Specifies the string to eventually replace with.
backup		##Whether to back up the file before modifying it, preferably set to yes.

experiment:

[lee@ansible ansible]$ ansible all -m replace -a 'path=/mnt/westos regexp="westos" replace="WESTOS" backup=yes'
172.25.254.211 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup_file": "/mnt/westos.69768.2021-08-21@15:52:27~",
    "changed": true,
    "msg": "11 replacements made"
}

(19).setup

Function: the setup module is used to collect some basic information of the remote host

Common parameters

filter		##Used for conditional filtering. If set, only the information matching the filter criteria is returned

experiment:

[lee@ansible ansible]$ ansible all -m setup   -a "filter='ansible_all_ipv4_addresses'" -k
SSH password: 
172.25.254.211 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.25.254.211"
        ],
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}

(20).debug

Function: debugging module, used to output information during debugging

Common parameters:

msg: 		##Debug output messages
var:		##Pass the output of a task execution as a variable to the debug module
		##debug will print it out directly
verbosity: 	##debug level (level 0 by default, all displayed)

experiment:

amount to shell Output in echo""
[lee@ansible ansible]$ ansible all -m debug -a "msg=hello"
172.25.254.211 | SUCCESS => {
    "msg": "hello"
}

Topics: Linux