ansible常用命令

  • ansible常用命令已关闭评论
  • 70 views
  • A+
所属分类:运维实战

首先/etc/ansible/hosts中添加如下内容:

[webservers]
172.21.201.70
172.21.201.71

详解命令

ansible-doc -s  xxx

比如,查看shell,则命令为ansible-doc -s shell

ping

ansible -i /etc/ansible/hosts webservers -m ping

返回结果:

172.21.201.71 | SUCCESS => {
 "changed": false, 
 "ping": "pong"
}
172.21.201.70 | SUCCESS => {
 "changed": false, 
 "ping": "pong"
}

command

不支持管道

ansible -i /etc/ansible/hosts webservers -m command -a 'hostname'

返回结果:

172.21.201.71 | SUCCESS | rc=0 >>
localhost.localdomain
172.21.201.70 | SUCCESS | rc=0 >>
localhost.localdomain

shell

支持管道

ansible -i /etc/ansible/hosts webservers -m shell -a 'cat /etc/passwd |wc -l'

返回结果:

172.21.201.71 | SUCCESS | rc=0 >>
19
172.21.201.70 | SUCCESS | rc=0 >>
19

file

ansible -i /etc/ansible/hosts webservers -m file -a 'dest=/tmp/t.sh mode=755 owner=root group=root'

cron

添加cron

ansible -i /etc/ansible/hosts webservers -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'

minute表示分钟,hour表示小时,day表示日期,month表示月份,weekday表示周,如果没写则默认为 *

删除cron

ansible -i /etc/ansible/hosts webservers -m cron -a "name='test cron' state=absent"

只能删除该crontab,不能修改,也不能自己crontab -e去修改,不然ansible会操作不了该crontab

创建组以及用户

创建组

创建一个组名为a,gid为1900的组

ansible -i /etc/ansible/hosts webservers -m group -a 'gid=1900 name=a'

创建用户

用户名work

ansible -i /etc/ansible/hosts webservers -m user -a 'name=work'

删除用户

ansible -i /etc/ansible/hosts webservers -m user -a 'name=a groups=a remove=yes'

yum安装

ansible -i /etc/ansible/hosts webservers -m yum -a "state=present name=httpd"

启动服务

启动http并加到系统自启动

ansible -i /etc/ansible/hosts webservers -m service -a 'name=httpd state=started enabled=yes'

软连接

创建软连接

ansible -i /etc/ansible/hosts webservers -m file -a "src=/etc/resolv.conf dest=/tmp/resolv.conf state=link"

删除软连接

ansible -i /etc/ansible/hosts webservers -m file -a "path=/tmp/resolv.conf state=absent"

文件拷贝

ansible -i /etc/ansible/hosts webservers -m copy -a "src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=root group=root mode=0644"

文件下载

url下载到tmp

ansible -i /etc/ansible/hosts webservers -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'

远程命令

在webservers节点上执行hostname命令,支持管道

ansible -i /etc/ansible/hosts webservers -m raw-a 'hostname|tee'

目录同步

[root@localhost ~]# ansible-doc -s synchronize
- name: A wrapper around rsync to make common tasks in your playbooks quick and easy.
 synchronize:
 archive: # 归档,相当于同时开启recursive(递归), links, perms, times, owner,-D选项都为yes ,默认该项为开启.
 checksum: # Skip based on checksum, rather than mod-time & size; Note that that "archive"
 option is still enabled by default - the
 "checksum" 默认关闭.
 compress: # Compress file data during the transfer. In most cases, leave this enabled unless it causes problems.是否开启压缩
 copy_links: # Copy symlinks as the item that they point to (the referent) is copied, rather than the symlink.复制链接文件,默认为no
 delete: # Delete files in `dest' that don't exist (after transfer, not before) in the 删除不存在的文件,默认no
 `src' path. This option requires
 `recursive=yes'.
 dest: # (required) Path on the destination host that will be synchronized from the
 source; The path can be absolute or relative.
 dest_port: # Port number for ssh on the destination host. Prior to ansible 2.0, the
 ansible_ssh_port inventory var took precedence
 over this value.
 dirs: # Transfer directories without recursing
 existing_only: # Skip creating new files on receiver.
 group: # Preserve group
 link_dest: # add a destination to hard link against during the rsync.
 links: # Copy symlinks as symlinks.
 mode: # Specify the direction of the synchronization. In push mode the localhost or
 delegate is the source; In pull mode the remote
 host in context is the source.
push是通过本机向远程主机同步文件,pull是从远程获取文件到本地
 owner: # Preserve owner (super user only)
 partial: # Tells rsync to keep the partial file which should make a subsequent transfer of
 the rest of the file much faster.
 perms: # Preserve permissions.
 private_key: # Specify the private key to use for SSH-based rsync connections (e.g.
 `~/.ssh/id_rsa')
 recursive: # Recurse into directories.
 rsync_opts: # Specify additional rsync options by passing in an array.
 rsync_path: # Specify the rsync command to run on the remote host. See `--rsync-path' on the
 rsync man page.
 rsync_timeout: # Specify a --timeout for the rsync command in seconds.
 set_remote_user: # put user@ for the remote paths. If you have a custom ssh config to define the
 remote user for a host that does not match the
 inventory user, you should set this parameter to
 "no".
 src: # (required) Path on the source host that will be synchronized to the
 destination; The path can be absolute or
 relative.
 times: # Preserve modification times
 use_ssh_args: # Use the ssh_args specified in ansible.cfg
 verify_host: # Verify destination host key.

将控制端/root/123目录同步到webservers节点的tmp目录下

ansible -i /etc/ansible/hosts webservers -m synchronize -a 'mode=push src=/root/123 dest=/tmp'

sudo

/etc/ansible/host文件中添加如下:

[sudotest]
172.21.201.71:22 ansible_ssh_user=work ansible_ssh_pass='123456' ansible_sudo_pass='123456'

执行命令如下:

ansible -i /etc/ansible/hosts sudotest-m shell -a "mkdir -p /root/test" -u work --sudo

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar