Examples of commonly used automation Ansible

安装ansible
[root@ansible ~]# yum install -y ansible

Change management which hosts the manifest file
[root @ ansible ansible] # vi / etc / ansible / hosts
[webserver]
192.168.1.21
192.168.1.22
192.168.1.23

Ansible to between host and managed to make a key to log
[root @ ansible ansible] # SSH-keygen
[root @ ansible ansible] # SSH-Copy-the above mentioned id 192.168.1.21

The management host are two ways (command / script)
commands:
Command Format
ansible <hosts> [options]

Examples 1: ping test patency through
[the root @ ansible ansible] -u # ansible the root of ping the webserver -m
... omitted

Example 2: Call the echo command in the directory
[root @ ansible ansible] # ansible All -a "/ bin / echo the Hello world"
... omitted

Examples 3: copy the file to a different directory
[root @ ansible ansible] # ansible webserver -m copy -a "src = / etc / passwd dest = / opt / passwd"

... omitted

Example 4: Install the software
[@ ansible the root ansible] # yum ansible the webserver -m -a "lrzsz name ="
... omitted

Example 5: Adding a user
[root @ ansible ansible] # ansible webserver -m user -a "name = zhangsan password = 123"

例子6: 启动系统的某个服务
[root@ansible ansible]# ansible webserver -m service -a "name=sshd state=started"
...省略
[root@ansible ansible]# ansible webserver -m service -a "name=httpd state=started"
...省略
[root@ansible ansible]# ansible webserver -m service -a 'name=httpd state=restarted'
...省略

Example 7: Restart a service
[root @ ansible ansible] # ansible Service webserver -a -m "name = httpd State RESTARTED ="
... omitted

Example 8: 3 specifies a command to execute the same machine (belonging to the parallel execution)
[the root @ ansible ansible] -a # ansible the webserver "echo Hello" -f 3
... omitted

Example 9: acquiring system information
[the root @ ansible ansible] -m # ansible the webserver Setup
... omitted

Screenplay:
Playbook composition

hosts: 目标主机
remote_user: 执行操作的用户身份
vars: 执行中的一些变量
tasks: 定义顺序执行的action,每个action调用一个模块
handers: event处理操作,仅有在action触发时才会执行,多次触发只执行一次并按照声明的顺序执行。

Example 10: install httpd service
[root @ ansible /] # vi test.yml

  • hosts: webserver
    remote_user: root
    tasks:

    • name: install httpd
      yum: pkg=httpd state=latest
  • hosts: webserver
    remote_user: root
    tasks:
    • name: start httpd
      service: name=httpd state=started

Example 11: get debug information
[root @ ansible /] # vi debug.yml

  • hosts: webserver
    remote_user: root
    tasks:
    • name: debug
      debug:
      msg: "{{ansible_default_ipv4.gateway}}"

Examples 12: shell module
[root @ ansible /] # vi shell.yml

  • hosts: webserver
    remote_user: root
    tasks:
    • name: guanbifanghuoqiang
      shell: systemctl stop firewalld

Example 13: copy module
[root @ ansible /] # vi copy.yml

  • hosts: all
    remote_user: root
    tasks:

    • name: Copy
      Copy: src = / etc / passwd = dest / Home
      Example 14: Create a user and then delete the user performs a dual task
      [root @ ansible /] # vi user.yml
  • hosts: all
    remote_user: root
    tasks:

    • name: create user
      user:
      name: apeng
      uid: 5000
      group: ftp
      shell: /bin/bash
      groups: apeng
      append: yes

    • name: delete user
      user:
      name: apeng
      state: absent
      remove: yes

Example 15: Installation httpd then uninstall
[root @ ansible /] # vi yum.yml

  • hosts: all
    remote_user: root
    tasks:
    • name: install httpd
      yum:
      name: httpd
      state: latest
    • name: remove httpd
      yum:
      name: httpd
      state: absent

Example 16: command module
[root @ ansible /] # vi command.yml

  • hosts: root
    remote_user: root
    tasks:
    • name: cmd
      command: ls

Guess you like

Origin blog.51cto.com/kangxi/2425989