当前位置:网站首页>Automatic operation and maintenance 2 - common modules in ansible

Automatic operation and maintenance 2 - common modules in ansible

2022-06-21 13:38:00 Zhaohui_ Zhang

Catalog

One 、ansible The way to achieve management

Two 、Ad-Hoc How to get help in execution

  3、 ... and 、ansible Command operation mode and common parameters

1、 Format

2、 Common parameters

Four 、ansible The basic color of the letter represents

  5、 ... and 、ansible Common modules in

1、command: function : Execute command on remote host , This module is the default module

2、shell: function : and command The module functions like , But the supported functions are more comprehensive .

3、script: stay ansible The script written in the host is executed in the controlled host

4、copy: from ansible Copy files from the host to the controlled host

5、fetch: Copy files from the controlled host to ansible host , But does not support directory

6、file: Set file properties

7、archive: Compress

 8、unarchive: decompression

9、cron: Set scheduled tasks

10、hostname: Management host name

11、yum_repository: Configure the system software warehouse source file

12、dnf: Manage... In the system dnf Warehouse and management software

13、service: Manage system service status

14、firewalld: Fire wall management

15、user: Modules can help us manage users on remote hosts , For example, create users 、 Modify the user 、 Delete user 、 Create key peer operations for users

16、group: Can help us manage groups on remote hosts .

17、lineinfile: Match lines

18、replace: You can replace the string in the file according to the regular expression we specify , All matched strings in the file will be replaced

19、setup: The module is used to collect some basic information of the remote host

20、debug: Debug module , Used to output information during debugging

Summary of this chapter :


One 、ansible The way to achieve management

The way 1:Ad-Hoc

utilize ansible Command direct management , It is mainly used in temporary command usage scenarios

The way 2:playbook

ansible Script , It is mainly used in large project scenarios , It needs early planning

ansible-playbook test.yml        # perform playbook Script 
ansible-playbook test.yml -vv    # Show details on execution 

Two 、Ad-Hoc How to get help in execution

ansible-doc Instructions for displaying module help
Format ansible-doc [ Parameters ] [ modular ...]
Common parameters -l        ## List the available modules
-s       ## Displays the playbook fragment

1、 With ping Module as an example :

ansible-doc ping -s        # see ping Short help for the module 

Add parameters <-s> Show short messages , Show details without overtime . 

In the case of displaying details , You can query the... Of this module Example !!!

2、 <-l> Parameters are used to display ansible All available modules in

ansible-doc -l        # Show ansible All available modules 

  3、 ... and 、ansible Command operation mode and common parameters

1、 Format

2、 Common parameters

 

(1) Specify the version 、 Specify modules 、 Show details

ansible --version            # Show ansible Version information 
ansible westos -m ping       # Appoint ping modular 
ansible westos -m ping -v    # Display the details when executing the command ,v The more, the more detailed ( most 3 individual v)

(2) Pre execution detection 、 Wait for error reporting time 、 Specify the remote user identity to execute the command

ansible westos -m shell -a 'useradd user1' -C            # Perform a pre test , But not really 
ansible westos -m shell -a 'hostname' -T 5                # Waiting time for error reporting 5 second     
ansible westos -m shell -a 'whoami' -b --become-user=westos    # Specify the identity of the user executing the command in the remote host as westos

(3) Specify the login user 、 Specifies whether to enter a switch sudo Password for identity

ansible westos -m shell -a 'whoami' -b --become-user=westos -u root -k
                        # Specify remote ssh Use a password to connect root Login as user 
ansible westos -m shell -a 'whoami' -b --become-user=westos -K
                        # Specify remote ssh Connect with westos User login and enter sudo password 

Four 、ansible The basic color of the letter represents

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

Be careful : The above summary belongs to most cases , Not exactly . There are some exceptions

  5、 ... and 、ansible Common modules in

1、command: function : Execute command on remote host , This module is the default module

chdir Enter the specified directory before executing the command
cmd Run the command to specify ( Generally, there is no need to add )
creates If the file exists, it will not run
removes If the file exists, it will run
free_form Commands executed in the remote host , This parameter does not need to be added

  Test and test results :

ansible westos -m command -a 'pwd'                    # perform pwd
ansible westos -m command -a 'chdir=/mnt pwd'            # Get into /mnt Execute under directory pwd
ansible westos -m command -a 'creates=/mnt pwd'        # If /mnt If it exists, it will not be executed pwd
ansible westos -m command -a 'removes=/mnt pwd'        # If /mnt If it doesn't exist, execute pwd

Be careful :Linux Many wildcards in command Module does not support , And in the shell Can be supported in . 

2、shell: function : and command The module functions like , But the supported functions are more comprehensive .

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

test result :

For some special symbols ,command Can't use , however shell It can be used .

executable: Specify the environment in which the command is executed  

3、script: stay ansible The script written in the host is executed in the controlled host

Write a script that displays the host name , Execute in the remote host .

ansible westos -m script -a './hostname.sh' 

The test results are as follows : 

4、copy: from ansible Copy files from the host to the controlled host

src Source file location
dest Destination file location
owner  /   group Specify the destination file owner /  All groups
mode Specify destination file permissions
backup=yes Back up the original file when the file exists in the controlled host
content Specifies that the text content is generated directly in the controlled host

Transfer files :

ansible westos -m copy -a 'src=/root/.ansible/inventory dest=/mnt'

Transfer files and set permissions :mode

ansible westos -m copy -a 'src=/root/.ansible/inventory dest=/mnt/inventory1 mode=777'

Transfer files and set everyone and all groups :owner / group

ansible westos -m copy -a 'src=/root/.ansible/inventory dest=/mnt/inventory1 mode=777 owner=westos group=westos'

If line breaks are required , Add... At the end of the text <\n>

Transfer files and input contents directly :content

ansible westos -m copy -a 'content="hello world" dest=/mnt/inventory2 mode=777 owner=westos group=westos'

Transfer files and back up the original files :backup=yes

If it is not set by default, the files of the target host will be overwritten directly . therefore , Commonly used in “ Back up the original file when the file exists in the controlled host

Be careful : When the transferred file is the same as the previous file , Will not transfer successfully , Therefore, there is no need to overwrite or back up the files of the target host .

ansible westos -m copy -a 'content="hello world\n" dest=/mnt/inventory2 mode=777 owner=westos group=westos backup=yes'

5、fetch: Copy files from the controlled host to ansible host , But does not support directory

src The source file of the controlled host
dest Local directory
flat Basic name function

Directly copy the remote host files to the local computer :

ansible westos -m fetch -a 'src=/etc/hostname dest=/mnt'

flat Basic name function : Indicates that the copied file is a file

add <flat=yes> After the parameter , A file is copied to this computer

  Don't add <flat=yes> When parameters are , What is copied to this computer is a directory

6、file: Set file properties

path Specify the file name
state

Specify the operation status :

touch         establish

absent         Delete

directory         recursive

link /hard        Building links

mode Set permissions
owner Set file user
group Set filegroup
src Source file
dest Target file
recurse=yes Recursive change

(1)path and state

ansible westos -m file -a 'path=/mnt/westos state=touch'       # create a file  
ansible westos -m file -a 'path=/mnt/westos state=absent'        # Delete file 
ansible westos -m file -a 'path=/mnt/westos state=directory'    # Create directory 

Create a hard link :

ansible westos -m file -a 'src=/mnt/westos/westosfile dest=/mnt/westos_hard state=hard'

  You can see , After the hard connection is created , Two files ID It's the same

(2)mode、owner and group

ansible westos -m file -a 'path=/mnt/westos/westosfile state=touch mode=777 owner=westos group=westos'

(3)link

ansible westos -m file -a 'src=/mnt/westos dest=/mnt/westos_link state=link'

4、recurse=yes: Recursive change

ansible westos -m file -a 'path=/mnt/westos mode=400 owner=westos group=westos recurse=yes'

7、archive: Compress

(1) Common parameters :

(2) test :

ansible westos -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=777'

 8、unarchive: decompression

(1) Common parameters

(2) test :

ansible westos -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt copy=no mode=777'

9、cron: Set scheduled tasks

(1) Common parameters

 (2) test

Set timing task :

ansible westos -m cron -a 'job=date name=test1 minute=*/2'
# Set timing task , Time is displayed every two minutes 

  Enable / Turn off scheduled tasks :

ansible westos -m cron -a 'job=date name=test1 disabled=yes'    # Close it. It's called test1 The task of 
ansible westos -m cron -a 'job=date name=test1 disabled=no'     # Enable named test1 The task of 

start-up / The principle of closing is , Whether to use before the task “#” Commented out  

Delete scheduled tasks :

ansible westos -m cron -a 'job=date name=test1 state=absent'

10、hostname: Management host name

ansible 172.25.254.151 -m hostname -a 'name=Client_151.westos.org'

11、yum_repository: Configure the system software warehouse source file

(1) Common parameters

(2) test

If you want to append, you can directly add... To the existing file , It won't cover .

ansible westos -m yum_repository -a "name=AppStream baseurl=http://172.25.254.50/rhel8.2/AppStream description=AppStream gpgcheck=no file=westos"

ansible westos -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.50/rhel8.2/BaseOS description=BaseOS gpgcheck=no file=westos"

Disable software source :<enabled=0>

ansible westos -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.50/rhel8.2/BaseOS description=BaseOS file=westos enabled=0"

If re enabled , Set up <enabled=1> that will do .

When you want to delete this software source <state=absent>

ansible westos -m yum_repository -a "name=BaseOS baseurl=http://172.25.254.50/rhel8.2/BaseOS description=BaseOS file=westos enabled=1 state=absent"

12、dnf: Manage... In the system dnf Warehouse and management software

(1) Common parameters

 (2) test :

  Install the software :

ansible westos -m dnf -a 'name=vsftpd state=latest'        # install vsftpd

 <state=lastes> Indicates that the latest version will be installed if it has not been installed , If an older version is installed, update .

Install multiple software / And does not detect authorization information :

ansible westos -m dnf -a 'name="httpd,dhcp-server" state=latest disable_gpg_check=yes'

<disable_gpg_check=yes> Indicates that authorization information is not detected

Uninstall software :

ansible westos -m dnf -a 'name=httpd state=absent autoremove=yes'

<autoremove=yes> Indicates that dependencies are also uninstalled when the software is uninstalled . If not, only the software itself will be uninstalled by default , Do not unload dependencies .

Install software group :

ansible westos -m dnf -a 'name="@Virtualization Tools" state=latest'

Install network software :

ansible westos -m dnf -a 'name="http://172.25.254.50/public/linuxqq_2.0.0-b2-1084_x86_64.rpm" state=present disable_gpg_check=yes'

  Be careful : General network software requires by default gpg testing , Therefore, you need to add parameters manually during installation to skip gpg testing .<disable_gpg_check=yes>

13、service: Manage system service status

(1) Common parameters

(2) test

Opening service :

ansible westos -m service -a 'name=vsftpd state=started'        # Start the service 
ansible westos -m service -a 'name=vsftpd state=restarted'      # Restart the service 

  Reload service configuration , Boot up

ansible westos -m service -a 'name=httpd state=reloaded enabled=yes'
# Set the service to start automatically ; Reload configuration 

  Be careful : Some services support reloaded Of , Some services do not support . Not absolutely !

14、firewalld: Fire wall management

(1) Common parameters

Be careful : Of the client The fire wall must be opened This configuration can only be performed after

(2) test

Set the fire wall of the remote host , And test in the browser

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

 

15、user: Modules can help us manage users on remote hosts , For example, create users 、 Modify the user 、 Delete user 、 Create key peer operations for users

(1) Common parameters

 

(2) test

Create users and specify uid, The main group , Additional group , name ,shell, Home directory , Generate sshkey:

ansible westos -m user -a 'name=zhangzhaohui uid=888 group="westos" groups="dhcpd,root" shell=/bin/bash home=/home/haha generate_ssh_key=yes state=present'

 

  Delete user :

ansible westos -m user -a 'name=lee uid=888 shell=/bin/bash state=absent remove=yes'

Be careful : When deleting users , If you need to delete it directly, you will only delete the user , The user's home directory will not be deleted ; Complete deletion can be added with parameters <remove=yes>

Create user and set password :

openssl passwd -6 'westos'        # Generate ciphertext password 
ansible westos -m user -a 'name=lee uid=888 group="westos" groups="root" shell=/bin/bash state=present password="$6$NLiQ6kNGPbtCZaGI$d/8UNj0YGwAk6NfqTwzw4.nw0MVV/H0AVTr6GuF50g7LKEJUC.kay1GxEz2Px.1lrAEaFYyHocYrbl0ldxxam1"'

16、group: Can help us manage groups on remote hosts .

(1) Common parameters

(2) test

Establishment of user groups :

ansible westos -m group -a 'name=hahaha gid=6666 state=present'

  Set up user groups , And set the group ID by 6666

Deletion of user groups :

ansible westos -m group -a 'name=hahaha state=absent'

17、lineinfile: Match lines

(1) Common parameters

(2) test

18、replace: You can replace the string in the file according to the regular expression we specify , All matched strings in the file will be replaced

(1) Common parameters

 (2) test

First, generate test text in the target host , Then use the replace command to replace the characters .<backup=yes> Means to back up the original file before modifying .

ansible westos -m replace -a 'path=/mnt/test regexp="westos" replace="zhangzhaohui" backup=yes'

19、setup: The module is used to collect some basic information of the remote host

(1) Common parameters

(2) test

ansible westos -m setup 

  To see what's new , You can filter keywords directly .

If you view the contents of an item , You can add parameters directly <filter> To display the contents of the item . 

20、debug: Debug module , Used to output information during debugging

Is similar to the shell Medium echo command . Used to output characters .

(1) Common parameters

(2) test

msg and verbosity:

ansible westos -m debug -a 'msg="hello world"'        # Display on the controlled host hello world
ansible westos -m debug -a 'msg="hello world" verbosity=0'        #debug Display level 

 <verbosity=1> Show debug The level of , The default is 0, Completely show . The bigger the value is. , The less you show .

var: As a variable , Generally used to debug <.yml> Script file

  Only when yml When a variable in a script has a value , It will output normally ; If there is no value , Will be an error

 

Summary of this chapter :

1、ansible In common parameters <-k> Indicates that the input is remote ssh Password ,<-K> Indicates input sudo Password of user identity .

2、shell and command The main difference is :

(1)shell Support executable command , You can specify the environment in which the command is executed ;

(2)shell Medium can 、 Some special characters are supported , and command China does not support .

3、 Display all components of the system / Software Group

dnf group list --hidden         # Show all components ( No addition hidden Do not show hidden components )

4、debug Module “msg” Used to display string , and “var” Used to display variables .

5、 Due to time constraints , lineinfile Module has not been written yet

~~~~~~~~~~~ To be continued ~~~~~~~~~~~~~~~~

原网站

版权声明
本文为[Zhaohui_ Zhang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221437485688.html