# Example from Ansible Playbooks
- copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644

# Copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
- copy: src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes

# Copy a new "sudoers" file into place, after passing validation with visudo
- copy: src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'

- file: path=/etc/foo.conf owner=foo group=foo mode=0644
- file: src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link
- file: src=/tmp/{{ item.path }} dest={{ item.dest }} state=link
    - { path: 'x', dest: 'y' }
    - { path: 'z', dest: 'k' }

# Add the user 'johnd' with a specific uid and a primary group of 'admin'
- user: name=johnd comment="John Doe" uid=1040

# Add the user 'james' with a bash shell, appending the group 'admins' and 'developers' to the user's groups
- user: name=james shell=/bin/bash groups=admins,developers append=yes

# Remove the user 'johnd'
- user: name=johnd state=absent remove=yes

# Create a 2048-bit SSH key for user jsmith
- user: name=jsmith generate_ssh_key=yes ssh_key_bits=2048

# Example from Ansible Playbooks
- script: /some/local/ --some-arguments 1234

# Run a script that creates a file, but only if the file is not yet created
- script: /some/local/ --some-arguments 1234 creates=/the/created/file.txt

# Run a script that removes a file, but only if the file is not yet removed
- script: /some/local/ --some-arguments 1234 removes=/the/removed/file.txt

- name: install the latest version of Apache
  yum: name=httpd state=latest

- name: remove the Apache package
  yum: name=httpd state=removed

- name: install the latest version of Apache from the testing repo
  yum: name=httpd enablerepo=testing state=installed

- name: upgrade all packages
  yum: name=* state=latest

- name: install the nginx rpm from a remote repo
  yum: name= state=present

- name: install nginx rpm from a local file
  yum: name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present

- name: install the 'Development tools' package group
  yum: name="@Development tools" state=present

# Example from Ansible Playbooks
- template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode=0644

# Copy a new "sudoers" file into place, after passing validation with visudo
- template: src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'

# Ensure a job that runs at 2 and 5 exists.
# Creates an entry like "* 5,2 * * ls -alh > /dev/null"
- cron: name="check dirs" hour="5,2" job="ls -alh > /dev/null"

# Ensure an old job is no longer present. Removes any job that is prefixed
# by "#Ansible: an old job" from the crontab
- cron: name="an old job" state=absent

# Creates an entry like "@reboot /some/"
- cron: name="a job for reboot" special_time=reboot job="/some/"

# Creates a cron file under /etc/cron.d
- cron: name="yum autoupdate" weekday="2" minute=0 hour=12
        user="root" job="YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate"

# Removes a cron file from under /etc/cron.d
- cron: cron_file=ansible_yum-autoupdate state=absent

# Example action to start service httpd, if not running
- service: name=httpd state=started

# Example action to stop service httpd, if running
- service: name=httpd state=stopped

# Example action to restart service httpd, in all cases
- service: name=httpd state=restarted

# Example action to reload service httpd, in all cases
- service: name=httpd state=reloaded

# Example action to enable service httpd, and not touch the running state
- service: name=httpd enabled=yes

# Example action to start service foo, based on running process /usr/bin/foo
- service: name=foo pattern=/usr/bin/foo state=started

# Example action to restart network service for interface eth0
- service: name=network state=restarted args=eth0

[root@monitor ansible]#  ansible-doc -l
acl                  Sets and retrieves file ACL information.                    
add_host             add a host (and alternatively a group) to the ansible-playbo
airbrake_deployment  Notify airbrake about app deployments                       
alternatives         Manages alternative programs for common commands            
apache2_module       enables/disables a module of the Apache2 webserver          
apt                  Manages apt-packages                                        
apt_key              Add or remove an apt key                                    
apt_repository       Add and remove APT repositores                              
apt_rpm              apt_rpm package manager                                     
arista_interface     Manage physical Ethernet interfaces                         
arista_l2interface   Manage layer 2 interfaces                                   
arista_lag           Manage port channel (lag) interfaces                        
arista_vlan          Manage VLAN resources                                       
assemble             Assembles a configuration file from fragments               
assert               Fail with custom message                                    
at                   Schedule the execution of a command or script file via the a
authorized_key       Adds or removes an SSH authorized key                       
azure                create or terminate a virtual machine in azure              
bigip_facts          Collect facts from F5 BIG-IP devices                        
bigip_monitor_http   Manages F5 BIG-IP LTM http monitors                         
bigip_monitor_tcp    Manages F5 BIG-IP LTM tcp monitors                          
bigip_node           Manages F5 BIG-IP LTM nodes                                 
bigip_pool           Manages F5 BIG-IP LTM pools                                 
bigip_pool_member    Manages F5 BIG-IP LTM pool members                          
boundary_meter       Manage boundary meters                                      
bzr                  Deploy software (or files) from bzr branches                
campfire             Send a message to Campfire                                  
capabilities         Manage Linux capabilities                                   
cloudformation       create a AWS CloudFormation stack                           
command              Executes a command on a remote node                         
composer             Dependency Manager for PHP                                  
copy                 Copies files to remote locations.                           
cpanm                Manages Perl library dependencies.                          
cron                 Manage cron.d and crontab entries.                          
datadog_event        Posts events to DataDog  service                            
debconf              Configure a .deb package                                    
debug                Print statements during execution                           
digital_ocean        Create/delete a droplet/SSH_key in DigitalOcean             
digital_ocean_domain Create/delete a DNS record in DigitalOcean                  
digital_ocean_sshkey Create/delete an SSH key in DigitalOcean                    
django_manage        Manages a Django application.                               
dnsimple             Interface with (a DNS hosting service).        
dnsmadeeasy          Interface with (a DNS hosting service).     
docker               manage docker containers                                    
docker_image         manage docker images                                        
easy_install         Installs Python libraries                                   
ec2                  create, terminate, start or stop an instance in ec2, return 
ec2_ami              create or destroy an image in ec2, return imageid           
ec2_ami_search       Retrieve AWS AMI for a given operating system.              
ec2_asg              Create or delete AWS Autoscaling Groups                     
ec2_eip              associate an EC2 elastic IP with an instance.               
ec2_elb              De-registers or registers instances from EC2 ELBs           
ec2_elb_lb           Creates or destroys Amazon ELB. - Returns information about 
ec2_facts            Gathers facts about remote hosts within ec2 (aws)           
ec2_group            maintain an ec2 VPC security group.                         
ec2_key              maintain an ec2 key pair.                                   
ec2_lc               Create or delete AWS Autoscaling Launch Configurations      
ec2_metric_alarm     Create/update or delete AWS Cloudwatch 'metric alarms'      
ec2_scaling_policy   Create or delete AWS scaling policies for Autoscaling groups
ec2_snapshot         creates a snapshot from an existing volume                  
ec2_tag              create and remove tag(s) to ec2 resources.                  
ec2_vol              create and attach a volume, return volume id and device map.
ec2_vpc              configure AWS virtual private clouds                        
ejabberd_user        Manages users for ejabberd servers                          
elasticache          Manage cache clusters in Amazon Elasticache.                
facter               Runs the discovery program `facter' on the remote system... 
fail                 Fail with custom message                                    
fetch                Fetches a file from remote nodes                            
file                 Sets attributes of files                                    
filesystem           Makes file system on block device                           
fireball             Enable fireball mode on remote node                         
firewalld            Manage arbitrary ports/services with firewalld              
flowdock             Send a message to a flowdock                                
gc_storage           This module manages objects/buckets in Google Cloud Storage.
gce                  create or terminate GCE instances                           
gce_lb               create/destroy GCE load-balancer resources                  
gce_net              create/destroy GCE networks and firewall rules              
gce_pd               utilize GCE persistent disk resources                       
gem                  Manage Ruby gems                                            
get_url              Downloads files from HTTP, HTTPS, or FTP to node            
git                  Deploy software (or files) from git checkouts               
github_hooks         Manages github service hooks.                               
glance_image         Add/Delete images from glance                               
group                Add or remove groups                                        
group_by             Create Ansible groups based on facts                        
grove                Sends a notification to a channel                  
hg                   Manages Mercurial (hg) repositories.                        
hipchat              Send a message to hipchat                                   
homebrew             Package manager for Homebrew                                
homebrew_cask        Install/uninstall homebrew casks.                           
homebrew_tap         Tap a Homebrew repository.                                  
hostname             Manage hostname                                             
htpasswd             manage user files for basic authentication                  
include_vars         Load variables from files, dynamically within a task.       
ini_file             Tweak settings in INI files                                 
irc                  Send a message to an IRC channel                            
jabber               Send a message to jabber user or chat room                  
jboss                deploy applications to JBoss                                
jira                 create and modify issues in a JIRA instance                 
kernel_blacklist     Blacklist kernel modules                                    
keystone_user        Manage OpenStack Identity (keystone) users, tenants and role
layman               Manage Gentoo overlays                                      
librato_annotation   create an annotation in librato                             
lineinfile           Ensure a particular line is in a file, or replace an existin
linode               create / delete / stop / restart an instance in Linode Publi
lldp                 get details reported by lldp                                
locale_gen           Creates of removes locales.                                 
logentries           Module for tracking logs via                 
lvg                  Configure LVM volume groups                                 
lvol                 Configure LVM logical volumes                               
macports             Package manager for MacPorts                                
mail                 Send an email                                               
modprobe             Add or remove kernel modules                                
mongodb_user         Adds or removes a user from a MongoDB database.             
monit                Manage the state of a program monitored via Monit           
mount                Control active and configured mount points                  
mqtt                 Publish a message on an MQTT topic for the IoT              
mysql_db             Add or remove MySQL databases from a remote host.           
mysql_replication    Manage MySQL replication                                    
mysql_user           Adds or removes a user from a MySQL database.               
mysql_variables      Manage MySQL global variables                               
nagios               Perform common tasks in Nagios related to downtime and notif
netscaler            Manages Citrix NetScaler entities                           
newrelic_deployment  Notify newrelic about app deployments                       
nexmo                Send a SMS via nexmo                                        
nova_compute         Create/Delete VMs from OpenStack                            
nova_keypair         Add/Delete key pair from nova                               
npm                  Manage node.js packages with npm                            
ohai                 Returns inventory data from `Ohai'                          
open_iscsi           Manage iscsi targets with open-iscsi                        
openbsd_pkg          Manage packages on OpenBSD.                                 
openvswitch_bridge   Manage Open vSwitch bridges                                 
openvswitch_port     Manage Open vSwitch ports                                   
opkg                 Package manager for OpenWrt                                 
osx_say              Makes an OSX computer to speak.                             
ovirt                oVirt/RHEV platform management                              
pacman               Manage packages with `pacman'                               
pagerduty            Create PagerDuty maintenance windows                        
pause                Pause playbook execution                                    
ping                 Try to connect to host and return `pong' on success.        
pingdom              Pause/unpause Pingdom alerts                                
pip                  Manages Python library dependencies.                        
pkgin                Package manager for SmartOS                                 
pkgng                Package manager for FreeBSD >= 9.0                          
pkgutil              Manage CSW-Packages on Solaris                              
portage              Package manager for Gentoo                                  
portinstall          Installing packages from FreeBSD's ports system             
postgresql_db        Add or remove PostgreSQL databases from a remote host.      
postgresql_privs     Grant or revoke privileges on PostgreSQL database objects...
postgresql_user      Adds or removes a users (roles) from a PostgreSQL database..
quantum_floating_ip  Add/Remove floating IP from an instance                     
quantum_floating_ip_associate Associate or disassociate a particular floating IP with an i
quantum_network      Creates/Removes networks from OpenStack                     
quantum_router       Create or Remove router from openstack                      
quantum_router_gateway set/unset a gateway interface for the router with the specif
quantum_router_interface Attach/Dettach a subnet's interface to a router             
quantum_subnet       Add/Remove floating IP from an instance                     
rabbitmq_parameter   Adds or removes parameters to RabbitMQ                      
rabbitmq_plugin      Adds or removes plugins to RabbitMQ                         
rabbitmq_policy      Manage the state of policies in RabbitMQ.                   
rabbitmq_user        Adds or removes users to RabbitMQ                           
rabbitmq_vhost       Manage the state of a virtual host in RabbitMQ              
raw                  Executes a low-down and dirty SSH command                   
rax                  create / delete an instance in Rackspace Public Cloud       
rax_cbs              Manipulate Rackspace Cloud Block Storage Volumes            
rax_cbs_attachments  Manipulate Rackspace Cloud Block Storage Volume Attachments.
rax_clb              create / delete a load balancer in Rackspace Public Cloud...
rax_clb_nodes        add, modify and remove nodes from a Rackspace Cloud Load Bal
rax_dns              Manage domains on Rackspace Cloud DNS                       
rax_dns_record       Manage DNS records on Rackspace Cloud DNS                   
rax_facts            Gather facts for Rackspace Cloud Servers                    
rax_files            Manipulate Rackspace Cloud Files Containers                 
rax_files_objects    Upload, download, and delete objects in Rackspace Cloud File
rax_identity         Load Rackspace Cloud Identity                               
rax_keypair          Create a keypair for use with Rackspace Cloud Servers       
rax_meta             Manipulate metadata for Rackspace Cloud Servers             
rax_network          create / delete an isolated network in Rackspace Public Clou
rax_queue            create / delete a queue in Rackspace Public Cloud           
rax_scaling_group    Manipulate Rackspace Cloud Autoscale Groups                 
rax_scaling_policy   Manipulate Rackspace Cloud Autoscale Scaling Policy         
rds                  create, delete, or modify an Amazon rds instance            
rds_param_group      manage RDS parameter groups                                 
rds_subnet_group     manage RDS database subnet groups                           
redhat_subscription  Manage Red Hat Network registration and subscriptions using 
redis                Various redis commands, slave and flush                     
replace              Replace all instances of a particular string in a file using
rhn_channel          Adds or removes Red Hat software channels                   
rhn_register         Manage Red Hat Network registration using the `rhnreg_ks' co
riak                 This module handles some common Riak operations             
rollbar_deployment   Notify Rollbar about app deployments                        
route53              add or delete entries in Amazons Route53 DNS service        
rpm_key              Adds or removes a gpg key from the rpm db                   
s3                   idempotent S3 module putting a file into S3.                
script               Runs a local script on a remote node after transferring it..
seboolean            Toggles SELinux booleans.                                   
selinux              Change policy and state of SELinux                          
service              Manage services.                                            
set_fact             Set host facts from a task                                  
setup                Gathers facts about remote hosts                            
shell                Execute commands in nodes.                                  
slack                Send Slack notifications                                    
slurp                Slurps a file from remote nodes                             
sns                  Send Amazon Simple Notification Service (SNS) messages      
stackdriver          Send code deploy and annotation events to stackdriver       
stat                 retrieve file or file system status                         
subversion           Deploys a subversion repository.                            
supervisorctl        Manage the state of a program or group of programs running v
svr4pkg              Manage Solaris SVR4 packages                                
swdepot              Manage packages with swdepot package manager (HP-UX)        
synchronize          Uses rsync to make synchronizing file paths in your playbook
sysctl               Manage entries in sysctl.conf.                              
template             Templates a file out to a remote server.                    
twilio               Sends a text message to a mobile phone through Twilio.      
typetalk             Send a message to typetalk                                  
ufw                  Manage firewall with UFW                                    
unarchive            Copies an archive to a remote location and unpack it        
uri                  Interacts with webservices                                  
urpmi                Urpmi manager                                               
user                 Manage user accounts                                        
virt                 Manages virtual machines supported by libvirt               
vsphere_guest        Create/delete/manage a guest VM through VMware vSphere.     
wait_for             Waits for a condition before continuing.                    
win_feature          Installs and uninstalls Windows Features                    
win_get_url          Fetches a file from a given URL                             
win_group            Add and remove local groups                                 
win_msi              Installs and uninstalls Windows MSI files                   
win_ping             A windows version of the classic ping module.               
win_service          Manages Windows services                                    
win_stat             returns information about a Windows file                    
win_user             Manages local Windows user accounts                         
xattr                set/retrieve extended attributes                            
yum                  Manages packages with the `yum' package manager             
zfs                  Manage zfs                                                  
zypper               Manage packages on SuSE and openSuSE                        
zypper_repository    Add and remove Zypper repositories                          
[root@monitor ansible]# 


[root@monitor ansible]# ansible localhost -a 'uptime'
localhost | success | rc=0 >>
 17:43:28 up 864 days, 20:37,  4 users,  load average: 1.00, 1.42, 1.53


[root@monitor ansible]# ansible localhost -m copy -a "src=./cmd dest=./bid/ mode=755 owner=root group=root"
localhost | success >> {
    "changed": true, 
    "dest": "./bid/cmd", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "5e31eb3c0821400aad8a2c8d9d8ec30e", 
    "mode": "0755", 
    "owner": "root", 
    "size": 1554, 
    "src": "/root/.ansible/tmp/ansible-tmp-1409219115.51-104821256190330/source", 
    "state": "file", 
    "uid": 0

[root@monitor ansible]# ansible localhost -m command -a "ls ./bid/"                            localhost | success | rc=0 >>

[root@monitor ansible]# ansible localhost -m command -a "ls -l ./bid/"
localhost | success | rc=0 >>
total 4
-rwxr-xr-x 1 root root 1554 Aug 28 17:45 cmd


[root@monitor ansible]# ansible localhost -m file -a "dest=./bid/cmd mode=666 owner=nabody group=nobody"
localhost | FAILED >> {
    "failed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "msg": "chown failed: failed to look up user nabody", 
    "owner": "root", 
    "path": "./bid/cmd", 
    "size": 1554, 
    "state": "file", 
    "uid": 0

[root@monitor ansible]# ansible localhost -m file -a "dest=./bid/cmd mode=666 owner=nobody group=nobody"
localhost | success >> {
    "changed": true, 
    "gid": 99, 
    "group": "nobody", 
    "mode": "0666", 
    "owner": "nobody", 
    "path": "./bid/cmd", 
    "size": 1554, 
    "state": "file", 
    "uid": 99

[root@monitor ansible]# ansible localhost -m command -a "ls -l ./bid/"                         localhost | success | rc=0 >>
total 4
-rw-rw-rw- 1 nobody nobody 1554 Aug 28 17:45 cmd

[root@monitor ansible]# ansible localhost -m group -a "name=mlgbd state=present"
localhost | success >> {
    "changed": true, 
    "gid": 509, 
    "name": "mlgbd", 
    "state": "present", 
    "system": false

[root@monitor ansible]# ansible localhost -m user -a "name=mlgbd2 groups=mlgbd state=present"  
localhost | success >> {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 510, 
    "groups": "mlgbd", 
    "home": "/home/mlgbd2", 
    "name": "mlgbd2", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 509

[root@monitor ansible]# ansible localhost -m user -a "name=mlgbd2 state=absent remove=yes"   
localhost | success >> {
    "changed": true, 
    "force": false, 
    "name": "mlgbd2", 
    "remove": true, 
    "state": "absent"

[root@monitor ansible]# ansible localhost -m group -a "name=mlgbd state=absent system=yes"
localhost | success >> {
    "changed": true, 
    "name": "mlgbd", 
    "state": "absent"


[root@monitor ansible]# ansible localhost -m script -a "./"
localhost | success >> {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "root\n"

[root@monitor ansible]# ansible localhost  -a "/bin/sh /etc/ansible/"
localhost | success | rc=0 >>

[root@monitor ansible]# ansible localhost -m shell -a "/etc/ansible/"        
localhost | success | rc=0 >>


[root@monitor ansible]# ansible localhost -m service -a "name=salt-master state=started"
localhost | success >> {
    "changed": true, 
    "name": "salt-master", 
    "state": "started"

[root@monitor ansible]# ansible localhost -m service -a "name=salt-master state=stopped"
localhost | success >> {
    "changed": true, 
    "name": "salt-master", 
    "state": "stopped"

[root@monitor ansible]# 


[root@monitor ansible]# ansible cront -a 'crontab -l'                    | success | rc=0 >>
#0 0 1 * * wget -N -P /usr/share/spamassassin; /etc/init.d/amavisd restart 
#Ansible: check io
* * * * * vmstat 1

[root@monitor ansible]# ansible cront -m cron -a 'name="check io" hour="1,4,7" job="vmstat 1"' | success >> {
    "changed": true, 
    "jobs": [
        "check io"

[root@monitor ansible]# ansible cront -a 'crontab -l' | success | rc=0 >>
#0 0 1 * * wget -N -P /usr/share/spamassassin; /etc/init.d/amavisd restart 
#Ansible: check io
* 1,4,7 * * * vmstat 1

[root@monitor ansible]# ansible cront -m cron -a 'name="check io" state=absent'       | success >> {
    "changed": true, 
    "jobs": []

[root@monitor ansible]# ansible cront -a 'crontab -l'                  | success | rc=0 >>
#0 0 1 * * wget -N -P /usr/share/spamassassin; /etc/init.d/amavisd restart 

- raw: yum -y install python-simplejson

[root@monitor ~]# ansible cront -m raw -a "ps -ef | grep mail" | success | rc=0 >>
root      5686  5684  0 Aug28 ?        00:00:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t
root     21882 21878  0 14:26 ?        00:00:00 bash -c ps -ef | grep mail
root     21915 21882  0 14:26 ?        00:00:00 grep mail


电子邮件地址不会被公开。 必填项已用*标注