Essays on ansible

If you have nothing to do, write a little knowledge about ansible.
1. Ansible's Ad-Hoc
What is Ad-Hoc? It is very simple as a temporary command, which can be executed directly relative to the playbook. A simple example.
ansible -i /tmp/host_test.txt -m command -a "hostname" -f 2
ansible -i /tmp/test.txt -f5 -m ping
This is the simplest one that can be executed directly. As for -i,-m, -f, -v, -s, -u, etc. These parameter commands, just a few, and check the others yourself.
-i --inventory=PATH generally specifies the location of hosts, the default is /etc/ansible/hosts
-m specifies the module used
-f the number of concurrent threads
-v outputs more detailed, -vvv outputs the execution process
-s --sudo executes sudo The command
-u --user specifies the user to execute
this should be very clear -------- (too lazy to write)
2. Ansible's playbook
first made it clear that the playbook is written in yaml syntax, and the data structure is expressed by indentation, continuous The project is represented by "-", the key/value is separated by ":", the beginning of the file "---", must contain task,
the simplest shell name can be converted into a playbook, for example:
#! /bin/bash
yum install -y httpd httpd-devel
cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
service httpd restart
chkconfig httpd on
####################################


  • hosts: all
    sudo: yes
    tasks:
    • name: "安装apacher"
      yum: name={{ item }} state=present
      with_items:
  • httpd
  • httpd-devel
    • name: "Copy configuration file"
      copy:
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
      owner: root
      group: root
      mode: 0644
      with_items:
  • {
    src: "/tmp/httpd.conf"
    dest: "/etc/httpd/conf/httpd.conf"}
    -{
    src: "/tmp/httpd-devel.conf"
    dest: "/etc/httpd/conf/httpd-devle.conf"}
    }
    -name: "检查开机自启"
    service: name=httpd state=present enabled=yes

############################################
You can see that it has been converted For the standard yaml ansible-playbook, we use several modules.

Guess you like

Origin blog.51cto.com/10226634/2543105