This article was first published in My blog - Ansible Common use scenarios , Welcome to visit the original text .
to Ansible The next complete definition is difficult , The official propaganda is Ansible is Simple IT Automation
It seems that automation is still the main focus .
Ansible delivers simple IT automation that ends repetitive tasks and frees up DevOps teams for more strategic work.
There is also information Ansible Is a model driven configuration manager , Personally, I don't think this description is very appropriate . from Ansible In terms of the use of , By default push The way is more like Automation . and Puppet By default pull The way , It is more like configuration driver .
Ansible There are two modes of using , One is to call the module directly on the command line to execute a single command in batch , This is called AD HOC Pattern .
$ ansible [host_inventory] -m command -a 'hostname'
You can also write yaml file , To orchestrate a set of operations , This is called playbook Pattern . Look at a simple configuration .
# test.yaml
---
- hosts: 192.168.1.31
remote_user: root
tasks:
- name: run df -h
remote_user: test
shell: name=df -h
This is how it is used ,AD HOC Patterns cannot be persisted ,playbook The way can be through yaml File persistence and repeated execution .
$ ansible-playbook test.yaml
Today, let's get familiar with some of the most commonly used modules :
command modular
Execute command on remote node , Use as follows :
$ ansible [host_list] -m command -a "hostname"
But this way is not to call shell Environment to execute orders , So we can't get environment variables , You cannot use pipe symbols 、 Redirect etc. .
Support chdir
Parameters , That is, switch to the specified directory before executing the command .
$ ansible [host_list] -m command -a "ls -l chdir=/etc/yum.repos.d"
# command yes ansible Default module for , It can be omitted
$ ansible [host_list] -a "ls -l chdir=/etc/yum.repos.d"
shell modular
If you need to be in the remote shell Give orders , You need to use shell modular . Usage and command identical , But it is called when the command is executed /bin/sh
$ ansible [host_list] -m command -a 'echo $LOGNAME'
# This command can print the currently logged in user name
$ ansible [host_list] -m command -a 'ps -ef | grep java | wc -l'
# This command can count the java Number of programs
copy modular
Copy file , Support local replication to the server .
# Copy files from the server to the managed machine
$ ansible [host_list] -m copy -a 'src=/etc/hosts dest=/tmp owner=root mode=0755'
# Save the content as a remote file
$ ansible [host_list] -m copy -a 'content=Hello World! desc/tmp/test.txt owner=root force=yes mode=0755'
fetch modular
Get file from remote , If dest
When specified as a folder , The default is based on IP Create a folder for the name , Relevant files are saved in the corresponding folder .
# Get the file from the remote server
$ ansible [host_list] -m fetch -a 'src=/etc/hosts dest=/home/path owner=root mode=0755'
parameter list :
dest
: The directory where the target is stored . If you get/etc/hosts
The target is stored in/home
Next , The final save path is/home/host.example.com/etc/hosts
, The host name is based on/etc/ansible/hosts
Configuration of .src
: At present, it can only be files .
Common user execution Ansible Problems encountered
Scenario as follows , Manager of root The account number was collected , I can only use ordinary users shiqiang
perform ansible command . here , If the host list is configured as follows :
$ cat /etc/ansible/hosts
[testhost]
128.128.128.128
$ ansible testhost -m ping
Will be submitted to the Permission Denied
Error of . The reason is to use shiqiang
This account executes ansible On command , By default, the current user will try to log in to the target host without secret . because testhost The host and the management machine have done root Mutual trust of account ,, In this case, the use of root user , At the same time, the management machine shiqiang
User id_rsa.pub copy to testhost Of .ssh/authorized_keys
In file .
$ cat /etc/ansible/hosts
[testhost]
128.128.128.128 ansible_ssh_user=root