Ansible常用模块,


ansible笔记


hosts:远程主机

user:执行该任务组的用户

remote_user:与user相同

sudo:如果设置为yes,执行该任务组的用户在执行任务的时候,获取root权限。

sudo_user:如果设置user为tom,sudo为yes,sudo_user为jerry,则tom获取jerry用户的权限。

connection:通过什么方式连接到远程主机。

###gather:收集  facts:事实

gather_facts:除非你明确说明不需要在远程主机上执行setup模块,否则默认会自动执行。如果你确实不需要setup模块所传过来的变量,你可以启用该选项。


file 模块

1 2 3 4 ansible test -m file -a "src=/etc/fstab dest=/tmp/fstab state=link" ansible test -m file -a 'path=/tmp/fstab state=absent' ansible test -m file -a 'path=/tmp/file1 state=touch' ansible test -m file -a 'path=/tmp/dir1 state=directory mode=755 owner=root group=root'


-V 或-vvv参数

ansible执行自动化脚本获取执行过程的详细信息。

1 2 3 4 5 ansible test -m  ping -v ansible test -m  ping -vvv # ansible 192.168.121.128:192.168.121.129 -m  ping -o 192.168.121.129 | SUCCESS => {"changed"false"ping""pong"} 192.168.121.128 | SUCCESS => {"changed"false"ping""pong"}


copy模块:

1 2 3 4 ansible test -m copy -a 'src=jdk.yml dest=/tmp'   #content=:内容填充到文件中。 ansible test -m copy -a 'content="hello\n world\n" dest=/tmp/test.ansible'


group模块:

创建新用户的组:

1 ansible all -m group -a "name=hh system=yes"

添加新用户:

1 ansible all -m user -a "name=hh group=hh system=yes"


ansible批量添加用户,前提是你要有这个组,否则会报错的:

1 2 3 4 5 echo ansible | openssl passwd -1 -stdin//ansible user的password参数需要接受加密后的值。 ansible test -m user -a 'name=meng password="$1$nZ1s3fvA$gzQzd8FZuYRIldm0HMqNm/"' -f 5 -o      #system=yes 系统用户,默认使用/bin/bash ansible test -m user -a 'name=meng group=meng system=yes'


command:

creates:指定一个文件名,当该文件存在,下面的命令将不执行。

removes:指定一个文件名,当该文件不存在,则后续命令不执行。

1 2 3 4 5 6 7 8 9 10 11 12 [root@192 ansible]# ansible test  -a 'creates=/tmp/22 ls /home' 192.168.121.129 | SUCCESS | rc=0 >> skipped, since /tmp/22 exists 192.168.121.128 | SUCCESS | rc=0 >> skipped, since /tmp/22 exists [root@192 ansible]# ansible test  -a 'creates=/tmp/file4 ls /home' 192.168.121.129 | SUCCESS | rc=0 >> meng ody 192.168.121.128 | SUCCESS | rc=0 >> meng ody



command模块不支持管道过滤。

1 ansible test -a 'netstat -tnlp|grep ssh'


shell支持管道

1 ansible test -m shell -a 'netstat -tnlp|grep ssh'


类似于shell命令的raw模块也支持管道。

1 ansible test -m raw -a 'netstat -tnlp|grep ssh'


chdir:  执行命令之前先 切换到指定目录。

1 ansible test  -a 'chdir=/tmp tar czf file.tar.gz file'


service模块

enable=yes|no 开机自启动

name:服务名称

runlevel:运行级别

state:当前服务的运行状态(started,stopped,restarted,reloaded)

sleep:对服务执行restarted操作,stop和start之间沉睡几秒钟。

pattern:定义一个匹配模式,如果status指令查看服务状态时没有响应,就会通过ps命令在进程中根据该模式查找,如果匹配到,则认为该服务依然在运行。


重启服务:

1 ansible test -m raw -a 'name=httpd state=restarted sleep=3'

停止服务:

1 ansible test -m raw -a 'name=httpd state=stopped'


cron:计划任务管理

backup:对远程主机上的原计划任务内容修改之前做备份。

cron_file:如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户计划

day:日(1-31,*,*/2,)

hour:小时(0-23,*,*/2)

minute:分钟(0-59,*,*/2)

mouth:月(1-12,*,*/2)

weekday:周(0-7,*,)

job:要执行的任务,依赖于state=present

name:该任务的描述

special_time:指定什么时候执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly

state:确认该任务计划是创建还是删除。

user:以哪个用户身份执行。


实例:

创建计划任务:

1 ansible test -m cron -a 'name="reboot system" hour=2 user=root job="/sbin/reboot"'


查看计划任务:

1 ansible test -m command -a 'crontab -l'


删除计划任务:

1 ansible test -m cron -a 'name="reboot system" hour=2 user=root job="/sbin/reboot" state=absent'



filesystem:在块设备上创建文件系统

选项:

dev:目标块设备

force:在一个已有文件系统的设备上强制创建

fstype:文件系统的类型

opts:传递给mkfs命令的选项


yum软件包管理:

config_file:yum的配置文件

disable_gpg_check:关闭gpg_check

disablerepo:不启用某个源

enablerepo:启用某个源

list

name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm的路径。

state:状态(present,absent,latest)

实例:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 - name: 安装最新版本的apache   yum: name=httpd state=latest - name: 移除apache   yum: name=httpd state=absent - name: 安装一个特殊版本的apache   yum: name=httpd-2.2.29-1.4.amzn1 state=present - name: 升级所有的软件包   yum: name=* state=latest - name: 从一个远程yum仓库安装nginx   yum: name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present - name: 从本地仓库安装nginx   yum: name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present - name: 安装整个Development tools相关的软件包   yum: name="@Development tools" state=present




dd if:

1 2 ansible test -m command -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024' ansible -m commmand -a 'losetup /dev/loop0 /disk.img'


ansible-playbook的语法校验。

[root@192 ansible]# ansible-playbook jdk.yml --syntax-check


playbook: jdk.yml


显示指定playbook中的task。

[root@192 ansible]# ansible-playbook jdk.yml --list-task


playbook: jdk.yml


  play #1 (test): testTAGS: []

    tasks:

      jdk : jdk-8u101-linux-x64.tar.gzTAGS: []

      jdk : tar jdk-8u101-linux-x64.tar.gzTAGS: []

      jdk : java_profile_1TAGS: []

      jdk : java_profile_2TAGS: []

      jdk : java profile_3TAGS: []

      jdk : java_4TAGS: []


查看playbook针对的主机

1 2 3 4 5 6 7 [root@192 ansible]# ansible-playbook jdk.yml --list-hosts playbook: jdk.yml   play #1 (test): testTAGS: []     pattern: [u'test']     hosts (2):       192.168.121.129       192.168.121.128

指定task从第某行开始执行。

1 ansible-playbook jdk.yml --start-at-task='java_profile_1'



使用ansible做秘钥登录:

推送key的前提是配置文件编辑好密码。然后使用下面推送:

1 10.10.2.9  ansible_ssh_pass='Z6tqEE'


authorized_key 模块,root用户下推送:

1 ansible 10.74.246.69 -m authorized_key -a "user=root key={{lookup('file','/root/.ssh/id_rsa.pub') }}"


推送普通用户的key也是一样,使用root用户,不能切换为hh用户,否则报错,推送失败。

1 ansible 10.74.246.153 -m authorized_key -a "user=hh key={{lookup('file','/home/hh/.ssh/id_rsa.pub') }}"


--sudo 使用:

1 ansible all --sudo -m copy -a 'src=/tmp/tomcat_port.sh dest=/usr/local/zabbix/share/zabbix/alertscripts/ mode=0777'

本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1886305

相关内容