Ansible installation and module management (the most complete operation, can follow

Keywords: MySQL ansible ssh CentOS

1, Ansible installation

Environmental preparation

Management end: CentOS 7-2 192.168.18.147
Managed end 1: CentOS 7-3 192.168.18.128
Managed end 2: CentOS 7-4 192.168.18.148

Management side:
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum install ansible -y
[root@localhost ~]# ansible --version
ansible 2.9.2
[root@localhost ~]# yum install tree -y
[root@localhost ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg         #configuration file
├── hosts
└── roles

1 directory, 2 files
`Configure host list`
[root@localhost ~]# vim  /etc/ansible/hosts
#Insert the following under line 24
[webserver]
192.168.18.128
[mysql]
192.168.18.148

`Generate key pair`
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):        #Direct carriage return
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):     #Enter password: abc123
Enter same passphrase again:                    #Enter the password again: abc123
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:5RvIVqbI9hscNK1Y4YivNnnUEgQeNfNm/WJcBXr8jWc root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|    oo= .   ...  |
|   . + * + o .   |
|    o o O B +    |
|     o @ @ + . o |
|      O S * . o E|
|     = = o +   o |
|    = . + .      |
|   . o   o       |
|        .        |
+----[SHA256]-----+

`Key pair location`
[root@localhost ~]# ls -la
//Total dosage 56
......
drwx------.  2 root root   38 1 Month 2217:34 .ssh
......Multiple lines are omitted here
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
id_rsa(Private key  id_rsa.pub(Public key)

`Push the key to the managed end 1`
[root@localhost .ssh]# ssh-copy-id root@192.168.18.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.18.128 (192.168.18.128)' can't be established.
ECDSA key fingerprint is SHA256:mTT+FEtzAu4X3D5srZlz93S3gye8MzbqVZFDzfJd4Gk.
ECDSA key fingerprint is MD5:fa:5a:88:23:49:60:9b:b8:7e:4b:14:4b:3f:cd:96:a0.
Are you sure you want to continue connecting (yes/no)? yes  #Confirm link
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.18.128's password:     #Enter the root password of the corresponding managed end

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.18.128'"
and check to make sure that only the key(s) you wanted were added.
`Push the key to the managed end 2`
[root@localhost .ssh]# ssh-copy-id root@192.168.18.148
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.18.148 (192.168.18.148)' can't be established.
ECDSA key fingerprint is SHA256:mTT+FEtzAu4X3D5srZlz93S3gye8MzbqVZFDzfJd4Gk.
ECDSA key fingerprint is MD5:fa:5a:88:23:49:60:9b:b8:7e:4b:14:4b:3f:cd:96:a0.
Are you sure you want to continue connecting (yes/no)? yes  #Confirm link
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.18.148's password:     #Enter the root password of the corresponding managed end

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.18.148'"
and check to make sure that only the key(s) you wanted were added.

Verify that the key was successfully pushed:

Managed end 1: 192.168.18.128
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
authorized_keys
#Key push succeeded at this time
Managed end 2:
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
authorized_keys
#Key push succeeded at this time

2, Ansible command line module

1. command module

`Use IP Address viewing time of managed end 1`
[root@localhost .ssh]# ansible 192.168.18.128 -m command -a 'date'
Enter passphrase for key '/root/.ssh/id_rsa':       #Enter the key password abc123
192.168.18.128 | CHANGED | rc=0 >>
2020 Sunday, February 2, 2015:53:20 CST
`Use alias to view the time of managed side 2`
[root@localhost .ssh]# ansible mysql -m command -a 'date'
Enter passphrase for key '/root/.ssh/id_rsa':       #Enter the key password abc123
192.168.18.148 | CHANGED | rc=0 >>
2020 Sunday, February 2, 2015:55:13 CST

`To avoid the trouble of always entering passwords, we can execute interaction free agents`
[root@localhost .ssh]# ssh-agent bash
[root@localhost .ssh]# ssh-add
Enter passphrase for /root/.ssh/id_rsa:             #Enter the key password abc123   
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

[root@localhost .ssh]# ansible webserver -m command -a 'date'
192.168.18.128 | CHANGED | rc=0 >>
2020 Sunday, February 2, 2016:01:40 CST
#In this case, the time can be displayed directly without interaction

`All hosts Host execution date command`
[root@localhost .ssh]# ansible all -a 'date'
192.168.18.128 | CHANGED | rc=0 >>
2020 Sunday, February 2, 2016:21:08 CST

192.168.18.148 | CHANGED | rc=0 >>
2020 Sunday, February 2, 2016:21:08 CST

2. cron module

Two state s: present means add (can be omitted), and absent means remove

[root@localhost .ssh]# ansible mysql -m cron -a 'minute="*/1" job="/usr/bin/echo hello"  name="test hello"'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "test hello"
    ]
}
[root@localhost .ssh]# ansible mysql -a 'crontab -l'
192.168.18.148 | CHANGED | rc=0 >>
#Ansible: test hello
*/1 * * * * /usr/bin/echo hello
At this time, we can enter the managed end 2 for verification:
[root@localhost .ssh]# crontab -l
#Ansible: test hello
*/1 * * * * /usr/bin/echo hello
//You have new messages in / var/spool/mail/root

[root@localhost .ssh]# vim /var/spool/mail/root
From root@localhost.localdomain  Sun Feb  2 16:40:02 2020
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)
        id 2255A319AE4E; Sun,  2 Feb 2020 16:40:02 +0800 (CST)
From: "(Cron Daemon)" <root@localhost.localdomain>
To: root@localhost.localdomain
Subject: Cron <root@localhost> /usr/bin/echo hello
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=19>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=zh_CN.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20200202084002.2255A319AE4E@localhost.localdomain>
Date: Sun,  2 Feb 2020 16:40:02 +0800 (CST)

hello
#Several lines are omitted below, and one is generated every minute
In this case, remove the scheduled task and use absent:
[root@localhost .ssh]# ansible mysql -m cron -a 'name="test hello" state=absent'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "envs": [],
    "jobs": []
}

`In this case, go back to managed end 2 and you will find that the planned task disappears`
[root@localhost ~]# crontab -l

3. user module

The user module requests three instructions: useradd, userdel and usermod

`Create user test01`
[root@localhost ~]# ansible all -m user -a 'name=test01'
192.168.18.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/test01",
    "name": "test01",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/test01",
    "name": "test01",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
`At this time, go back to the managed end 1 to verify adding users`
[root@localhost ~]# id test01
uid=1001(test01) gid=1001(test01) group=1001(test01)
`At this time, go back to the managed end 2 to verify the addition of users`
[root@localhost ~]# id test01
uid=1001(test01) gid=1001(test01) group=1001(test01)
#At this time, both test01 users of the managed end are added successfully

`delete webserver In the middle test01 user`
[root@localhost ~]# ansible webserver -m user -a 'name=test01 state=absent'
192.168.18.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "force": false,
    "name": "test01",
    "remove": false,
    "state": "absent"
}
`Now return to managed end 1:webserver Verify user status in`
[root@localhost ~]# id test01
id: test01: no such user
#The display cannot be found, indicating that it has been deleted

3. group module

group module requests three instructions: groupadd, groupdel and groupmod

`Establish mysql group`
[root@localhost ~]# ansible mysql -m group -a 'name=mysql gid=306 system=yes'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 306,
    "name": "mysql",
    "state": "present",
    "system": true
}
`Remote viewing managed end 2:mysql Are there any mysql group`
[root@localhost ~]# ansible mysql -a 'tail /etc/group'
192.168.18.148 | CHANGED | rc=0 >>
postfix:x:89:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
zhou:x:1000:
dhcpd:x:177:
named:x:25:
test01:x:1001:
mysql:x:306:        #At this time, there are mysql groups, and the gid number is 306

`Create new user test02 And add to mysql group`
[root@localhost ~]# ansible mysql -m user -a 'name=test02 uid=306 group=mysql system=yes'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 306,
    "home": "/home/test02",
    "name": "test02",
    "shell": "/bin/bash",
    "state": "present",
    "system": true,
    "uid": 306
}
`Remote viewing managed end 2:mysql Is China in? mysql Whether the group has newly created users test02`
[root@localhost ~]# ansible mysql -a 'id test02'
192.168.18.148 | CHANGED | rc=0 >>
uid=306(test02) gid=306(mysql) group=306(mysql)

4. copy module

`Remote handle managed end 2:mysql Medium etc Directory fstab Auto mount files, copying to opt Directory and named fstab.bk,Belong to group root,The permissions are 644.`
[root@localhost ~]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=644'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "100f3bbf6644926857bbec2a40ab2f70bf1c060b",
    "dest": "/opt/fstab.bk",
    "gid": 0,
    "group": "root",
    "md5sum": "f57167de0e8f6f2963771a72af8a2840",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:usr_t:s0",
    "size": 595,
    "src": "/root/.ansible/tmp/ansible-tmp-1580693038.81-171191249824445/source",
    "state": "file",
    "uid": 0
}
`Remote viewing managed end 2:mysql Of opt Is the directory in mysql Is there any group? fstab.bk file`
[root@localhost ~]# ansible mysql -a 'ls -l /opt'
192.168.18.148 | CHANGED | rc=0 >>
//Total dosage 4
-rw-r--r--. 1 root root 595 2 Month 309:24 fstab.bk
drwxr-xr-x. 2 root root   6 3 Month 262015 rh

`Specified content this is test,Redirect generate new file test.txt stay opt Directory`
[root@localhost ~]# ansible mysql -m copy -a 'content="this is test" dest=/opt/test.txt'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "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-1580693472.89-123279558248268/source",
    "state": "file",
    "uid": 0
}
`Remote viewing managed end 2:mysql Of opt Directory test.txt Whether the content in the file is this is test`
[root@localhost ~]# ansible mysql -a 'cat /opt/test.txt'
192.168.18.148 | CHANGED | rc=0 >>
this is test

5. file module

`Route opt Next file test.txt,User is test02,Group specified as mysql,The permissions are 666.`
[root@localhost ~]# ansible mysql -m file -a 'path=/opt/test.txt owner=test02 group=mysql mode=666'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "gid": 306,
    "group": "mysql",
    "mode": "0666",
    "owner": "test02",
    "path": "/opt/test.txt",
    "secontext": "system_u:object_r:usr_t:s0",
    "size": 12,
    "state": "file",
    "uid": 306
}
`Now return to managed end 2:mysql in opt Directory test.txt Details of documents`
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls -l
//Total dosage 8
-rw-r--r--. 1 root   root  595 2 Month 309:24 fstab.bk
drwxr-xr-x. 2 root   root    6 3 Month 262015 rh
-rw-rw-rw-. 1 test02 mysql  12 2 Month 309:31 test.txt
#At this time, the test.txt file owner is test02, the group is mysql, and the permission is 666

`Set up/opt/test.txt.link by/opt/test.txt Linked files for`
[root@localhost ~]# ansible mysql -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "dest": "/opt/test.txt.link",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:usr_t:s0",
    "size": 13,
    "src": "/opt/test.txt",
    "state": "link",
    "uid": 0
}
`Now return to managed end 2:mysql in opt View this linked file in the directory`
[root@localhost opt]# ls -l
//Total dosage 8
-rw-r--r--. 1 root   root  595 2 Month 309:24 fstab.bk
drwxr-xr-x. 2 root   root    6 3 Month 262015 rh
-rw-rw-rw-. 1 test02 mysql  12 2 Month 309:31 test.txt
lrwxrwxrwx. 1 root   root   13 2 Month 309:59 test.txt.link -> /opt/test.txt  #Linked files

`Create an empty file`
[root@localhost ~]# ansible mysql -m file -a 'path=/opt/abc.txt state=touch'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "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
}
`Now return to managed end 2:mysql in opt Directory abc.txt Details of documents`
[root@localhost opt]# ls                #The abc.txt file is available
abc.txt  fstab.bk  rh  test.txt  test.txt.link
[root@localhost opt]# cat abc.txt       #There is no content because it is an empty file

`Delete created abc.txt Empty file`
[root@localhost ~]# ansible mysql -m file -a 'path=/opt/abc.txt state=absent'
192.168.18.148 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "path": "/opt/abc.txt",
    "state": "absent"
}
`Now return to managed end 2:mysql in opt Is there any in the directory abc.txt file`
[root@localhost opt]# ls
fstab.bk  rh  test.txt  test.txt.link

6. ping module

`Test whether two managed terminals are online`
[root@localhost ~]# ansible all -m ping
192.168.18.148 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.18.128 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

7. yum module

`On managed side 1:webserver Installation in China httpd service`
[root@localhost ~]# ansible webserver -m yum -a 'name=httpd'
192.168.18.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "installed": [
            "httpd"
        ]
    },
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.huaweicloud.com\n * extras: mirror.bit.edu.cn\n * updates: mirror.bit.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-90.el7.centos.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-5.el7 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-90.el7.centos 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-90.el7.centos         base        2.7 M\nInstalling for dependencies:\n apr                x86_64        1.4.8-5.el7                 base        103 k\n apr-util           x86_64        1.5.2-6.el7                 base         92 k\n httpd-tools        x86_64        2.4.6-90.el7.centos         base         91 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                                              1.0 MB/s | 3.0 MB  00:03     \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : apr-1.4.8-5.el7.x86_64                                       1/5 \n  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5 \n  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                       3/5 \n  Installing : mailcap-2.1.41-2.el7.noarch                                  4/5 \n  Installing : httpd-2.4.6-90.el7.centos.x86_64                             5/5 \n  Verifying  : apr-1.4.8-5.el7.x86_64                                       1/5 \n  Verifying  : mailcap-2.1.41-2.el7.noarch                                  2/5 \n  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                       3/5 \n  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  4/5 \n  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                             5/5 \n\nInstalled:\n  httpd.x86_64 0:2.4.6-90.el7.centos                                            \n\nDependency Installed:\n  apr.x86_64 0:1.4.8-5.el7                     apr-util.x86_64 0:1.5.2-6.el7    \n  httpd-tools.x86_64 0:2.4.6-90.el7.centos     mailcap.noarch 0:2.1.41-2.el7    \n\nComplete!\n"
    ]
}
#The process is displayed when the installation is complete
`At this time, you can return to the managed end 1:webserver Mid view httpd Whether the service is installed successfully`
[root@localhost ~]# rpm -q httpd
httpd-2.4.6-90.el7.centos.x86_64        #The service installation is complete

`You can remove the service with the following command`
[root@localhost ~]# ansible webserver -m yum -a 'name=httpd state=absent'

8. service module

`Start the httpd service`
[root@localhost ~]# ansible webserver -m service -a 'enabled=true name=httpd state=started'
192.168.18.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
......Multiple lines are omitted here
`Now to managed end 1:webserver Mid view httpd Status of the service`
[root@localhost ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since One 2020-02-03 10:24:28 CST; 2min 25s ago
#The status is displayed as running

9. shell module

`Create a user to generate an interactive password for the user`
[root @ localhost ~] (ansible webserver - M user - a 'name = Jerry'; create a new user Jerry
192.168.18.128 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/jarry",
    "name": "jarry",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
[root@localhost ~]# ansible webserver -m shell -a 'echo abc123 | passwd --stdin jarry'
192.168.18.128 | CHANGED | rc=0 >>
Change the password of user Jerry.
passwd: all authentication tokens have been successfully updated.
#The login password of the generated Jerry user is abc123

10. script module

`First, write a script on the management side`
[root@localhost ~]# cd /opt/
[root@localhost opt]# vim test.sh
#!/bin/bash
echo "this is test script" > /opt/script.txt
chmod 666 /opt/script.txt
[root@localhost opt]# ls
rh  test.sh
[root@localhost opt]# chmod +x test.sh      #Give Execution Authority

`Execute for all managed ends test.sh Script`
[root@localhost opt]# ansible all -m script -a 'test.sh'
192.168.18.128 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.18.128 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.18.128 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}
192.168.18.148 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.18.148 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.18.148 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}
`Verify two managed end's opt Is there any in the directory script.txt file`
#Managed end 1:
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
rh  script.txt
[root@localhost opt]# cat script.txt
this is test script
#Managed end 2:
[root@localhost opt]# ls
fstab.bk  rh  script.txt  test.txt  test.txt.link
[root@localhost opt]# cat script.txt
this is test script

11. setup module

`List managed end 2:mysql All host information for`
[root@localhost opt]# ansible mysql -m setup
192.168.18.148 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.122.1",
            "192.168.18.148"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::1cb1:b734:7f72:576f",
            "fe80::578f:4368:6a2c:80d7",
            "fe80::6a0c:e6a0:7978:3543"
        ],
        "ansible_apparmor": {
            "status": "disabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "07/29/2019",
        "ansible_bios_version": "6.00",
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64",
            "LANG": "zh_CN.UTF-8",
            "crashkernel": "auto",
            "quiet": true,
            "rhgb": true,
            "ro": true,
            "root": "UUID=32c169ff-9bf7-4d89-a2f1-a99a7e59d4f2"
        },
        "ansible_date_time": {
            "date": "2020-02-03",
            "day": "03",
            "epoch": "1580698171",
            "hour": "10",
            "iso8601": "2020-02-03T02:49:31Z",
            "iso8601_basic": "20200203T104931948449",
            "iso8601_basic_short": "20200203T104931",
            "iso8601_micro": "2020-02-03T02:49:31.948682Z",
            "minute": "49",
            "month": "02",
            "second": "31",
            "time": "10:49:31",
            "tz": "CST",
            "tz_offset": "+0800",
            "weekday": "Monday",
            "weekday_number": "1",
            "weeknumber": "05",
            "year": "2020"
        },
        "ansible_default_ipv4": {
            "address": "192.168.18.148",
            "alias": "ens33",
            "broadcast": "192.168.18.255",
            "gateway": "192.168.18.2",
            "interface": "ens33",
            "macaddress": "00:0c:29:79:45:8e",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "192.168.18.0",
            "type": "ether"
        },
......Several lines are omitted below

Posted by santhosh_89 on Mon, 03 Feb 2020 07:34:46 -0800