ansible common modules

Keywords: ansible

one     ping module

ansible web -m ping

command result
ansible web -m ping
192.168.64.135 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.64.140 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

two     command module

ansible web -m command -a 'df -h'

command result
192.168.64.135 | CHANGED | rc=0 >>
file system                 Capacity used available used% Mount point
/dev/mapper/centos-root   15G  1.9G   14G   13% /
devtmpfs                 901M     0  901M    0% /dev
tmpfs                    912M  124K  912M    1% /dev/shm
tmpfs                    912M  8.6M  904M    1% /run
tmpfs                    912M     0  912M    0% /sys/fs/cgroup
/dev/sda1                197M  121M   77M   62% /boot
tmpfs                    183M     0  183M    0% /run/user/0
192.168.64.140 | CHANGED | rc=0 >>
file system                 Capacity used available used% Mount point
/dev/mapper/centos-root   15G  1.7G   14G   11% /
devtmpfs                 901M     0  901M    0% /dev
tmpfs                    912M     0  912M    0% /dev/shm
tmpfs                    912M  8.5M  904M    1% /run
tmpfs                    912M     0  912M    0% /sys/fs/cgroup
/dev/sda1                197M  121M   77M   62% /boot
tmpfs                    183M     0  183M    0% /run/user/0

ansible web -m command -a 'chdir=/tmp/ ls'

three     shell module

The shell module can call the shell interpreter to run commands on the remote host, and support various functions of the shell, such as pipeline, etc.

ansible web -m shell -a 'cat /etc/passwd | grep root'

command result
192.168.64.140 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
192.168.64.135 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

ansible 192.168.159.130   - m shell   - The a '/bin/sh /root/date.sh' script is for the remote host, and the same result is placed on the remote host

 

New cases

View effect

four     copy module

Copy files to remote host

ansible  192.168.64.140  -m copy -a 'src=/etc/passwd dest=/tmp/ mode=755 owner=root'

ansible   192.168.64.140   - m copy -a 'src=/etc dest=/tmp/ '   # With folder

ansible   192.168.64.140   - m copy -a 'src=/etc/ dest=/tmp /' # without folder

command result
192.168.64.140 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "a99e56beabfc720c97bd937b9d8f24d555fa4adb", 
    "dest": "/tmp/passwd", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "2e204e5a7a6285bf34a29cbd875ff2df", 
    "mode": "0755", 
    "owner": "root", 
    "size": 1191, 
    "src": "/root/.ansible/tmp/ansible-tmp-1630761209.31-13163-45377160973753/source", 
    "state": "file", 
    "uid": 0
}

View effect

five     yum module

ansible 192.168.64.140  -m yum -a "state=installed name=tree"  

command result
192.168.64.140 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "changes": {
        "installed": [
            "tree"
        ]
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package tree.x86_64 0:1.6.0-10.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch             Version                   Repository      Size\n================================================================================\nInstalling:\n tree           x86_64           1.6.0-10.el7              base            46 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 46 k\nInstalled size: 87 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : tree-1.6.0-10.el7.x86_64                                     1/1 \n  Verifying  : tree-1.6.0-10.el7.x86_64                                     1/1 \n\nInstalled:\n  tree.x86_64 0:1.6.0-10.el7                                                    \n\nComplete!\n"
    ]
}

six     file module

Ansible file create directory

ansible 192.168.64.140 -m file -a "path=/tmp/`date +%F` state=directory  mode=755"

command result
192.168.64.140 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/2021-09-04", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}

View effect

Ansible file create file

ansible 192.168.64.140  -m file -a "path=/tmp/jfedu.txt state=touch mode=755"

View effect

 

seven     service module

 

parameter

Options / defaults

interpretation

enabled

no yes

Set whether the service is powered on and started automatically. If the parameter is not specified, the original service's power on and start status will be retained

name (required)

 

Set the name of the service to start / stop

state=

reloaded

graceful restart

=

restarted

restart

=

started

start-up

=

stopped

stop it

ansible all -m service -a "name=httpd state=restarted"

eight     user module

Ansible user specifies the Shell environment

ansible 192.168.64.140  -m user -a "name=jfedu home=/tmp/ shell=/sbin/nologin"

nine     cron module

Set scheduled tasks

It was set unsuccessfully at the beginning, but now it seems that the time also needs ""

It was set unsuccessfully at the beginning, but now it seems that the time also needs ""

ansible all -m cron -a 'name="start test.sh" minute=*/2 hour=* day=* month=* weekday=* job="sh /root/test/test.sh"'

ansible all -m cron -a 'name="ping_ip" minute="*/5" job="sh /tmp/check_ip"'

Delete scheduled task

ansible all -m cron -a 'name="start test.sh" state="absent"'

The name must be consistent

notes

ansible all -m cron -a 'name="start test.sh" minute=*/2 hour=* day=* month=* weekday=* job="sh /root/test/test.sh" disabled=yes'

note off

ansible all -m cron -a 'name="start test.sh" minute=*/2 hour=* day=* month=* weekday=* job="sh /root/test/test.sh" disabled=no'

Note: the job and time parameters must be filled in when commenting and uncommenting

ten   setup module

ansible_distribution: displays what system it is

ansible_distribution_version: displays only the system version

ansible_hostname: displays only the host name

ansible_kernel: displays the kernel version

ansible_lvm: displays lvm related information

ansible_memtotal_mb: displays the total memory of the system

ansible_memfree_mb: displays the available system memory

ansible_memory_mb: detailed display of memory

ansible_swaptotal_mb: displays the total swap memory

ansible_swapfree_mb: displays the available memory of swap memory

ansible_mounts: displays the system disk mount status

ansible_processor: displays the number of CPUs (specifically the model of each cpu)

ansible_ processor_ V cpu s: displays the number of CPUs (only the total number)

Reprint link https://blog.51cto.com/liuzhengwei521/1962350

Reprint link http://www.681314.com/linux/zi-dong-hua-yun-wei-gong-ju-ansible-xiang-jie.html

Posted by liquefi on Fri, 03 Dec 2021 05:40:34 -0800