ansible进阶

ansible-playbook:

ansible常用模块:

 command模块:ansible all -a ‘ls -l'

shell模块:ansible all -m shell -a 'xxx'

copy上传模块:ansible all -m copy -a 'src=/root/1.txt dest=/root/1.txt'

===================下载和解压=============================
-name: wget url
 get_url:  url=http://xxxx dest=/mnt   下载包及存放地址
=========================================================
-name: unzip
 unarchive:   解压
  src:       解压包地址
  dest:      解压后目录
  copy: no   本地解压no代表不是拷贝

file文件管理模块:

给与权限:ansible all -m file  -a 'src=/root/foo.txt state=touch mode=0755‘

创建文件:ansible all -m file  -a 'src=/root/1.txt state=touh'
absent删除文件:ansible all -m file -a 'src=/root/1.txt state=absent'
link设置软连接:ansible all -m file -a 'src=/root/1.sh dset=/root/2.sh state=link'
hostname模块:ansible name1 -m hostname -a 'name=dd'
corn模块:
增加定时:ansible -m corn  all -a  'minte=30/*  weekdat=*  job=/root/2.sh'
删除定时:ansible all -m corn  -a 'disabled=no  job=/root/2.sh'
启用定时:ansible all -m corn -a 'disabled=yes  job=/root/2.sh'
删除定时:ansible all -m corn -a 'job=/root/2.sh  state=absent'
yum管理包
安装:ansible all -m yum -a 'name=wget'
删除:ansible all -m yum -a 'name=wget state=absent'
service管理服务:
启动服务:ansible -m service -a 'name=http state=restarted'
停止服务:ansible -m service -a 'name=http state=shtoped'
开启自启:ansible -m service -a 'name=http state=restarted enabled=yes'
user管理用户:ansible -m user -a 'name=mm '
group管理用户组:ansible -m user -a 'name=mm group=ddos '


 

playbook:

基本结构:

Tasks:任务,由模板定义的操作列表
Variables:变量{命令行优先于配置文件变量,配置文件单个变量优先于公共变量}
Templates:模板,即使用模板语法的文件
Handlers:处理器 ,当某条件满足时,触发执行的操作
Roles:角色

handlers:触发器

notify :触发条件

示例:

[root@localhost ansible]# cat httpd.yml 

---
- hosts: test
  remote_user: root
  tasks:
    - name: install httpd package
      yum: name=httpd
    - name: copy conffile
      copy: src=/root/1.conf dest=/etc/httpd/conf backup=yes
      notify: restart service
    - name: start service
      service: name=httpd state=started enabled=yes

  handlers:
    - name: restart service
      service: name=httpd state=restarted

ansible优化:ansible是基于ssh对远程主机进行管控,只需要优化ssh服务即可

A.修改/etc/ssh/sshd.config文件中的配置

UseDNS no

# GSSAPI options
GSSAPIAuthentication no
然后,执行/etc/init.d/sshd restart重启sshd进程使上述配置生效,在连接一般就不慢了

B.检查ssh服务端上/etc/hosts文件中,127.0.0.1对应的主机名是否和
uname -n的结果一样,或者把本机ip和hostname(uname -n结果)加入到/etc/hosts里

C.利用ssh-v的调试功能查找慢的原因

[root@localhost ansible]# ssh -v root@192.168.0.110
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.0.110 [192.168.0.110] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.0.110:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:ITI/LuGfNPC5CfDsdbghgHboLLQVrLsm7lk2EgwdH5A
debug1: Host '192.168.0.110' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)

debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)

debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: password

 


版权声明:本文为qq_45444610原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。