Fork me on GitHub

delegate_to 任务委托

在特定的主机上运行,而非一开始指定的所有主机

此时就需要用到Ansible的任务委托功能 delegate_to 关键字便可以配置任务在指定的机器上执行

需要明确,delegate_to只能对单个主机进行操作,批量操作需要通过with_item,loop循环

对于单独task,playbook,role,都可通过 delegate_to 委托指定主机运行任务

---
  - hosts: ubuntu
    vars:
      ignore: 'yes'
    tasks:
      - name: Run a command by delegate_to
        command: hostname
        delegate_to: localhost

      - name: Run a command by groups
        command: hostname
        delegate_to: "{{ groups['ubuntu'] | first }}"
ansible-playbook playbook2.yaml 

PLAY [ubuntu] *****

TASK [Gathering Facts] *****
ok: [192.168.255.110]

TASK [Run a command by delegate_to] *****
changed: [192.168.255.110 -> localhost]

TASK [Run a command by groups] *****
changed: [192.168.255.110 -> 192.168.255.110]

PLAY RECAP *****
192.168.255.110            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


local_action 本地执行

在 Ansible 中,local_action 是一个特殊的任务操作符,用于在控制节点(即运行 Ansible 的机器)上执行任务,而不是在远程主机上执行

- hosts: all
  tasks:
    - name: Create a file on the control node
      local_action:
        module: file
        path: /tmp/local_file.txt
        state: touch
ansible-playbook playbook.yaml

PLAY [all] *****

TASK [Gathering Facts] *****
ok: [192.168.255.110]

TASK [Create a file on the control node] *****
changed: [192.168.255.110 -> localhost]

PLAY RECAP *****
192.168.255.110            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

任务暂停

wait_for模块等待远程主机上的端口变为可用、等待文件出现或消失、等待某个服务启动等场景

playbook3.yaml

---
 - hosts: ubuntu
   tasks:
    - name: Wait for webserver to start.
      wait_for:
        host: localhost
        port: 11211
        delay: 10
        timeout: 300
        state: started
参数 注解
active_connection_states list elements=string 被视为活动连接的TCP连接状态的列表 默认值: ["ESTABLISHED""FIN_WAIT1","FIN_WAIT2","SYN_RECV","SYN_SENT","TIME_WAIT"]
connect_timeout integer 关闭并重试之前等待连接发生的最大秒数. Default: 5
delay integer 开始轮询之前等待的秒数. Default: 0
exclude_hosts list / elements=string 查找排空状态的活动TCP连接时要忽略的主机或ip的列表.
host string 要等待的可解析主机名或IP地址. Default: "127.0.0.1"
msg string 覆盖因无法满足所需条件而导致的正常错误消息.
path path 文件系统上必须存在才能继续的文件的路径. path and port are mutually exclusive parameters.
port integer 要轮询的端口号. path and port are mutually exclusive parameters.
search_regex string 可用于匹配文件或套接字连接中的字符串. Defaults to a multiline regex.
sleep integer 两次检查之间休眠的秒数. 在Ansible 2.3之前,这被硬编码为1秒. Default: 1
state string Either present , started , or stopped , absent , or drained .
When checking a port started will ensure the port is open,
stopped will check that it is closed,
drained will check for active connections.
When checking for a file or a search string present or started will ensure that the file or string is present before continuing,
absent will check that file is absent or removed.
Choices: - "absent" - "drained" - "present" - "started" ← (default) - "stopped"
timeout integer 等待的最大秒数,当与另一个条件一起使用时,它将强制错误. 当在没有其他条件的情况下使用时,它相当于睡觉. Default: 300

var_prompt交互模块

·private:该值为yes,即用户所有的输入在命令中默认都是不可见的;而将其值设为no时,用户输入可见.

·default:为变量设置默认值,以节省用户输入时间.

·confirm:特别适合输入密码的情况,如果将值设为yes,则会要求用户输入两次,以增加输入的正确性.

输入提示 这种交互式的操作方法可以很方便地让用户输入特定的信息,但是在大部分情况下我们应尽量避免使用这一功能,除非是在非常必要时,因为交互操作虽然满足了用户的个性化需求,但是大大降低了Ansible自动化运维的能力.

var_prompt官方页面

playbook4.yaml

---
 - hosts: ubuntu
   vars_prompt:
    - name: "username"
      prompt: "Please enter your username"
      private: no

    - name: "password"
      prompt: "Please enter your password"
      private: yes
      encrypt: 
   tasks:
    - name: print vars
      debug:
        msg: 
         - "{{ username }}"
         - "{{ password }}"

ansible-playbook playbook4.yaml
Please enter your username: jeon
Please enter your password: ****

PLAY [ubuntu] *****

TASK [Gathering Facts] *****
ok: [192.168.255.110]

TASK [print vars] *****
ok: [192.168.255.110] => {
    "msg": [
        "jeon",
        "abcd"
    ]
}

PLAY RECAP *****
192.168.255.110            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

posted on 2024-08-19 17:08  anyux  阅读(223)  评论(0)    收藏  举报