ansible MODULE FOR AUTOMATIC OPERATION AND MANAGEMENT

Keywords: Linux ansible CentOS MySQL RPM

Ansible can use the command line to manage automatically. Command management tools are supported by a series of modules and parameters. The basic grammar is as follows:

ansible  <host-pattern>  [ –m  module_name ]  [ –a  args]

<host-pattern> : Which hosts are valid

[ –m  module_name ]  :Specify the module to be used

[-a args ]  :Module-specific parameters

Ansible software execution results

l. The output is green: it indicates successful execution without any change.

(l) Output content display * * indicates successful execution, but changes have been made to the managed host

The output is shown in red: Fai l ure to execute!!!

The use environment is as follows:


Role Host name IP address Group name
Control host     ansible       192.168.66.138
Managed Host     node1       192.168.66.141        webserver
Managed Host     node2       192.168.66.142        mysql

Using ansible-doc to view module busy information tools, the most important option-l to list available modules, and-s to accumulate the description information and use cases of a module. For example, the description information and operation actions of the command module are listed.

[root@promote .ssh]# ansible-doc -s command
- name: Executes a command on a remote node
   command:
       argv:                  # Allows the user to provide the command as a list
                                vs. a string.  Only
                                the string or the
                                list form can be
                                provided, not both.
                                One or the other
                                must be provided.
       chdir:                 # Change into this directory before running the
                                command.
       creates:               # A filename or (since 2.0) glob pattern. If it
                                already exists, this
                                step *won't* be run.
                                      ………

Ansible has a lot of modules that can download and execute various tasks of Ansible. First, look at the common core modules.

1. command module

The Ansible management tool uses the m option to specify the use module, and defaults to the command module, i.e., the m option omission can run the module for running commands on the managed host.

For example, the date command is executed on the managed host to display the time of the managed host. There are three ways to execute commands to manage hosts written to the host list.

(1) Use IP address to specify the running host

[root@promote .ssh]# ansible 192.168.66.141 -m command -a 'date'           //-m Designate modules, –a  Module-specific parameters
192.168.66.141 | CHANGED | rc=0 >>
2018 Saturday, October 20, 2001:58:18 CST

(2) Using classified operations in managed hosts

[root@promote .ssh]# ansible mysql -m command -a 'date'
192.168.66.142 | CHANGED | rc=0 >>
2018 Saturday, October 20, 2001:58:43 CST

(3) Host running in all host lists, using all

[root@promote .ssh]# ansible all -m command -a 'date'
192.168.66.141 | CHANGED | rc=0 >>
2018 Saturday, October 20, 2001:59:20 CST

192.168.66.142 | CHANGED | rc=0 >>
2018 Saturday, October 20, 2001:59:22 CST

2. cron module

The cron module in Ansible is used to define task plans. There are two state s: press for addition (default when omitted), absent for removal.

(1) Adding planned tasks. Output a file named "test haha" to all managed hosts every minute in / bin / directory

[root@ansible .ssh]# ansible all -m cron -a 'minute="*/1" job="/bin/echo haha" name="test haha"'
192.168.66.141 | CHANGED => {             //Expressing successful implementation
     "changed": true,
     "envs": [],
     "jobs": [
         "test haha"
     ]
}
192.168.66.142 | CHANGED => {
     "changed": true,
     "envs": [],
     "jobs": [
         "test haha"
     ]
}

View planned tasks

[root@ansible .ssh]# ansible all -a 'crontab -l'
192.168.66.141 | CHANGED | rc=0 >>
#Ansible: test haha
*/1 * * * * /bin/echo haha

192.168.66.142 | CHANGED | rc=0 >>
#Ansible: test haha
*/1 * * * * /bin/echo haha

View on the managed host

[root@node1 bin]# which echo
/usr/bin/echo
You are in /var/spool/mail/root There are new emails
[root@node1 bin]# crontab -l
#Ansible: test haha
*/1 * * * * /bin/echo haha

[root@node2 .ssh]# which echo
/usr/bin/echo
You are in /var/spool/mail/root There are new emails
[root@node2 .ssh]# crontab -l
#Ansible: test haha
*/1 * * * * /bin/echo haha

(2) Remove planned tasks

[root@ansible ~]# ansible all -m cron -a 'name="test haha" state=absent'
192.168.66.141 | CHANGED => {
     "changed": true,
     "envs": [],
     "jobs": []
}
192.168.66.142 | CHANGED => {
     "changed": true,
     "envs": [],
     "jobs": []
}

3. user module

The user module in Ansible is used to create new users and to change and delete existing users. The name option is used to specify the creation user name.

(1) Create users. Create a user named "test01" on all managed hosts

[root@ansible ~]# ansible all -m user -a 'name="test01"'           //Name specifies the creation of a user name
192.168.66.141 | CHANGED => {
     "changed": true,
     "comment": "",
     "create_home": true,
     "group": 1002,
     "home": "/home/test01",
     "name": "test01",
     "shell": "/bin/bash",
     "state": "present",
     "system": false,
     "uid": 1002
}
192.168.66.142 | CHANGED => {
     "changed": true,
     "comment": "",
     "create_home": true,
     "group": 1001,
     "home": "/home/test01",
     "name": "test01",
     "shell": "/bin/bash",
     "state": "present",
     "system": false,
     "uid": 1001
}

Check whether users are created on the managed host

[root@node1 ~]# id test01
uid=1001(test01) gid=1001(test01) group=1001(test01)

[root@node2 ~]# id test01
uid=1002(test01) gid=1002(test01) group=1002(test01)

(2) Delete users

[root@ansible ~]# ansible mysql -m user -a 'name=test01 state=absent'       //Delete the user of the managed host 1 mysql, state absent means removal
192.168.66.142 | CHANGED => {
     "changed": true,
     "force": false,
     "name": "test01",
     "remove": false,
     "state": "absent"
}

Query the "test01" user on the managed host for deletion

[root@node1 ~]# id test01
id: test01: no such user

4. group module

The group module in Ansible is used to manage user groups.

(1) Create a test02 group and add test02 users to the test02 group

[root@ansible ~]# ansible mysql -m group -a 'name=test02 gid=306 system=yes'
192.168.66.142 | CHANGED => {
     "changed": true,
     "gid": 306,
     "name": "test02",
     "state": "present",
     "system": true
}

View the added groups on the managed host

[root@ansible ~]# vim /etc/ansible/hosts
[root@ansible ~]# ansible mysql -a 'tail /etc/group'
192.168.66.142 | CHANGED | rc=0 >>
avahi:x:70:
slocate:x:21:
postdrop:x:90:
postfix:x:89:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
liu:x:1000:
test02:x:306:

(2) user modules can also be used to create users and add them to groups.

[root@ansible ~]# ansible webserver -m user -a 'name=mysql uid=306 group=mysql system=yes'
192.168.66.141 | CHANGED => {
     "append": false,
     "changed": true,
     "comment": "",
     "group": 1001,
     "home": "/home/mysql",
     "move_home": false,
     "name": "mysql",
     "shell": "/sbin/nologin",
     "state": "present",
     "uid": 306
}

View the created user group

[root@ansible ~]# ansible webserver -a 'id mysql'
192.168.66.141 | CHANGED | rc=0 >>
uid=306(mysql) gid=1001(mysql) group=1001(mysql)

[root@node2 ~]# id mysql
uid=306(mysql) gid=1001(mysql) group=1001(mysql)

5. copy module

Copy module in Ansible, which is used to copy files and distribute them in batches. src is used to define the local source file path, dest is used to define the managed host file path, and content is used to specify information content to generate the target file.

(1) Copy the local file / etc/fstab to / opt/fstab.bk on the managed host and set the owner to root with 644 privileges.

[root@ansible ~]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=644'
192.168.66.142 | CHANGED => {                      //src defines the local source file path and dest defines the managed host file path
     "changed": true,
     "checksum": "cb9821536bd491d110542bdf799c0828715ab6bd",
     "dest": "/opt/fstab.bk",
     "gid": 0,
     "group": "root",
     "md5sum": "643872130cf520bc6762e1c5d803a890",
     "mode": "0644",
     "owner": "root",
     "secontext": "system_u:object_r:usr_t:s0",
     "size": 689,
     "src": "/root/.ansible/tmp/ansible-tmp-1540103669.94-50253022890998/source",
     "state": "file",
     "uid": 0
}

View replica files

[root@ansible ~]# ansible mysql -a 'ls -l /opt'
192.168.66.142 | CHANGED | rc=0 >>
Total dosage 4
-rw-r--r--. 1 root root 689 10 Month 2114:34 fstab.bk
drwxr-xr-x. 2 root root   6 3 month  26 2015 rh

Query on the managed host

[root@node2 ~]# cd /opt/
[root@node2 opt]# ls -l

Total dosage 4
-rw-r--r--. 1 root root 689 10 Month 2114:34 fstab.bk
drwxr-xr-x. 2 root root   6 3 month  26 2015 rh

(2) Write "this is test" into the / opt/test.txt file

[root@ansible ~]# ansible mysql -m copy -a 'content="this is test" dest=/opt/test.txt'  
192.168.66.142 | CHANGED => {                        //Content specifies the information content to generate the target file
     "changed": true,
     "checksum": "b6794b2000d94d348203d0279c2e7322b922cb16",
     "dest": "/opt/test.txt",
     "gid": 0,
     "group": "root",
     "md5sum": "8c6d115258631625b625486f81b09532",
     "mode": "0644",
     "owner": "root",
     "secontext": "system_u:object_r:usr_t:s0",
     "size": 12,
     "src": "/root/.ansible/tmp/ansible-tmp-1540104362.42-107935433980182/source",
     "state": "file",
     "uid": 0
}

View the generated file

[root@ansible ~]# ansible mysql -a 'cat /opt/test.txt'
192.168.66.142 | CHANGED | rc=0 >>
this is test

Query on the managed host

[root@node2 opt]# ls
fstab.bk  rh  test.txt
[root@node2 opt]# cat test.txt
this is test[root@node2 opt]#

6. file module

File module -- used to set file properties. Where path is used to specify the file path, src is used to define the source file path, and name or dest is used to replace the symbolic link to create the file.

(1) Set the file / opt/fstab.bk to belong to mysql, belong to mysql, permission is 666

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/fstab.bk owner=mysql group=mysql mode=666'
192.168.66.142 | CHANGED => {                    //Path specifies file path
     "changed": true,
     "gid": 1001,
     "group": "mysql",
     "mode": "0666",
     "owner": "mysql",
     "path": "/opt/fstab.bk",
     "secontext": "system_u:object_r:usr_t:s0",
     "size": 689,
     "state": "file",
     "uid": 306
}

View on the managed host

[root@node2 opt]# ls -la
Total dosage 8
drwxr-xr-x.  3 root  root   48 10 Month 2114:46 .
dr-xr-xr-x. 18 root  root  235 8 month  22 18:33 ..
-rw-rw-rw-.  1 mysql mysql 689 10 Month 2114:34 fstab.bk      //Owner, subgroup mysql, permission 666
drwxr-xr-x.  2 root  root    6 3 month  26 2015 rh
-rw-r--r--.  1 root  root   12 10 Month 2114:46 test.txt

(2) Set the file / opt/fstab.txt.link to the file / opt/fstab.bk

[root@ansible ~]# ansible mysql -m file -a 'src=/opt/fstab.bk path=/opt/fstab.txt.link state=link'
192.168.66.142 | CHANGED => {                   //src Define the source file path                                     //The state is link
     "changed": true,
     "dest": "/opt/fstab.txt.link",
     "gid": 0,
     "group": "root",
     "mode": "0777",
     "owner": "root",
     "secontext": "unconfined_u:object_r:usr_t:s0",
     "size": 13,
     "src": "/opt/fstab.bk",
     "state": "link",
     "uid": 0
}

[root@node2 opt]# ls
fstab.bk  fstab.txt.link  rh  test.txt

(3) Create empty files. The state is touch

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/abc.txt state=touch'
192.168.66.142 | CHANGED => {
     "changed": true,
     "dest": "/opt/abc.txt",
     "gid": 0,
     "group": "root",
     "mode": "0644",
     "owner": "root",
     "secontext": "unconfined_u:object_r:usr_t:s0",
     "size": 0,
     "state": "file",
     "uid": 0
}

[root@node2 opt]# ls
abc.txt  fstab.bk  fstab.txt.link  rh  test.txt
[root@node2 opt]# cat abc.txt                 //Empty file
[root@node2 opt]#

(4) Create directories. The status is directory

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/abc state=directory'
192.168.66.142 | CHANGED => {
     "changed": true,
     "gid": 0,
     "group": "root",
     "mode": "0755",
     "owner": "root",
     "path": "/opt/abc",
     "secontext": "unconfined_u:object_r:usr_t:s0",
     "size": 6,
     "state": "directory",
     "uid": 0
}

(5) Delete files.

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/abc.txt state=absent'
192.168.66.142 | CHANGED => {
     "changed": true,
     "path": "/opt/abc.txt",
     "state": "absent"
}

[root@node2 opt]# ls
abc  fstab.bk  fstab.txt.link  rh  test.txt

7. ping module

Use the ping module in Ansible to check the connectivity of the specified host.

[root@ansible ~]# ansible all -m ping
192.168.66.142 | SUCCESS => {
     "changed": false,
     "ping": "pong"
}
192.168.66.141 | SUCCESS => {
     "changed": false,
     "ping": "pong"
}

8. yum module

The yum module in Ansible is responsible for installing and uninstalling software packages on and on managed masters. Where name specifies the package to be installed, state specifies the status of the package to be installed, present, latest denotes installation, absent denotes uninstallation.

(1) Installing httpd packages on managed hosts

[root@node1 ~]# rpm -q httpd               
Uninstalled package httpd                                              //You can see that httpd services are installed on managed hosts

[root@node2 opt]# rpm -q httpd
Uninstalled package httpd

Installation prompts will not appear during installation, and longer installation information will be displayed only after installation is completed.

[root@ansible ~]# ansible all -m yum -a 'name=httpd'
192.168.66.141 | CHANGED => {             //After the first host is installed
     "ansible_facts": {
         "pkg_mgr": "yum"
     },
     "changed": true,
     "msg": "warning: /var/cache/yum/x86_64/7/base/packages/mailcap-2.1.41-2.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY\nImporting GPG key 0xF4A80EB5:\n Userid     : \"CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>\"\n Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5\n Package    : centos-release-7-4.1708.el7.centos.x86_64 (@anaconda)\n From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7\n",
     "rc": 0,
     "results": [
         "Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: centos.ustc.edu.cn\n * extras: centos.ustc.edu.cn\n * updates: centos.ustc.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-80.el7.centos.1 will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-80.el7.centos.1 for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed\n---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package           Arch         Version                     Repository     Size\n================================================================================\nInstalling:\n httpd             x86_64       2.4.6-80.el7.centos.1       updates       2.7 M\nInstalling for dependencies:\n apr               x86_64       1.4.8-3.el7_4.1             base          103 k\n apr-util          x86_64       1.5.2-6.el7                 base           92 k\n httpd-tools       x86_64       2.4.6-80.el7.centos.1       updates        90 k\n mailcap           noarch       2.1.41-2.el7                base           31 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\nDownloading packages:\nPublic key for mailcap-2.1.41-2.el7.noarch.rpm is not installed\nPublic key for httpd-tools-2.4.6-80.el7.centos.1.x86_64.rpm is not installed\n--------------------------------------------------------------------------------\nTotal                                              1.6 MB/s | 3.0 MB  00:01     \nRetrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : apr-1.4.8-3.el7_4.1.x86_64                                   1/5 \n  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5 \n  Installing : httpd-tools-2.4.6-80.el7.centos.1.x86_64                     3/5 \n  Installing : mailcap-2.1.41-2.el7.noarch                                  4/5 \n  Installing : httpd-2.4.6-80.el7.centos.1.x86_64                           5/5 \n  Verifying  : mailcap-2.1.41-2.el7.noarch                                  1/5 \n  Verifying  : httpd-tools-2.4.6-80.el7.centos.1.x86_64                     2/5 \n  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  3/5 \n  Verifying  : apr-1.4.8-3.el7_4.1.x86_64                                   4/5 \n  Verifying  : httpd-2.4.6-80.el7.centos.1.x86_64                           5/5 \n\nInstalled:\n  httpd.x86_64 0:2.4.6-80.el7.centos.1                                          \n\nDependency Installed:\n  apr.x86_64 0:1.4.8-3.el7_4.1                  apr-util.x86_64 0:1.5.2-6.el7   \n  httpd-tools.x86_64 0:2.4.6-80.el7.centos.1    mailcap.noarch 0:2.1.41-2.el7   \n\nComplete!\n"
     ]
}
192.168.66.142 | CHANGED => {             //After the installation of the second host is completed
     "ansible_facts": {
         "pkg_mgr": "yum"
     },
     "changed": true,
     "msg": "http://ftp.sjtu.edu.cn/centos/7.5.1804/os/x86_64/Packages/mailcap-2.1.41-2.el7.noarch.rpm: [Errno 14] curl#7 - \"Failed to connect to 2001:da8:8000:6023::230: Network is unreachable\"\nTrying other mirror.\n",
     "rc": 0,
     "results": [
         "Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.nju.edu.cn\n * extras: mirrors.163.com\n * updates: mirrors.nju.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-80.el7.centos.1 will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-80.el7.centos.1 for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed\n---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package           Arch         Version                     Repository     Size\n================================================================================\nInstalling:\n httpd             x86_64       2.4.6-80.el7.centos.1       updates       2.7 M\nInstalling for dependencies:\n apr               x86_64       1.4.8-3.el7_4.1             base          103 k\n apr-util          x86_64       1.5.2-6.el7                 base           92 k\n httpd-tools       x86_64       2.4.6-80.el7.centos.1       updates        90 k\n mailcap           noarch       2.1.41-2.el7                base           31 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal                                              198 kB/s | 3.0 MB  00:15     \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : apr-1.4.8-3.el7_4.1.x86_64                                   1/5 \n  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5 \n  Installing : httpd-tools-2.4.6-80.el7.centos.1.x86_64                     3/5 \n  Installing : mailcap-2.1.41-2.el7.noarch                                  4/5 \n  Installing : httpd-2.4.6-80.el7.centos.1.x86_64                           5/5 \n  Verifying  : mailcap-2.1.41-2.el7.noarch                                  1/5 \n  Verifying  : httpd-tools-2.4.6-80.el7.centos.1.x86_64                     2/5 \n  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  3/5 \n  Verifying  : apr-1.4.8-3.el7_4.1.x86_64                                   4/5 \n  Verifying  : httpd-2.4.6-80.el7.centos.1.x86_64                           5/5 \n\nInstalled:\n  httpd.x86_64 0:2.4.6-80.el7.centos.1                                          \n\nDependency Installed:\n  apr.x86_64 0:1.4.8-3.el7_4.1                  apr-util.x86_64 0:1.5.2-6.el7   \n  httpd-tools.x86_64 0:2.4.6-80.el7.centos.1    mailcap.noarch 0:2.1.41-2.el7   \n\nComplete!\n"
     ]
}

Check whether the httpd service is installed on the managed host.

[root@node1 ~]# rpm -q httpd
httpd-2.4.6-80.el7.centos.1.x86_64

[root@node2 opt]# rpm -q httpd
httpd-2.4.6-80.el7.centos.1.x86_64

(2) Unloading http packages

[root@ansible ~]# Ansible webserver - M Yum - a'name = httpd state = absent'// uninstall httpd services on one of the hosts.
192.168.66.141 | CHANGED => {
     "ansible_facts": {
         "pkg_mgr": "yum"
     },
     "changed": true,
     "msg": "",
     "rc": 0,
     "results": [
         "Loaded plug-ins: fastestmirror, langpacks\n Resolving dependencies\n--> Inspection of affairs under way\n---> software package httpd.x86_64.0.2.4.6-80.el7.centos.1 Will be deleted\n--> Resolve dependency completeness\n\n Dependency Resolution\n\n================================================================================\n Package      Framework          Edition                          source               Size\n================================================================================\n Deleting:\n httpd        x86_64        2.4.6-80.el7.centos.1         @updates        9.4 M\n\n Transaction summary\n================================================================================\n remove  1 software package\n\n Installation Size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Deleting    : httpd-2.4.6-80.el7.centos.1.x86_64                          1/1 \n  Verification      : httpd-2.4.6-80.el7.centos.1.x86_64                          1/1 \n\n delete:\n  httpd.x86_64 0:2.4.6-80.el7.centos.1                                          \n\n Complete!\n"
     ]
}

9. service module

Use service module in Ansible to control the running status of management service. Among them, enabled is used to indicate whether the boot is self-starting, and the value is ture or flase; name is used to define the service name; and state is used to specify the service status, starting, stopped and re started respectively.

Start the httpd service and set the boot-up self-start.

[root@ansible ~]# ansible mysql -m service -a 'name=httpd enabled=true state=started'
192.168.66.142 | CHANGED => {                                                 //Set the boot-up self-starting state to start
     "changed": true,
     "enabled": true,
     "name": "httpd",
     "state": "started",
     "status": {
         "ActiveEnterTimestampMonotonic": "0",
         "ActiveExitTimestampMonotonic": "0",
         "ActiveState": "inactive",

               …….

View the status of httpd services

[root@node2 opt]# systemctl status httpd
httpd.service - The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
    Active: active (running) since Day 2018-10-21 15:22:41 CST; 46s ago
      Docs: man:httpd(8)
            man:apachectl(8)
  Main PID: 6036 (httpd)
    Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    CGroup: /system.slice/httpd.service
            ├─6036 /usr/sbin/httpd -DFOREGROUND
            ├─6050 /usr/sbin/httpd -DFOREGROUND
            ├─6051 /usr/sbin/httpd -DFOREGROUND
            ├─6052 /usr/sbin/httpd -DFOREGROUND
            ├─6053 /usr/sbin/httpd -DFOREGROUND
            └─6056 /usr/sbin/httpd -DFOREGROUND

10 Month 2115:22:16 node2 systemd[1]: Starting The Apache HTTP Server...
10 Month 2115:22:31 node2 httpd[6036]: AH00558: httpd: Could not reliably determine the serv...age
10 Month 2115:22:41 node2 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

10. shell module

The shell module in Ansible can run commands on managed hosts and support complex commands such as pipeline symbols

Set passwords for users in non-interactive mode after creating users

[root@ansible ~]# ansible mysql -m user -a 'name=tom'
192.168.66.142 | CHANGED => {
     "changed": true,
     "comment": "",
     "create_home": true,
     "group": 1003,
     "home": "/home/tom",
     "name": "tom",
     "shell": "/bin/bash",
     "state": "present",
     "system": false,
     "uid": 1003
}
[root@ansible ~]# ansible mysql -m shell -a 'echo acb123 | passwd --stdin tom'
192.168.66.142 | CHANGED | rc=0 >>
Change user tom Password.
passwd: All authentication tokens have been successfully updated.

11. script module

script in Ansible can copy local scripts to the managed host for execution. Note that relative paths are used to specify scripts.

Edit a local script test.sh and copy it to the managed host to run.

[root@ansible ~]# cd /opt/
[root@ansible opt]# vim test.sh

#!/bin/bash
echo "this is test script" > /opt/script.txt            //Managed host script path
chmod 666 /opt/script.txt

[root@ansible opt]# chmod +x test.sh // / Give script execution permission


[root@ansible opt]# Ansible all-m script-a'test.sh'// copy to managed host
192.168.66.142 | CHANGED => {
     "changed": true,
     "rc": 0,
     "stderr": "Shared connection to 192.168.66.142 closed.\r\n",
     "stderr_lines": [
         "Shared connection to 192.168.66.142 closed."
     ],
     "stdout": "",
     "stdout_lines": []
}
192.168.66.141 | CHANGED => {
     "changed": true,
     "rc": 0,
     "stderr": "Shared connection to 192.168.66.141 closed.\r\n",
     "stderr_lines": [
         "Shared connection to 192.168.66.141 closed."
     ],
     "stdout": "",
     "stdout_lines": []
}

Viewing scripts on managed hosts

[root@node1 opt]# ls -l
Total dosage 4
drwxr-xr-x. 2 root root  6 3 month  26 2015 rh
-rw-rw-rw-. 1 root root 20 10 Month 2115:31 script.txt                 //Script file
[root@node1 opt]# cat script.txt                                               //View script content
this is test script

[root@node2 opt]# ls -l
Total dosage 12
drwxr-xr-x. 2 root  root    6 10 Month 2115:07 abc
-rw-rw-rw-. 1 mysql mysql 689 10 Month 2114:34 fstab.bk
lrwxrwxrwx. 1 root  root   13 10 Month 2115:00 fstab.txt.link -> /opt/fstab.bk
drwxr-xr-x. 2 root  root    6 3 month  26 2015 rh
-rw-rw-rw-. 1 root  root   20 10 Month 2115:31 script.txt
-rw-r--r--. 1 root  root   12 10 Month 2114:46 test.txt
[root@node2 opt]# cat script.txt
this is test script

12. setup module

setup module is used in Ansible to collect and view facts of managed host (facts is a function of Ansible to collect information of managed host devices). Before receiving and running commands, each managed host sends its own information (operating system version, IP address, etc.) to the control host.

[root@ansible opt]# ansible mysql -m setup
192.168.66.142 | SUCCESS => {
     "ansible_facts": {
         "ansible_all_ipv4_addresses": [
             "192.168.122.1",
             "192.168.66.142"
         ],
         "ansible_all_ipv6_addresses": [
             "fe80::7d01:50f5:b8bc:52cd"
         ],
         "ansible_apparmor": {
             "status": "disabled"
         },
         "ansible_architecture": "x86_64",
         "ansible_bios_date": "05/19/2017",
         "ansible_bios_version": "6.00",
         "ansible_cmdline": {
             "BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64",
             "LANG": "zh_CN.UTF-8",
             "quiet": true,
             "rhgb": true,
             "ro": true,
             "root": "UUID=5e874d95-9d77-4a71-8f97-454363885543"

              ……

Posted by migmedia on Sun, 05 May 2019 12:32:38 -0700