Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    jghal
    @jghal
    the command expects the opening/closing curly brace in the argument value
    jghal
    @jghal
    blech, seems like {%raw%}{{%endraw%}{{myvar}}{%raw%}}{%endraw%} works but that's really verbose. is there any shorter syntax
    jghal
    @jghal
    {{ myvar | regex_replace('(.*)', '{\1}') }} gets the job done, seems a bit easier to read to me.
    matrixbot
    @matrixbot
    dziendobry Hi. I would like to know if someone can help me a on ansible-vault. I have a roles directory here and one role has a vars/main.yml file with the var I need to run my script and there is another file containing sensitive info call vault.yml (encrypted by ansible-vault). I would like to know how to use this vault.yml file. I first try to add it to main.yml using (include_vars: vault.yml) but it is not working (or I'm missing something)
    dziendobry I mean...I'm missing something for sure /o\
    pierremig
    @PierreOoo

    Hi I just bumped on this problem on my project, when I gather_facts, vars interpretation doesn't work anymore. This happends in ansible 2.4 and 2.8
    I'd like to keep vars interpretation when activating fact gathering.

    when gather_facts: no

    TASK [debug] **********************************************************************************************************************************
    ok: [test1-server] => (item={u'path': u'/tmp/somedir/a', u'name': u'some_name'}) => {
        "msg": {
            "name": "some_name", 
            "path": "/tmp/somedir/a"
        }

    when gather_facts: yes

    TASK [debug] **********************************************************************************************************************************
    ok: [test1-server] => (item={u'path': u'{{ dir_a }}', u'name': u'some_name'}) => {
        "msg": {
            "name": "some_name", 
            "path": "{{ dir_a }}"
        }
    }

    Playbook:

    - hosts: all
      gather_facts: yes
      vars:
        type: "dirs"
        dir_a: /tmp/somedir/a
        dirs:
          - name: "some_name"
            path: "{{ dir_a }}"
      tasks:
        - name: debug
          debug:
            msg: "{{ item }}"
          with_items:
            - "{{ vars[type]}}"
    sorry for the multiple edits
    Jay Carlson
    @nopdotcom
    Is vars a legit way of writing hostvars[inventory_hostname]?
    also I would never name a variable type :-)
    but yes, I can reproduce that
    pierremig
    @PierreOoo

    Hi Jay, here those play vars doesn't end up in hostvars[inventory_hostname].
    and yes type is a bad example for a variable
    I am using vars[] to force the interpretation of a string into a variable, which is the problem I have in my actual project.
    vars[dvars] => vars['dirs'] => [{ name: "dir_a", path: "/tmp/somedir/a"}]

    To be more precise my question is: What is the approprate way to make ansible inteprete variable that referes to other variables, and/or values(string) that referes to variables ? Espacially in this kind of nested cases !

    In this example dir_a is never interpreted through vars[dvar] (gather_facts value doesn't change anything)

    - hosts: all
      gather_facts: yes
      vars:
        dvar: "dirs"
        dir_a: /tmp/somedir/a
        dirs:
          - name: "dir_a"
            path: "{{ dir_a }}"
      tasks:
        - name: debug dvar
          debug:
            msg: "{{ dvar }}"
    
        - name: debug dirs
          debug:
            msg: "{{ dirs }}"
    
        - name: debug vars[dvar]
          debug:
            msg: "{{ vars[dvar] }}"
    
        - name: debug hostvars[inventory_hostname][dvar]
          debug:
            msg: "{{ hostvars[inventory_hostname][dvar] }}"
    TASK [debug dvar] ****************************************************************************************************************************************************************************
    ok: [test1-server] => {
        "msg": "dirs"
    }
    
    TASK [debug dirs] ****************************************************************************************************************************************************************************
    ok: [test1-server] => {
        "msg": [
            {
                "name": "dir_a", 
                "path": "/tmp/somedir/a"
            }
        ]
    }
    
    TASK [debug vars[dvar]] **********************************************************************************************************************************************************************
    ok: [test1-server] => {
        "msg": [
            {
                "name": "dir_a", 
                "path": "{{ dir_a }}"
            }
        ]
    }
    TASK [debug hostvars[inventory_hostname][dvar]] **********************************************************************************************************************************************
    fatal: [test1-server]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute u'dirs'\
    ....
    line 24, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: debug hostvars[inventory_hostname][dvar]\n      ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute u'dirs'"}
    Jay Carlson
    @nopdotcom
    I wonder where vars is documented. omit was pretty well hidden.
    pierremig
    @PierreOoo

    Things seems to work far away better with lookup('vars')...
    available from 2.5
    https://docs.ansible.com/ansible/latest/plugins/lookup/vars.html

    Thanks anyway

       - name: debug lookup('vars', dvar )
          debug:
            msg: "{{ lookup('vars', dvar )}}"
    matrixbot
    @matrixbot
    Michael S. Moody Anyone have any good solutions for a synchronize-esque behavior that works on Windows? Would the synchronize module work if rsync was installed in path on Windows?
    matrixbot
    @matrixbot
    dziendobry Is there a way to wait a task to be finish before move to the next one?
    matrixbot
    @matrixbot
    dziendobry I'm using the vmware modules to create a new vm and I woud like to move to the next step only after creation is done. Same goes to power on the VM. I would like to move on to the next step only after the OS is booted. Any tips?
    matrixbot
    @matrixbot
    dziendobry I've got it :D. Thanks!
    ecaepp
    @ecaepp

    @PierreOoo sorry for the slow response it has been busy for the last few days.

    If I am understanding correctly you have an array dirs that contains the dict name, path and want to set the variable dvar to one of the vaules in dirs, or are trying to pass the values of dirs to a module?

    Jay Carlson
    @nopdotcom
    I got tired of making role directories meta/main.yml, vars/main.yml, etc, so I wrote a tiny PR so you can write meta.yml and vars.yml. Here's nopdotcom/ansible#1. I ran some stats on all of Galaxy and of course there are huge numbers of redundant directories...
    Chris Francy
    @zoredache
    Isn't it valid to make a vars/main/foo.yml, and vars/main/blah.yml, and it will load them all
    Oh, and if you don't like making all the dirs, there is always the ansible-galaxy init command to build out the structure for you.
    Jay Carlson
    @nopdotcom
    @zoredache "Isn't it valid to make a vars/main/foo.yml"....it doesn't look like it from the documentation. Roles and Variable precedence don't seem to say so. But they also don't say that you can use .yaml and .json.
    And yeah, you can always add tooling when your environment requires boilerplate, but that's a code smell.
    Chris Francy
    @zoredache
    Pretty sure it allows vars/main to be a dir. I know it isn't documented well. I think the code is the allow_dir=true https://github.com/nopdotcom/ansible/blob/9035c49e1acad9e35375669b76075c290effd987/lib/ansible/playbook/role/__init__.py#L206
    Jay Carlson
    @nopdotcom
    Yeah. If I read the code correctly, the added code in that PR won't affect that. It's possible I should explicitly check preconditions at the top of the patch part, instead of depending on tracing through that if/then/else tree.
    matrixbot
    @matrixbot
    wilornel Oh dang, I'm having trouble doing something in Ansible
    wilornel I'd like to: run a long-running task A (poll: 0), run a task B repetitively which is the command module until A is complete.
    I'm not sure how popular is the approach above but it has been working quite well for us. The problem now is we are expanding into multiple datacenters and I'm not quite sure how to adapt the strategy above for that without duplicating files - something undesirable. Any tips?
    ecaepp
    @ecaepp
    @nicholasamorim is your inventory static, dynamic, or combo of both?
    Nicholas Amorim
    @nicholasamorim
    @ecaepp static
    Nicholas Amorim
    @nicholasamorim

    I have an inventory folder to manage multi-dc envs. I have 01_us1_hosts, 10_ca1_hosts and 99_groups. I declare hosts for each dc in the first two and in the groups one I do something like this, declaring group of groups:

    [us1:children]
    us1_database
    us1_webservers
    
    [ca1:children]
    ca1_database
    ca1_webservers

    It works very well. The problem being... I was expecting that vars set in group_vars/ca1.yaml (or us1.yaml would be resolved, but they are not - any ideas why?

    Nevermind, already solved. The group_vars folder has to move one sub-level inside the inventory folder to work. Thanks for being my rubber duck :D
    matrixbot
    @matrixbot
    wilornel heeelp!
    wilornel Apt module is not working well I think
    matrixbot
    @matrixbot
    dziendobry Hi. Any article/tutorial or help on how to do inner loops on Ansible? I have a list of servers to interate trough and I have a list of shell commands to execute in each of those servers (from a bat file) and so far I have no idea on how to implement such task on Ansible. /o\
    ecaepp
    @ecaepp
    @nicholasamorim sorry it got really busy yesterday and I wasn't able to respond. Glad you got it figured out.
    matrixbot
    @matrixbot
    dziendobry Hi. what would be the best way to split a list in smaller chuncks using ansible? I have a list of servers which I use to execute async tasks. The problem is that the computer gets overload and now Im trying to find a way to send only 5 tasks at a time. Is it possible with ansible?
    ecaepp
    @ecaepp
    how would I respond to dziendobry issue from matrixbot?
    matrixbot
    @matrixbot
    Michael S. Moody If I understand what you mean, there's a concurrent task limit setting that's set in ansible.cfg
    matrixbot
    @matrixbot
    dziendobry Michael S. Moody: :D. I believe changing that value could help me. I will performe some tests. Thanks
    reneeshk
    @reneeshk
    Help!. How do I run an ad-hoc command using the raw module?
    ecaepp
    @ecaepp
    @reneeshk should just be ansible -m raw -a "cat my_file" my_host, and just as a side note Ansible recommends using the command or shell module when possible.
    reneeshk
    @reneeshk
    @ecaepp Thank you.
    Ahmed Shabana
    @AhmedShabana2_twitter

    Dears , I write down a module , but for reason that I donot know it did not ever run!!
    it just print the name tag with no real execution
    [root@ip-192-168-81-181 ansible]# ansible-playbook local.site.yml -vvv
    ansible-playbook 2.8.6
    config file = None
    configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
    ansible python module location = /usr/lib/python2.7/site-packages/ansible
    executable location = /bin/ansible-playbook
    python version = 2.7.16 (default, Jul 19 2019, 23:05:17) [GCC 7.3.1 20180712 (Red Hat 7.3.1-6)]
    No config file found; using defaults
    host_list declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
    Skipping due to inventory source not existing or not being readable by the current user
    script declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
    auto declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
    Skipping due to inventory source not existing or not being readable by the current user
    yaml declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
    Skipping due to inventory source not existing or not being readable by the current user
    ini declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
    Skipping due to inventory source not existing or not being readable by the current user
    toml declined parsing /etc/ansible/hosts as it did not pass it's verify_file() method
    [WARNING]: No inventory was parsed, only implicit localhost is available

    [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

    PLAYBOOK: local.site.yml **
    1 plays in local.site.yml

    PLAY [Preparing localhost] **
    META: ran handlers

    TASK [Trying to include role] *

    TASK [Just for debug] *
    task path: /usr/local/src/ansible/local.site.yml:15
    ok: [localhost] => {
    "msg": "???"
    }
    META: ran handlers
    META: ran handlers

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


    And below is my directory structure and my playbook


    [root@ip-192-168-81-181 ansible]# tree
    .
    ├── local
    │ ├── host_vars
    │ │ └── localhost
    │ └── vars
    │ └── cfg.yml
    ├── local.site.yml
    ├── production
    │ └── host_vars
    ├── roles
    │ └── sdpApp
    │ ├── common
    │ │ └── tasks
    │ │ └── main.yml
    │ └── default
    │ ├── main.yml
    │ └── requirements.yml
    └── test.yml

    10 directories, 7 files

    [root@ip-192-168-81-181 ansible]# cat local.site.yml

    • name: Preparing localhost

      hosts: localhost
      connection: local
      become: yes
      gather_facts: False
      roles:

          - sdpApp

      tasks:

          - name: "Trying to include role"
            include_role:
                    name: sdpApp
      
          - name: "Just for debug"
            debug:
                    msg: "???"
    As you can see that
    TASK [Trying to include role] *
    is empty at debug and not executed ever ?!