Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used modules in Ansible?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Ansible command

Command format: ansible [host] [- m module] [- an args]

Ansible-doc-l # lists all installed modules Note: press Q to exit

Ansible-doc-s yum #-s lists yum module description information and operation actions

Ansible 192.168.0.106-m command-a 'date' # specifies ip to execute date

Ansible abc-m command-a 'date' # specify category execution date

Ansible all-m command-a 'date' # all hosts hosts execute the date command

Ansible all-a'ls / root' # if the-m module is not added, the command module runs by default

Ansible module command module

The command module executes commands on the remote host and does not support shell features such as pipes and redirects. The common parameters are as follows:

Chdir: the directory to enter in advance before running commands on the remote host

Creates: create a file when the command is running. If the file already exists, the creation task will not be performed.

Removes: removes a file while the command is running. If the file does not exist, the removal task will not be performed.

Executeable: indicates the shell program that runs the command

Examples are as follows:

Ansible web-m command-a'ls / root'

Shell module

The shell module executes commands on the remote host, which is equivalent to calling the Shell process of the remote host, and then opens a sub-Shell to run the command under that Shell. The difference from the command module is that it supports Shell features such as pipes, redirects, and so on.

Examples are as follows:

Ansible web-m shell-a "echo hello world" # output to the screen

Ansible web-m shell-a "echo hello world > a.txt" # output to a.txt

Copy module

The copy module is used to copy the specified host files to the specified location of the remote host. Common parameters are as follows:

Dest: indicates the destination directory location of the copied file, using an absolute path. If the source is a directory, the destination is also a directory, and if the target file already exists, the original content will be overwritten.

Src: indicates the path of the source file. You can use relative and absolute paths, and you can specify directories directly. If the source is a directory, the destination should also be a directory

Mode: indicates the permissions of the target file when copying. Optional

Owner: indicates the owner of the target file when copying. Optional

Group: indicates the group of the target file at the time of replication. Optional

Content: indicates the content copied to the target host and cannot be used with src, which is equivalent to copying the data specified by content to the target file

Examples are as follows:

Ansible web-m copy-a 'src=/root/m.txt dest=/root/ owner=root mode=640'

Hostname module

The hostname module is used to manage hostnames on remote hosts

The common parameters are as follows:

Name: indicates the hostname

Examples are as follows:

Ansible 192.168.0.100-m hostname-a "name=web"

Yum module

The yum module is based on the yum mechanism and manages the package to the remote host. The common parameters are as follows:

Name: the package name, which can be accompanied by the version number. If the version is not specified, it defaults to the latest version

State=present | atest | absent: indicates the action to be performed on the package: present indicates to install the package, latest indicates to install the latest version of the package, and absent indicates to uninstall the package

Disablerepo: temporarily disable the ID of a warehouse when installing with yum

Enablerepo: temporarily enable the ID of a warehouse when installing with yum

Conf_file:yum runtime configuration file instead of using the default configuration file

Disable_gpg_check=yes | no: whether to enable integrity check

Examples are as follows:

Ansible web-m yum-a "name=httpd" # install httpd

Ansible web-m shell-a "rpm-qa | grep httpd" # View installation httpd

The management side only sends yum instructions to the managed side, and the managed side can only be installed successfully if there is an available yum repository.

Service module

The service module is a module used to manage services on remote hosts. Common parameters are as follows:

Name: name of the managed service

State=started | stopped | restarted: actions include startup, shutdown or restart

Enable=yes | no: indicates whether to set the service to start automatically

Runlevel: if enabled boot is set, define the targets under which to start automatically

Examples are as follows:

Ansible web-m service-a "name=httpd

Enabled=yes state=restarted "# set httpd service restart and boot auto start

User module

The user module is mainly used to manage user accounts on remote hosts. Common parameters are as follows:

Name: required parameter, account name

State=present | absent: create or delete an account. Present: create; absent: delete.

System=yes | no: whether it is a system account

Uid: user UID

Group: the basic group of the user

Groups: the user's additional group

Shell: shell used by default

Home: the user's home directory

Mve_home=yes | no: if the set home directory already exists, whether to move the existing home directory

Pssword: the user's password. It is recommended to use an encrypted string.

Comment: user's comment information

Remore=yes | no: whether to delete the user's home directory when state=absent

Examples are as follows:

Ansible web-m user-a "name=user01 system=yes uid=52 group=root groups=root shell=/etc/nologin home=/home/user01 password=123123"

Create a new system user on all hosts in the web group with a UID of 52

The subordinate group is root, the name is user01 and the password is 123123

Ansible abc-m command-a'tail-3 / etc/passwd'

Cron module

Used to define a task schedule

There are two states (state): present for addition (which can be omitted) and absent for removal.

Examples are as follows:

Ansible abc-m cron-a 'minute= "* / 1" job= "/ bin/echo hello world" name= "test cron job"'# write "hello world" every minute

Ansible abc-a 'crontab-l' # View scheduled tasks

Ansible abc-m cron-a 'name= "test cron job" state=absent' # remove scheduled tasks

Ansible abc-a 'crontab-l'

Group module

Manage user groups

The group module requests three instructions: groupadd, groupdel and groupmod.

Examples are as follows:

Ansible abc-m group-a 'name=nginx gid=306 system=yes'

Ansible abc-a'tail-2 / etc/group'

File module

Used to set file properties (path: is the file path src: defines the source file path)

Examples are as follows:

Ansible abc-m file-a "path=/root/test.txt state=touch" # New File

Ansible abc-a "ls-l / root/test.txt" # View

Ansible abc-m file-a "path=/root/test.txt state=absent" # Delete files

Ping module

Used to test the connectivity of specified hosts

Examples are as follows:

Ansible all-m ping

Script module

Local scripts can be copied to the managed host for execution. Note that relative paths are used to specify scripts

Examples are as follows:

Echo-e'echo "this is ansible script!" > / root/abc.sh' > / root/test.sh

Chmod + x test.sh

Ansible abc-m script-a 'test.sh'

Ansible abc-a 'cat / root/abc.sh'

Note: do not add a declaration (#! / bin/bash) to the local script, otherwise an error will be reported.

Setup module

Used to collect and view the facts of the managed host (facts is a function of Ansible to collect the device information of the managed host)

Examples are as follows:

Ansible abc-m setup

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report