使用 import_playbook 在运行时刷新 ansible 库存

问题描述 投票:0回答:0

我正在尝试创建一个服务器并在该主机上运行一个剧本。为此,我将 IP 地址写入主机文件,甚至尝试将其添加到 ansible 内存中。然后我导入一个剧本,其中 var 目标是主机值。但无论我尝试什么,我都无法让它发挥作用。有人可以透露一些信息吗?

我得到的错误是找不到主机

PLAY [localhost] ************************************************************************************************************************************************************************************************

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

TASK [get the roles directory] **********************************************************************************************************************************************************************************
changed: [localhost]

TASK [set the fact of role dir to output of last cmd] ***********************************************************************************************************************************************************
ok: [localhost]

TASK [ls terrafrom workspace] ***********************************************************************************************************************************************************************************
changed: [localhost]

TASK [Registering private IP as ansible variable.] **************************************************************************************************************************************************************
changed: [localhost]

TASK [Setting private IP as private_ip] *************************************************************************************************************************************************************************
ok: [localhost]

TASK [Now I will add 192.168.x.x to the ansible hosts file at /etc/ansible/hosts right under the line that [terrafrom] is on.] *******************************************************************************
changed: [localhost]

TASK [Now I will add the 192.168.x.x into ansibles memory] ***************************************************************************************************************************************************
changed: [localhost]

TASK [remove all files from ansible cache] **********************************************************************************************************************************************************************
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.  If you need to use command because file is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [localhost]

TASK [Refresh ansible facts] ********************************************************************************************************************************************
ok: [localhost]
[WARNING]: Could not match supplied host pattern, ignoring: private_ip

PLAY [config {{ private_ip }}] **********************************************************************************************************************************************************************************
skipping: no hosts matched

PLAY RECAP ******************************************************************************************************************************************************************************************************
localhost                  : ok=10   changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
---
# tasks file for mysql_linode_server
- hosts: localhost
  tasks:
    - name: "Run terrafrom workspace"
      shell:
        cmd: "terraform apply -auto-approve"
        chdir: "{{ role_dir }}/terraform/mysql_one_node/"
    - name: "Registering private IP as ansible variable."
      shell:
        cmd: "terraform output -raw ip"
        chdir: "{{ role_dir }}/terraform/mysql_one_node/"
      register: private_ip
    - name: "Setting private IP as private_ip"
      set_fact:
        private_ip: "{{ private_ip.stdout }}"
    - name: "Now I will add {{ private_ip }} to the ansible hosts file at /etc/ansible/hosts right under the line that [terrafrom] is on."
      lineinfile:
        path: /etc/ansible/hosts
        insertafter: "terraform"
        line: "{{ private_ip }}"
    - name: "Now I will add the {{ private_ip }} into ansibles inventory memory"
      ansible.builtin.add_host:
        name: "{{ private_ip }}"
        groups: terraform
    # I tried this to see if the inventory was cached.
    - name: remove all files from ansible cache
      shell:
        cmd: "rm -rfv ~/.ansible/tmp/* -rfv"
    # I refresh the inventory
    - name: Refresh inventory to ensure new instances exist in inventory
      ansible.builtin.meta: refresh_inventory
    # And even try to rebuild the facts.
    - name: Refresh ansible facts
      setup:

- name: "config {{ hostvars['localhost'].public_ip"
  import_playbook: "{{ playbook_dir }}/create_ansible_user.yml"
  vars:
    target: hostvars['localhost'].public_ip

如果您需要更多信息,我会在评论中出现

亲切的问候,

ansible ansible-2.x
© www.soinside.com 2019 - 2024. All rights reserved.