# In older versions of ansible use ``success``, now both are valid but succeeded uses the correct tense. -command:/bin/something_else when:resultissucceeded
-name:checkcontentsforemptiness debug: msg:"Directory is empty" when:contents.stdout==""
1 2 3 4 5 6 7 8
- name: test play hosts: mfs tasks: - shell: cat /etc/motd register: motd_contents
- shell: echo "motd contains the word hi" > /tmp/test2 when: motd_contents.stdout.find('hi') != -1
changed_when
1 2 3 4 5 6 7 8
tasks: - shell: /usr/bin/billybass --mode="take me to the river" register: bass_result changed_when: "bass_result.rc != 2" # this will never report 'changed' status - shell: wall 'beep' changed_when: False
failed_when
1 2 3 4
- name: this command prints FAILED when it fails command: /usr/bin/example-command -x -y -z register: command_result failed_when: "'FAILED' in command_result.stderr"
1 2 3 4 5 6 7 8
- name: this command prints FAILED when it fails command: /usr/bin/example-command -x -y -z register: command_result ignore_errors: True
- name: fail the play if the previous command did not succeed fail: msg="the command failed" when: "'FAILED' in command_result.stderr"
ansible test -m service -a "name=httpd state=started enabled=yes" asnible test -m service -a "name=foo pattern=/usr/bin/foo state=started" ansible test -m service -a "name=network state=restarted args=eth0"
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"' ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root" ansible test -m cron -a 'backup="True" name="test" minute="0" hour="5,2" job="ls -alh > /dev/null"' ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
yum模块
使用yum包管理器来管理软件包,其选项有:
config_file:yum的配置文件 。
disable_gpg_check:关闭gpg_check 。
disablerepo:不启用某个源 。
enablerepo:启用某个源。
name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径 。
state:状态(present(当前版本),absent(删除),latest)。
示例如下:
1 2 3
ansible test -m yum -a 'name=httpd state=latest' ansible test -m yum -a 'name="@Development tools" state=present' ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
ansible test -m synchronize -a 'src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync" ' ansible test -m synchronize -a 'src=some/relative/path dest=/some/absolute/path archive=no links=yes ' ansible test -m synchronize -a 'src=some/relative/path dest=/some/absolute/path checksum=yes times=no '
拉取 pull
1
ansible test -m synchronize -a 'src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull '
synchronize 结合-CD参数
1 2
.d..t...... ./ <f..tpog... jr-sta.conf
t: 表示修改了时间。
p: 表示修改了权限。
o: 表示修改了user。
g: 表示修改了group。
s: 表示修改了大小。
Explanation of each bit position and value in rsync’s output:
YXcstpoguax path/to/file ||||||||||| ||||||||||╰- x: The extended attribute information changed |||||||||╰-- a: The ACL information changed ||||||||╰--- u: The u slot is reserved for future use |||||||╰---- g: Group is different ||||||╰----- o: Owner is different |||||╰------ p: Permission are different ||||╰------- t: Modification time is different |||╰-------- s: Size is different ||╰--------- c: Different checksum (for regular files), or || changed value (for symlinks, devices, and special files) |╰---------- the file type: | f: for a file, | d: for a directory, | L: for a symlink, | D: for a device, | S: for a special file (e.g. named sockets and fifos) ╰----------- the type of update being done:: <: file is being transferred to the remote host (sent) >: file is being transferred to the local host (received) c: local change/creation for the item, such as: - the creation of a directory - the changing of a symlink, - etc. h: the item is a hard link to another item (requires --hard-links). .: the item is not being updated (though it might have attributes that are being modified) *: means that the rest of the itemized-output area contains a message (e.g. "deleting")
filesystem模块
在块设备上创建文件系统
dev:目标块设备.
force:在一个已有文件系统 的设备上强制创建.
fstype:文件系统的类型.
opts:传递给mkfs命令的选项.
示例:
1 2 3
ansible test -m filesystem -a 'fstype=ext2 dev=/dev/sdb1 force=yes'
ansible test -m filesystem -a 'fstype=ext4 dev=/dev/sdb1 opts="-cc"'
ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024' ansible test -a 'losetup /dev/loop0 /disk.img' ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0' ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
roles 查找路径 Description: Colon separated paths in which Ansible will search for Roles. Type: pathspec Default: ~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles Ini Section: defaults Ini Key: roles_path Environment: ANSIBLE_ROLES_PATH
ansible test -a 'uptime' 192.168.56.12| FAILED =>Using a SSH password instead of a key is not possible because HostKey checking is enabled and sshpass does not support this.Please add this host's fingerprint to your known_hosts file to manage this host.
[ssh_connection] # ssh arguments to use # Leaving off ControlPersist will result in poor performance, so use # paramiko on older platforms rather than removing it #ssh_args = -o ControlMaster=auto -o ControlPersist=60s 可以启用ssh_args 部分,使用下面的配置,避免上面出现的错误:
# {{ ansible_managed }} options { listen-on port 53 { 127.0.0.1; {% for ip in ansible_all_ipv4_addresses %} {{ ip }}; {% endfor %} }; listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; }; zone "." IN { type hint; file "named.ca"; }; include "/etc/named.rfc1912.zones"; include "/etc/named.root.key"; {# Variables for zone config #}
{% if 'authorativenames' in group_names %} {% set zone_type = 'master' %} {% set zone_dir = 'data' %}
{% else %} {% set zone_type = 'slave' %} {% set zone_dir = 'slaves' %} {% endif %}
zone "internal.example.com" IN { type {{ zone_type }}; file "{{ zone_dir }}/internal.example.com";
{% if 'authorativenames' not in group_names %} masters { 192.168.2.2; }; {% endif %} };
# Example setting host facts using key=value pairs - set_fact: one_fact="something" other_fact="{{ local_var * 2 }}" # Example setting host facts using complex arguments - set_fact: one_fact: something other_fact: "{{ local_var * 2 }}" # As of 1.8, Ansible will convert boolean strings ('true', 'false', 'yes', 'no') # to proper boolean values when using the key=value syntax, however it is still # recommended that booleans be set using the complex argument style: - set_fact: one_fact: true other_fact: false
# Example that prints the loopback address and gateway for each host - debug: msg="System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}"
- debug: msg="System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}" when: ansible_default_ipv4.gateway is defined
- name: Display all variables/facts known for a host debug: var=hostvars[inventory_hostname]
# Example playbook using fail and when together - fail: msg="The system may not be provisioned according to the CMDB status." when: cmdb_status != "to-be-staged"
/etc/ansible/group_vars/webserver # can optionally end in '.yml', '.yaml', or '.json' /etc/ansible/group_vars/webservers /etc/ansible/host_vars/foosball
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
cp /etc/selinux/config /etc/selinux/config.bak sed 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config.bak >/etc/selinux/config
优化SSH配置
1 2 3 4 5 6 7 8 9
sed -i 's/GSSAPIAuthentication yes/#GSSAPIAuthentication yes/g' /etc/ssh/ssh_config sed -i "s/Port 22/#Port 22/g" /etc/ssh/sshd_config cat >>/etc/ssh/sshd_config<<EOF Port 52113 PermitRootLogin no PermitEmptyPasswords no UseDNS no GSSAPIAuthentication no EOF
# ssh-keygen -t dsa ##创建密钥对使用dsa加密 Generating public/private dsa key pair. Enter file inwhich to save the key (/niu/.ssh/id_dsa): ##指定key存放位置,可以直接回车。 Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): ##输入密钥锁,可以直接回车。 Enter same passphrase again: Your identification has been saved in /niu/.ssh/id_dsa. Your public key has been saved in /niu/.ssh/id_dsa.pub. The key fingerprint is: 64:47:0d:9c:ab:8f:1e:7e:5a:b0:25:64:55:23:06:3a niu@centos6.7-mupan The key's randomart image is: +--[ DSA 1024]----+ | .o*=o | | . +o... | | E = .. | | * .. | | S.. | | .= | | oo. | | ..oo | | .+o | +-----------------+
global ##定义这是全局配置 log 127.0.0.1 local3 info ###全局的日志配置,使用log关键字,指定使用127.0.0.1上的syslog服务中的local0日志设备,记录日志等级为info的日志。 chroot /var/lib/haproxy ##使用chroot的模式运行haproxy user haproxy group haproxy ##设置运行haproxy的用户和组,可以使用uid gid 关键字代替。 daemon ##以守护进程的方式运行
global log 127.0.0.1 local3 info chroot /var/lib/haproxy user haproxy group haproxy daemon
defaults log global mode http option httplog option dontlognull timeout connect 5000 timeout client 50000 timeout server 50000
frontend http_front bind *:80 stats uri /haproxy?stats default_backend http_back
backend http_back balance roundrobin option httpchk GET /index.html option httpchk GET / server linux-node1 192.168.56.11:8080 check server linux-node2 192.168.56.12:8080 check
haproxy的ACL控制。
推荐生产上不要使用ACL访问控制。所有这里就没有研究。如果感兴趣的可以自行百度。
haproxy动态管理
方法一:
可以listen模式中开启stats admin if TRUE #开启页面管理 通过图形界面管理服务器上下线。