[root@ansible ~]# ansible-doc -s ping - name: Try to connect to host, verify a usable python and return `pong' on success ping: data: # Data to return for the `ping'return value. If this parameter is set to `crash', the module will cause an exception.
测试1:
使用ping模块进行测试时报错,提示错误,因为默认为key连接由于没有配置key所以报错
1 2 3 4 5 6
[root@ansible ~]# ansible 192.168.73.134 -m ping 192.168.73.134 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).", "unreachable": true }
[root@ansible ~]# ssh-copy-id 192.168.73.134 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to login 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.73.134's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.73.134'" and check to make sure that only the key(s) you wanted were added. [root@ansible ~]# ssh-copy-id 192.168.73.135 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /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.73.135's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.73.135'" and check to make sure that only the key(s) you wanted were added.
[root@ansible ~]# ansible-doc -s shell - name: Execute commands in nodes. shell: chdir: # cd into this directory before running the command creates: # a filename, when it already exists, this step will *not* be run. executable: # change the shell used to execute the command. Should be an absolute path to the executable. free_form: # (required) The shell module takes a free form command to run, as a string. There's not an actual option named "free form". See the examples! removes: # a filename, when it does not exist, this step will *not* be run. stdin: # Set the stdin of the command directly to the specified value. warn: # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false.
示例1:
使用shell来修改用户口令
1 2 3 4 5 6 7 8
[root@ansible ~]# ansible webserver -m shell -a 'echo 123456 | passwd --stdin masuri' 192.168.73.134 | CHANGED | rc=0 >> Changing password for user masuri. passwd: all authentication tokens updated successfully.
192.168.73.135 | CHANGED | rc=0 >> Changing password for user masuri. passwd: all authentication tokens updated successfully.
[root@ansible ~]# ansible webserver -m shell -a 'chdir=/data echo welcome to magedu > test.log' 192.168.73.135 | CHANGED | rc=0 >>
192.168.73.134 | CHANGED | rc=0 >>
#查看重定向是否成功 [root@ansible ~]# ansible webserver -m shell -a 'chdir=/data cat test.log' 192.168.73.135 | CHANGED | rc=0 >> welcome to magedu
192.168.73.134 | CHANGED | rc=0 >> welcome to magedu
示例4:
使用sed对文件进行修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[root@ansible ~]# ansible webserver -m shell -a 'sed -i "s/welcome to magedu/welcome to mylinuxops/" /data/test.log' [WARNING]: Consider using the replace, lineinfile or template module rather than running 'sed'. If you need to use command because replace, lineinfile or template is insufficient you can add 'warn: false' to this command task or set'command_warnings=False'in ansible.cfg to get rid of this message.
192.168.73.134 | CHANGED | rc=0 >>
192.168.73.135 | CHANGED | rc=0 >>
#验证是否修改 [root@ansible ~]# ansible webserver -m shell -a 'cat /data/test.log' 192.168.73.134 | CHANGED | rc=0 >> welcome to mylinuxops
192.168.73.135 | CHANGED | rc=0 >> welcome to mylinuxops
#抓取目录分两步走,先打包 [root@ansible ~]# ansible webserver -a 'tar Jcf /data/etc.tar.xz /etc' [WARNING]: Consider using the unarchive module rather than running 'tar'. If you need to use command because unarchive is insufficient you can add 'warn: false' to this command task or set'command_warnings=False'in ansible.cfg to get rid of this message.
192.168.73.135 | CHANGED | rc=0 >> tar: Removing leading `/' from member names 192.168.73.134 | CHANGED | rc=0 >> tar: Removing leading `/' from member names
#验证 [root@ansible data]# ansible webserver -a 'rpm -q httpd' [WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use command because yum, dnf or zypper is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False'in ansible.cfg to get rid of this message.
#验证 [root@ansible data]# ansible webserver -a 'rpm -q httpd' [WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use command because yum, dnf or zypper is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False'in ansible.cfg to get rid of this message.
192.168.73.134 | FAILED | rc=1 >> package httpd is not installednon-zero return code
192.168.73.135 | FAILED | rc=1 >> package httpd is not installednon-zero return code