39day ansible模块

12.ansible模块查看和帮助*****

查找模块

ansible-doc -l          #模块就Linux命令了。

查看某个模块的具体参数帮助

ansible-doc -s command  #Linux命令参数

12.1 command模块 *****

1)功能说明:

command  Executes a command on a remote node

功能说明:执行一个命令在远程节点上

操作实践:

ansible oldboy -m command -a "free -m"

ansible oldboy -m command -a "df -h"

ansible oldboy -m command -a "ls /root"

ansible oldboy -m command -a "cat redhat-release"

ansible oldboy -m command -a "cat /etc/redhat-release"

最通用的功能。

[root@m01 ~]# ansible oldboy -m command -a "cat /etc/redhat-release"

172.16.1.7 | CHANGED | rc=0 >>

CentOS Linux release 7.6.1810 (Core)

172.16.1.31 | CHANGED | rc=0 >>

CentOS Linux release 7.6.1810 (Core)

172.16.1.41 | CHANGED | rc=0 >>

CentOS Linux release 7.6.1810 (Core)

[root@m01 ~]# cat /server/scripts/cmd.sh

for n in 31 41

do

  echo "=====172.16.1.$n======"

  ssh 172.16.1.$n "$1"

done

[root@m01 ~]# sh /server/scripts/cmd.sh "cat /etc/redhat-release"

=====172.16.1.31======

CentOS Linux release 7.6.1810 (Core)

=====172.16.1.41======

CentOS Linux release 7.6.1810 (Core)

特殊:不支持的东西,例如 > < | &等 $HOME,替代方案用shell模块

ansible oldboy -m shell -a "ps -ef|grep ssh"

ansible oldboy -m shell -a "echo oldboy >/tmp/a.log"

2)常用参数说明及实践

[root@m01 ~]# 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.

      free_form:            # (required) The command module takes a free form command to run.  There is no

                              parameter actually named 'free form'. See the

                              examples!

      removes:              # A filename or (since 2.0) glob pattern. If it already exists, this step *will*  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'.

参数:chdir=/tmp配置相当于cd /tmp

[root@m01 ~]# ansible oldboy  -m command -a "pwd chdir=/etc"

ansible oldboy  -m shell -a "cd /etc/;pwd"

参数:creates=/etc  相当于条件测试  [ -e /etc ]||pwd 和下面removes相反

[root@m01 ~]# ansible oldboy  -m command -a "pwd creates=/etc"

参数:removes=/root 相当于条件测试 [ -e /root ]&&ls /root

ansible oldboy  -m command -a "ls /root removes=/root"

ansible oldboy  -m shell -a "[ -d /etc ]||pwd"

[root@m01 ~]# ansible oldboy  -m command -a "cat /etc/hosts removes=/etc/hosts"

参数:warn=False 忽略警告

[root@m01 ~]# ansible oldboy  -m command -a "chmod 000 /etc/hosts warn=False"

12.2 script模块功能说明:

功能说明:在远程节点上运行本地脚本

官方链接:http://docs.ansible.com/ansible/latest/script_module.html

远端可以没有脚本,本地有就行:

[root@m01 /server/scripts]# cat setup.sh

pwd

ls /root

for n in {1..100}

do

  echo $n >>/tmp/oldboy.log

done

执行:

ansible oldboy  -m script -a "/server/scripts/setup.sh"

项目实践作业:

写好rsync一键客户端配置,一键服务端配置。

12.3 copy模块功能说明:

copy模块功能说明:

功能说明:复制文件到远程主机

官方链接:http://docs.ansible.com/ansible/latest/copy_module.html

ansible oldboy  -m copy -a "src=/server dest=/ mode=ugo+x  group=root owner=root"

ansible oldboy  -m copy -a "src=/server/scripts/setup dest=/server/scripts mode=ugo+x  group=root owner=root backup=yes"

12.4 shell模块功能说明:

功能说明:执行一个命令在远程节点上

官方链接:http://docs.ansible.com/ansible/latest/shell_module.html

ansible oldboy  -m shell -a "free -m|grep buffer"

远程执行脚本:脚本必须在远端存在

ansible oldboy  -m shell -a "/bin/bash /server/scripts/setup.sh"

项目实践作业:

1、写好rsync一键客户端配置,一键服务端配置。

2、写好nfs一键服务端端配置,一键客户端挂载,并且加到自启动文件里(/etc/rc.local,/etc/fstab)。

步骤:

1、远端命令行非交互实现

echo 123456|passwd --stdin oldboy

2、所有步骤放在脚本里实现。

3、管理机上远程执行。

ssh /bin/sh /server/scripts/ins.sh

知识----能力-----价值-----金钱

学习方法:

学习能力

解决思路

任何问题有方法。

12.5 file模块功能说明:

功能说明:设置文件属性

官方链接:http://docs.ansible.com/ansible/latest/file_module.html

================================================================

替代方案:

ansible oldboy  -m command -a "chmod 777 /etc/hosts warn=false"

ansible oldboy  -m command -a "chmod 644 /etc/hosts warn=false"

ansible oldboy  -m command -a "chown oldboy /etc/hosts warn=false"

ansible oldboy  -m command -a "chown root /etc/hosts warn=false"

创建目录:mkdir /tmp/oldboy_dir

ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"

递归设置权限:

ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory mode=644 recurse=yes"

创建文件:touch /tmp/oldboy_file

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"

删除文件:rm -f /tmp/oldboy_file

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=absent"

创建链接文件:ln -s /etc/hosts /tmp/link_file

ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/link_file state=link"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=000"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"

12.5 yum模块功能说明:

功能说明:yum包管理模块

官方链接:http://docs.ansible.com/ansible/latest/yum_module.html

ansible oldboy -m yum -a "name=nginx state=installed"

[root@nfs01 oldboy_dir]# rpm -qa nginx

nginx-1.10.2-1.el6.x86_64

###不要用yum卸载,用rpm -e卸载。

12.6 service模块功能说明:

功能说明:启动停止服务

官方链接:http://docs.ansible.com/ansible/latest/service_module.html

#相当于

#service crond stop|/etc/init.d/crond stop

#chkconfig crond off

ansible oldboy -m service -a "name=crond state=stop enabled=no"

#相当于/etc/init.d/crond start

chkconfig crond on

ansible oldboy -m service -a "name=crond state=started enabled=yes"

ansible oldboy -m command -a "name=crond state=started enabled=yes"

有选择才叫有能力。

足球场上,让拿球队员有选择,就容易进球。

不让对方有选择,就得人盯人。

12.7 cron模块功能说明:

功能说明:管理定时任务条目信息模块

官方链接:http://docs.ansible.com/ansible/latest/cron_module.html

定时任务格式:

* * * * * CMD

创建定时任务:

ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"

结果:

#Ansible: sync time

00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1

添加如下定时任务:

05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1

命令如下:

ansible oldboy -m cron -a "name='backup data' minute=05 hour=03 job='/bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1'"

结果:

#Ansible: backup data

05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1

删除定时任务:

ansible oldboy -m cron -a "name='backup data' state=absent"

12.8 playbook

把所有ansible命令放在文件里执行就是playbook。

playbook替代方案1:

[root@m01 ~]# cat ansible.sh

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"

ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"

ansible oldboy -m yum -a "name=nginx state=installed"

ansible oldboy -m service -a "name=crond state=started enabled=yes"

ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"

playbook替代方案2:

[root@m01 ~]# cat ~/set.sh

touch /tmp/oldboy_file

chown oldboy.oldboy /tmp/oldboy_file

yum install nginx -y

/etc/init.d/crond start

chkconfig cornd on

echo '#sync time oldboy' >>/var/spool/cron/root

echo '00 00 * * *  /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root

执行:

ansible oldboy -m script -a "~/set.sh"

ansible剧本编写格式说明

ansible剧本遵循PYyaml语法规则进行编写,ymal文件基本编写规则如下说明:

规则一:缩进

yaml使用一个固定的缩进风格表示数据层结构关系,需要每个缩进级别由两个空格组成。切记一定不能使用tab键进行缩进。

规则二:冒号

每个冒号后面一定要有一个空格(以冒号结尾不需要空格,表示文件路径的模版可以不需要空格)

规则三:短横线

想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分

- name: This command will change the working directory to somedir/ and will only run when /path/to/database doesn't exist.

  command: /usr/bin/make_database.sh arg1 arg2

  args:

    chdir: somedir/

    creates: /path/to/database

[root@m01 ~]# cat /etc/ansible/a.yml

- hosts: oldboy

  tasks:

    - shell: echo hello oldboy linux. >/tmp/a.log

ansible oldboy -m command -a "echo hello oldboy linux."

=========写成剧本

- hosts: oldboy

  task:

    - command: echo hello oldboy linux.

=========写成剧本

ansible oldboy  -m command -a "pwd chdir=/etc"

- hosts: oldboy

  task:

    - command: echo hello oldboy linux.

用ansible完成一键部署rsync服务端。

你可能感兴趣的:(39day ansible模块)